I have a dropdown list which is binded to a dataTable as follows:
MatID MatName MatGroupID
1 Mat1 11
2 Mat2 22
3 Mat3 33
<asp:DropDownList ID="DropDownList_MAT" runat="server" AutoPostBack="true" OnSelectedIndexChanged="UpdateDetails" DataTextField="MatGroupID" DataValueField="Matname" />
<asp:Label ID="Label_StaticMatName" runat="server" />
code:
protected void UpdateDetails(object sender, EventArgs e)
{
Label_StaticLockGroupName.Text = DropDownList_MAT.SelectedItem.Value;
}
MatGroupID i have assigned as DataTextField and DataValueField is MatName. When i select a value in dropdown i want the textfield the value field as well as MatID. How do i maintain the MatID as the user selects dropdown value every time
create a class for Mat (that also includes the MatGroupId and MatId), so you can just save the unique ID from the class as DataValue and later you will get the class out of the unique id and then you get all your stored data in your class, because you cant store multiples DataValueFields.
other option would be to build a string, something like:
string value = "1" + "#" + "Mat1" + "#" + "11";
so later on you can split it using:
string MatId = value.Split('#')[0];
string MatName = value.Split('#')[1];
string MatGroupId = value.Split('#')[2];
The simplest solution is:
Populating multiple columns in Value attribute Separated by | (pipe)
string SQL="SELECT ID +' | '+ Field3 as ValField, Field2 as DispField FROM Table1";
ddl.DataSource = GetDataTable(SQL);
ddl.DataTextField = "DispField";
ddl.DataValueField = "ValueField";
ddl.DataBind();
Now Processing the value at postback
string[] values = ddl.SelectedValue.Split('|');
Response.Write ( values[0] );
Response.Write ( values[1] );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With