Rob Segal
Rob Segal

Reputation: 7625

Obtaining selected item, value or index from drop down list in formview after button press

I have a formview with an insertion template. In this template there is a drop down list with a number of items I want users to be able to select from. Beside the drop down list there is a button which I am using to add the selected item from the drop down list to a gridview which also exists in the insertion template.

My problem is that when I click the button to add the selected item from the drop down list the selected item, index or value from the drop down list are not available. I am using a OnClick event handler to catch the event from the button click but I suspect there is some kind of refresh of the template going on here which I am not understanding as nothing appears to be accessible from the button event handler. I don't believe a postback is occurring as I have disabled the CausesValidation property for my button.

Upvotes: 1

Views: 1396

Answers (4)

Rob Segal
Rob Segal

Reputation: 7625

So it turns it out it was all my fault. The formview control I have is contained in a panel which did not have view state enabled. This was preventing the drop down list from remembering the item I had selected it seems.

Thanks all for your comments and suggestions.

Upvotes: 0

Josh Mein
Josh Mein

Reputation: 28645

It seems like you are binding your DDL on postbacks as well. If the ddl data isnt hardcoded and you have the call for your ddl databind function in the Page_Load, you need to call the function like this to ensure it is not bound on postback:

if(!IsPostBack)
{
    BindDDL();
}

Otherwise we need more information to help you and please post your code.

Upvotes: 2

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

I would have to see the code, but it sounds like you are getting a rebind prior to pulling the chosen items. One way to examine this is to add a watcha nd then make sure you code in the various ASP.NET events and then watch.

Without seeing what you are doing in code, I cannot tell if this is a drag and drop anomoly or something you have coded in. But the symptoms you are describing fits the typcial bind in Page_Load scenario, which is what jmein was aiming at.

Upvotes: 0

Iain M Norman
Iain M Norman

Reputation: 2085

If you are clicking an asp:Button with an OnClick eevent attached to it then you are posting back to the server, no matter whether or not CausesValidation is true or not.

Are you binding data to the DropDownList? If so and you are rebinding it on postback then you'll not have the selected item you're expecting.

Can you paste us the code here?

Upvotes: 0

Related Questions