Thomas
Thomas

Reputation: 80

ASP.net Image Button Issue

I have a hyperlink on a page that sends certain variables e.g. Default.aspx?var1=x&var2=y

I then create an ImageButton dynamically thereafter. My ImageButton has its own event. It seems that the ImageButton inherits the values sent from the hyperlink. If I right click on the image button and view is properties via the browser it points to the following as well Default.aspx?var1=x&var2=y , I then tried to turn view state off which didnt work.

I also tried specifying the PostBackUrl so that the url variables are not sent. But then its event doesnt trigger.

Is there any way to not h

Upvotes: 1

Views: 902

Answers (2)

Jamie Treworgy
Jamie Treworgy

Reputation: 24334

The postback URL for a form in ASP.NET will ALWAYS be the same URL that the form was originally loaded using. So if you load a page from a hyperlink with a querystring, then use a postback control on that page (like an ImageButton), it will always post back to the same URL it was originally loaded with, including the querystring.

There are different ways to change the postback URL that depend on the version of ASP.NET you're using, but if 2.0 or below it will have to be javascript. Search on this issue specifically (changing postback url or form action in asp.net) to find the answers to your problem.

Other less elegant solutions include redirecting a page to itself with no querystring after the initial load with a querystring.

You can also just ignore querystrings on postback, which is almost always the desired behaviour, so for the most part this is a cosmetic problem. E.g. in your code just do...

if (!Page.IsPostback) {
 // check for querystring data and do stuff if there, otherwise ignore it
}

Upvotes: 1

Madhur Ahuja
Madhur Ahuja

Reputation: 22661

That's default behavior of ASP.NET. Since ImageButton will just cause the execution of event handler , the hyperlink attached to it will be its same page.

Why is this a concern ?

Upvotes: 1

Related Questions