views:

244

answers:

4

I have a dropdownlist and a textbox with a button on a form. When a button is clicked it does not go to my onclick even in the code but it goes to my dropdownlist's selectedIndexchanged event. How can I fix this?

A: 

Make sure the event handler is handling the correct control, or the button tag is referencing the correct method on it's onclick element.

Shawn Simon
+4  A: 

I'm not a VB.net person but try changing your asp:button to this:

 <asp:Button id="btnlookup" Height="24px" Text="LookUp" Width="60px" 
 OnClick="btnlookup_Click" runat="server"/>
TStamper
Why was this downvoted, when this is a noticeable error the OP has in his code: he has btnlookup_Click="btnlookup_Click" instead of OnClick="btnlookup_Click"
TStamper
thank you, for taking the downvote back. try looking carefully at an answer before downvoting it
TStamper
+1  A: 

Does your Page_Load have a call to changing the selected index or the items in the DropDownList outside of checking for postback?

blu
Actually you are very close to being right. Great Idea. However, it's not in the page load but i do it on the Page_SaveStatecomplete event. DropDownList1.Items.Insert(0, "Select a Vendor")DropDownList1.SelectedIndex = 0How can I do this without having this problem?
Eric
interesting, on http://msdn.microsoft.com/en-us/library/ms178472.aspx it says, "SaveStateComplete - Before this event occurs, ViewState has been saved for the page and for all controls. Any changes to the page or controls at this point will be ignored. Use this event perform tasks that require view state to be saved, but that do not make any changes to controls." Is this really where you should add the default vendor. You probably should add it in Page_Load on a non postback (first time the page runs). And also use "" as the value for required field validation.
blu
I thought that too but for some reason I can't add a default on Page_Load. Perhaps because the data comes from a datasource maybe?
Eric
The only two places I can set the default value is on SaveStateComplete and PreRenderComplete. They both cause the same problem.
Eric
In Page_Load you can do, this.DropDownList1.Items.Insert(0, new ListItem("Select a Vendor", "")); after you call DataBind on the list.
blu
That worked. The only thing I had to do was Set the DataSource and the Databind in the code as opposed to choosing a datasource from the control on the form. Thanks a lot for your time and help!
Eric
A: 

If your dropdown list is not set to autopostback, and you change its value before clicking the button (and triggering a postback), then the dropdown's onchange event is supposed to fire.

...not that this explains why you click handler isn't firing.

kristian
autopostback for the dropdown is set to true.
Eric