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?
views:
244answers:
4
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
2009-05-19 14:18:25
+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
2009-05-19 14:28:36
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
2009-05-19 14:52:09
thank you, for taking the downvote back. try looking carefully at an answer before downvoting it
TStamper
2009-05-19 15:00:55
+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
2009-05-19 14:31:30
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
2009-05-19 14:40:16
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
2009-05-19 14:48:11
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
2009-05-19 14:56:52
The only two places I can set the default value is on SaveStateComplete and PreRenderComplete. They both cause the same problem.
Eric
2009-05-19 15:01:37
In Page_Load you can do, this.DropDownList1.Items.Insert(0, new ListItem("Select a Vendor", "")); after you call DataBind on the list.
blu
2009-05-19 15:12:23
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
2009-05-19 15:21:39
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
2009-05-19 14:33:21