...

View Full Version : Simple ASP.Net if statement in C#



Trickstar
06-19-2012, 07:32 PM
I feel really dumb for asking about this but I have a problem using if statements in ASP.Net because I'm going from client-side script to server-side so I'm used to using things like it is in JavaScript because I have a really strong ability in client-side development (CSS/JavaScript/HTML) and my book I learned about ASP.Net from has all the examples in Visual Basic which I completely hate so I decided to use C# because it's more like JavaScript. This is a simple one I tried thats not working that would work in JavaScript but not in ASP. The code should be self-explanatory of what I'm trying to do. Can somebody just show me how to rewrite this correctly so I can go on my way to developing actual applications please? I believe the way I'm rewriting the variable in the if statement is incorrect in C# as well, so also tell me if thats correct if you could, thank you.


<script language="C#" runat="server">
string copyrightYear = DateTime.Now.Year.ToString();
if (copyrightYear != "2012") {
copyrightYear = "2012-" + DateTime.Now.Year.ToString();
}
</script>
<div class="copyright" align="center">Copyright &copy; <% Response.Write(copyrightYear); %> SiteName.com<br />All Rights Reserved</div>

alykins
06-19-2012, 09:32 PM
So it's not in a web form, but in a windows form- but you should be able to follow the logic...




namespace sample
{
public partial class Form1 : Form
{
private DateTime[] _testDates = new DateTime[4] { new DateTime(2001, 05, 16), new DateTime(2008, 09, 20), new DateTime(1995, 06, 03), new DateTime(1880, 02, 02) };

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text += string.Format("Copyright 2012-{0:yyyy}\n", DateTime.Now);
foreach (DateTime _d in _testDates)
{
richTextBox1.Text += string.Format("Copyright {0:yyyy}-2012. Current year is {1:yyyy}\n", _d, DateTime.Now);
}

}
}
}


this outputs...


Copyright 2012-2012
Copyright 2001-2012. Current year is 2012
Copyright 2008-2012. Current year is 2012
Copyright 1995-2012. Current year is 2012
Copyright 1880-2012. Current year is 2012


you will want to check out these references...
datetime (http://msdn.microsoft.com/en-us/library/system.datetime.aspx)
datetime.now (http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx)
string.Format (http://msdn.microsoft.com/en-us/library/system.string.format.aspx)
datetime and strings (http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx)

alykins
06-19-2012, 09:35 PM
Sorry, I didn't really answer the "if" part....
Altering this part of the code...


private DateTime[] _testDates = new DateTime[5] { new DateTime(2001, 05, 16), new DateTime(2008, 09, 20), new DateTime(1995, 06, 03), new DateTime(1880, 02, 02), DateTime.Now };
/// and also...

foreach (DateTime _d in _testDates)
{
if (_d.Year != 2012)
{
richTextBox1.Text += string.Format("Copyright {0:yyyy}-2012. Current year is {1:yyyy}\n", _d, DateTime.Now);
}
else
richTextBox1.Text += "The years are equal.";
}

would then alter the output to


Copyright 2012-2012
Copyright 2001-2012. Current year is 2012
Copyright 2008-2012. Current year is 2012
Copyright 1995-2012. Current year is 2012
Copyright 1880-2012. Current year is 2012
The years are equal.

Old Pedant
06-22-2012, 08:56 PM
Actually, I think your problem is simply that the code in the <script runat="Server"> tag is running *AFTER* the <% Response.Write ... %> code!

Try just changing it to


<%
string copyrightYear = DateTime.Now.Year.ToString();
if (copyrightYear != "2012") {
copyrightYear = "2012-" + DateTime.Now.Year.ToString();
}
%>
<div class="copyright" align="center">Copyright &copy; <%=copyrightYear%> SiteName.com<br />All Rights Reserved</div>

IF that doesn't work, you can do it all inline:


<div class="copyright" align="center">Copyright &copy;
<%= "2012" + (DateTime.Now.Year > 2012 ? "-" + DateTime.Now.Year : "" )%>
SiteName.com<br />All Rights Reserved</div>


*******

EDIT: I just tried BOTH those methods, and both worked.

And, indeed, the problem is that the <script runat="server"> code executes *AFTER* the <%...%> code. You can easily prove that by setting breakpoints in the debugger.

(I tested by using both 2012 and then 2011 as the test year values.)

alykins
06-23-2012, 05:04 PM
Actually, I think your problem is simply that the code in the <script runat="Server"> tag is running *AFTER* the <% Response.Write ... %> code!


I didn't think of that :P I guess I was thinking too much on code-behind

nicholagi
06-25-2012, 06:52 PM
using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number;

Console.WriteLine("Please enter a number between 0 and 10:");
number = int.Parse(Console.ReadLine());

if(number > 10)
Console.WriteLine("Hey! The number should be 10 or less!");
else
if(number < 0)
Console.WriteLine("Hey! The number should be 0 or more!");
else
Console.WriteLine("Good job!");

Console.ReadLine();
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum