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:
or from the DataSource_Inserting event and not the DetailsView_ItemInserting event u can do this:
Good Luck!