简体   繁体   中英

Using the value of a DropDownList in a GridView on inserting a new entry

I searched for a few days now but didn't find a solution. So here is my scenario:

I have a DetailView with a DataBindung to a database and DefaultMode set to " Insert " as a input-form for a new db-entry. One field in this DetailsView is a TemplateField with a InsertItemTemplate containing a DropDownList. This DropDownList is also bounded to another SqlDataSource, using a select-parameter "UserID" to get all rows of a database for the actual logged-in user. Those rows are shown in my DropDownList and when looking at the sourcecode in my browser I see the correct "option"-attributes with all those nice db-IDs in their corresponding value. So everything is fine to this point.

Now I press my Insert-Button to send my data back to the server. But when trying to get the selected value from my DropDownList in DetailsView1_ItemInserting() with the following code I get a null-value.

var list = (DropDownList)DetailsView1.Rows[0].FindControl( "DropDownList1" );
InsertEntryDataSource.InsertParameters["DropDownList1Value"].DefaultValue = list.Items[list.SelectedIndex].Value;

The same code works with another form I created in this project. The only difference is, that the other DropDownList is bounded to a DataSource without a select-parameter I have to set on runtime (the current logged-in UserID).

My resumption was that the DropDownList is empty after the postback because the databinding wasn't yet done. So I set the select-parameter for the SqlDataSource my DropDownList is bounded to in DetailsView1_ItemInserting() - and now the above line got my value.

BUT : I just tested the form another time and I determined, that no matter which entry I select in my DropDownList, I get always the value of the first entry! The whole procedure seems to clear my SelectedIndex of the DropDownList - so now I have a value to insert instead of null, but it's the wrong one. :-(

So my question: How can I access the selected value of my DropDownList in DetailsView1_ItemInserting() to save it to the database?

Thanks in advance, Anheledir

You can use this syntax to access the ddl without writing code:

<InsertParameters>
<asp:ControlParameter ControlID="DetailsView1$DropDownList1" Name="DropDownList1" PropertyName="SelectedValue" Type="String" />
</InsertParameters>

or from the DataSource_Inserting event and not the DetailsView_ItemInserting event u can do this:

  e.InputParameters["InputParameterFieldName"] = ((DropDownList)DetailsView1.FindControl("DropDownList1")).SelectedValue;

Good Luck!

Are you ever inserting more than one row ata a time? I'd like to back up a step and ask why it is you need to have the insert as part of the GridView itself and not it's own part of the UI (with some input controls and a submit button). The separation in space emphasizes the separation of function - one part for inserting, one part for reporting/display. The method you're using sounds like a lot of struggle for little reward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM