StrataFrame Forum

How to change the PostBackURL on an ImageButton - programmatically

http://forum.strataframe.net/Topic17325.aspx

By Howard Bennett - 6/25/2008



Hello!



I'm wanting to change the PostBackURL on an image button based on the value in a text box. Here's my code:



protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

{

if (this.txtCustomerCode.Text == "8868")



{

this.ImageButton1.PostBackUrl = "~/New Referral.aspx";

}



else



{

this.ImageButton1.PostBackUrl = "";

}

}



Right now - the button doesn't do anything...which is what I want if the Customer Code is incorrect. However - when the code is correct - it still does nothing. Can someone point me in the right direction here?



Thanks - HB
By Dustin Taylor - 6/25/2008

Howdy BigGrin

My guess is that the value isn't getting set until after the initial postback. When you click the button, it has to make a postback in order for tha code in the click to be fired, so by the time you overwrite that postback URL, it has already happened.

You could try checking the value of the text box in the post back and redirecting based on the value of the text box:

protected void Page_Load(object sender, EventArgs e)
{
   if(this.Page.IsPostBack && this.txtCustomerCode.Text == "8868"){
      this.Page.Response.Redirect("~/New Referral.aspx");
   }
}

Grabbing it before the postback is possible, but would mean venturing into the wide world of javascript and ajax Wink

By Howard Bennett - 6/25/2008

Thanks - that works - except I have other buttons that need to go to other pages regardless of what's in the txtCustomerCode.text.



I may have to pass the text to the target page and have it check what's passed by this page before loading...don't know how to do that either...but hopefully there's a way.



It's so "fun" learning new stuff! Crazy



HB


By Trent L. Taylor - 6/25/2008

Create a private method on your form that accepts the object from the Load on a post back that determines which avenue to take.  There are other ways as well, but that would be a quick and easy way to get it going.