PostBackUrl
-
Here's my problem: I have a button and when I click it I want to open a new page. I know that I can do this thing very easy by setting the PostBackUrl property of the button. But here's what I want to do: on my page I have a textbox and, for example when I press the button I want to test the text from the TextBox and if it's ok I want to show the new page; if not I want to display a message. I want to do that using c# language, and not JavaScript. I've tried this:
protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == "something") Button1.PostBackUrl = "NewPage.aspx"; else .................... }
Ok, everithing is fine until now. But when I press the button, assuming that in my textbox I've written "something", the NewPage isn't displayed. My current page is loaded again and when I press the button again then NewPage.aspx is displayed. And I don't know why this is happening. Can anyone help me please? -
Here's my problem: I have a button and when I click it I want to open a new page. I know that I can do this thing very easy by setting the PostBackUrl property of the button. But here's what I want to do: on my page I have a textbox and, for example when I press the button I want to test the text from the TextBox and if it's ok I want to show the new page; if not I want to display a message. I want to do that using c# language, and not JavaScript. I've tried this:
protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == "something") Button1.PostBackUrl = "NewPage.aspx"; else .................... }
Ok, everithing is fine until now. But when I press the button, assuming that in my textbox I've written "something", the NewPage isn't displayed. My current page is loaded again and when I press the button again then NewPage.aspx is displayed. And I don't know why this is happening. Can anyone help me please?Try:
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "something")
{
Response.Redirect("NewPage.aspx");
}
else
{
....................
}
}No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
-
Try:
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "something")
{
Response.Redirect("NewPage.aspx");
}
else
{
....................
}
}No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones "Rumour has it that if you play Microsoft CDs backwards you will hear Satanic messages.Worse still, is that if you play them forwards they will install Windows"
I think he wants to "Postback" to a new page and not re-direct to a new page.
Mark Graham (MCP) blogging about Design Patterns, C#, Asp.Net, CSS, Javascript and Ajax at: DESIGN CODE TEST[^]
-
Here's my problem: I have a button and when I click it I want to open a new page. I know that I can do this thing very easy by setting the PostBackUrl property of the button. But here's what I want to do: on my page I have a textbox and, for example when I press the button I want to test the text from the TextBox and if it's ok I want to show the new page; if not I want to display a message. I want to do that using c# language, and not JavaScript. I've tried this:
protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == "something") Button1.PostBackUrl = "NewPage.aspx"; else .................... }
Ok, everithing is fine until now. But when I press the button, assuming that in my textbox I've written "something", the NewPage isn't displayed. My current page is loaded again and when I press the button again then NewPage.aspx is displayed. And I don't know why this is happening. Can anyone help me please?The Postback url defaults to the current page and this is where your first click will return to, because, at this first click point your browser doesn't know to postback to NewPage.aspx. Only on the second click (after you've set Button1.PostBackUrl = "NewPage.aspx") will you get postback to the new page. IF YOU NEED TO POSTBACK TO A DIFFERENT PAGE THEN.... you can't avoid using javascript. Setting PostBackUrl on the server won't affect the current request (your first click). IF YOU'RE NOT REALLY CONCERNED WITH POSTBACK THEN.... do what the other guys suggested and replace the Button1.PostBackUrl = "NewPage.aspx" with the ReDirect, or Server.Transfer("NewPage.aspx"); code.
Mark Graham (MCP) blogging about Design Patterns, C#, Asp.Net, CSS, Javascript and Ajax at: DESIGN CODE TEST[^]