Answered by:
How to get values in code behind from a nested DropDownList Control?

Question
-
User426001450 posted
Hello to all !!
I do have a DropDownList nested in a DataList. When DataList displays on the page, if it has 20, 50, 75 or n records, it is obvious that there are going to be many ddl display on the DataList, one for each row. How may I access from code behind the particular ddl that user changes in order for me to get the new values?
When the text or value changes I would like to get in code behind the item index, value, text, and AutoNumber of the particular ddl and record.
I'm using Visual Basic, if you are willing to help please provide code in VB, thanks
Here is my code for the ddl:
<asp:DataList ID="DogRosterApprovedForShowBallotDataList" runat="server" GridLines="Both" DataSourceID="DogRosterApprovedForShowBallotSqlDataSource" DataKeyNames="DogAutoNumber, UserName, TDogShowStatus, AutoNumber, TDogShowBallotStatus" > <AlternatingItemStyle BackColor="#FFFBD6" /> <HeaderStyle BackColor="#00693E" ForeColor="White" /> <HeaderTemplate> Official dog show voting ballot <br /> </HeaderTemplate> <ItemTemplate> <br /> Performer # : <%# Container.ItemIndex + 1 %> <br /><br /> First Name / Click for info: <i style="color:#00693E"> <asp:HyperLink ID="OfficialListHyperLink" Target="_blank" Text='<%# Eval("TPerformerFirstName") %>' runat="server" NavigateUrl='<%#String.Concat("~/TalentShow/PerformerProfilePage.aspx?UserName=", Convert.ToString(Eval("UserName"))) %>' > </asp:HyperLink></i> <br /> Last Name : <i style="color:#00693E"><%# Eval("TPerformerLastName") %></i> <br /> Dog Name : <i style="color:#00693E"><%# Eval("TDogStageName") %></i> <br /><br /> Your selection : <asp:DropDownList ID="SelectionForVotingDropDownList" AutoPostBack="true" runat="server" > <asp:ListItem Selected="True" Value="Select">-- Select place --</asp:ListItem> <asp:ListItem Value="First place"> First place </asp:ListItem> <asp:ListItem Value="Second place"> Second place </asp:ListItem> <asp:ListItem Value="Third place"> Third place </asp:ListItem> </asp:DropDownList> <br /><br /> </ItemTemplate> <FooterTemplate> <%# DogRosterApprovedForShowBallotDataList.Items.Count %> Record(s) returned </FooterTemplate> </asp:DataList>
Thanks to all for helping !!
Friday, January 12, 2018 5:27 AM
Answers
-
User-1716253493 posted
AFAIK, DataList don't have multiple datakeys
You can use hiddenfeild or other control, or use gridview instead
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, January 14, 2018 8:11 AM -
User426001450 posted
This is completely stupid, there is lot nonsense and the one to blame here is msft.
1) The field once created in the DataList look like this: DataKeyField="AutoNumber". There is no other option like DataKey, Datakeys, Datakeyfields or whatever. This is the only option. However in the code behind, we cannot use the same word ‘DataKeyField’ to retrieve the value, but DataKeys instead, this is stupid and confusing.
2) In the code behind, the following will work to retrieve the value:
DataList.DataKeys(idx). But you need all the following code to find the index:
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim item As DataListItem = CType(ddl.NamingContainer, DataListItem)
Dim idx As Integer = CType(item, DataListItem).ItemIndex
3) Another stupidity or limitation is that DataKeyField can only hold one value. I added another value like : DataKeyField="AutoNumber, UserName". But I couldn’t retrieve anything and an error was displayed. This is the error: 'System.Data.DataRowView' does not contain a property with the name 'AutoNumber, UserName'.
So it can hold the AutoNumber or the UserName value but not both. This shouldn’t be because these limitations force us to enter more lines of code to get other values and good programing is about less coding.
Another stupid thing is that if it can only hold one value why I cannot retrieve it in the code behind as ‘DataKey’ without the ‘s’ but it has to be as DataKeys. Nonsense. And once again should be DataKeyField instead anyway for the retrieving. I already mentioned that. But if your code is DataList.DataKeyField(idx) to retrieve value you will get nothing.
Well I got fed up with this control, enough is enough. Thanks for your help.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 15, 2018 7:17 PM -
User426001450 posted
My final suggestion is to use a HiddenField to find all values even though there is more coding involve there is not other way. In order to find how to do it you can take a look at this link: https://forums.asp.net/t/1684637.aspx
Thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 16, 2018 12:47 AM
All replies
-
User-1716253493 posted
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged Dim ddl As DropDownList = CType(sender, DropDownList) Dim item As DataListItem = CType(ddl.NamingContainer, DataListItem) Dim idx As Integer = item.ItemIndex Dim auton As Integer = DataList1.DataKeys(idx) End Sub
Sunday, January 14, 2018 3:16 AM -
User426001450 posted
Thanks for your help.
Your code is pretty close to what I had but still is not working. There are two things that are giving me problems: 1) DataKeys in your code, there are none for DataList, so I changed that to the right one that should be DataKeyField. But it is not working either.
2) Since I have more than one DataKeyField, I think that not only the idx but the DataKeyField name should be include in the code.. So, I made the following changes but it is not working. Any thoughts?
Changes:
Dim ddl As DropDownList = CType(sender, DropDownList) Dim item As DataListItem = CType(ddl.NamingContainer, DataListItem) Dim idx As Integer = item.ItemIndex Dim autonumber As Integer = DataList1.DataKeyField("AutoNumber", idx).ToString
Thanks for your help again
Sunday, January 14, 2018 4:58 AM -
User-1716253493 posted
AFAIK, DataList don't have multiple datakeys
You can use hiddenfeild or other control, or use gridview instead
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, January 14, 2018 8:11 AM -
User426001450 posted
Ok, I understand what you are saying but if that is the case, it should works when the dataKeyField is holding one value only and it is not.
This is what I have just for testing and for trying to understand how this control works:
For the DataList I have:
<asp:DataList ID="DogsRosterApprovedForShowBallotDataList" runat="server" DataSourceID="DogsRosterApprovedForShowBallotSqlDataSource" DataKeyFields ="AutoNumber" >
As you can see, there is only one value in the DataKeyField. And this is my code behind:
Dim ddl As DropDownList = CType(sender, DropDownList) Dim item As DataListItem = CType(ddl.NamingContainer, DataListItem) Dim idx As Integer = CType(item, DataListItem).ItemIndex Dim keyAutoNumber As String = DogsRosterApprovedForShowBallotDataList.DataKeyField(idx).ToString Label2.Text = "This is the autonumber for this record: " + keyAutoNumber
But, it is not working even with one value, What is going on, what is the purpose of the DataKeyFied. I'm just trying to understand this control. Once again thanks for your help.
Thanks
Sunday, January 14, 2018 6:47 PM -
User-1716253493 posted
Check and try this
<asp:DataList ID="DataList1" runat="server" DataKeyField="AutoNumber"> </asp:DataList>
DataKeyField without "s"
Dim AutoNumber As Integer = DataList1.DataKeys(idx)
Monday, January 15, 2018 1:11 AM -
User426001450 posted
This is completely stupid, there is lot nonsense and the one to blame here is msft.
1) The field once created in the DataList look like this: DataKeyField="AutoNumber". There is no other option like DataKey, Datakeys, Datakeyfields or whatever. This is the only option. However in the code behind, we cannot use the same word ‘DataKeyField’ to retrieve the value, but DataKeys instead, this is stupid and confusing.
2) In the code behind, the following will work to retrieve the value:
DataList.DataKeys(idx). But you need all the following code to find the index:
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim item As DataListItem = CType(ddl.NamingContainer, DataListItem)
Dim idx As Integer = CType(item, DataListItem).ItemIndex
3) Another stupidity or limitation is that DataKeyField can only hold one value. I added another value like : DataKeyField="AutoNumber, UserName". But I couldn’t retrieve anything and an error was displayed. This is the error: 'System.Data.DataRowView' does not contain a property with the name 'AutoNumber, UserName'.
So it can hold the AutoNumber or the UserName value but not both. This shouldn’t be because these limitations force us to enter more lines of code to get other values and good programing is about less coding.
Another stupid thing is that if it can only hold one value why I cannot retrieve it in the code behind as ‘DataKey’ without the ‘s’ but it has to be as DataKeys. Nonsense. And once again should be DataKeyField instead anyway for the retrieving. I already mentioned that. But if your code is DataList.DataKeyField(idx) to retrieve value you will get nothing.
Well I got fed up with this control, enough is enough. Thanks for your help.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 15, 2018 7:17 PM -
User426001450 posted
My final suggestion is to use a HiddenField to find all values even though there is more coding involve there is not other way. In order to find how to do it you can take a look at this link: https://forums.asp.net/t/1684637.aspx
Thanks
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 16, 2018 12:47 AM -
User-1716253493 posted
Hi vstorpedo,
Each record has own datakey ie ID Or AutoNumber or DogAutoNumber
If you get the key value of the record, you dont need another value as criteria
ie : delele from tbl where AutoNumber=100 is enough
Tuesday, January 16, 2018 1:11 AM -
User426001450 posted
AFAIK, DataList don't have multiple datakeys
You can use hiddenfeild or other control, or use gridview instead
Thank you oned_gk, you are a great guy, thanks for your help. I picked this answer with the hiddenfield as the best solution for my question. You are one of the best here. Thanks
Tuesday, January 16, 2018 6:57 AM