Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding 3 values to dropdown list

Tags:

c#

asp.net

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

like image 326
Eve Avatar asked May 17 '26 16:05

Eve


2 Answers

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];
like image 92
Postback Avatar answered May 20 '26 05:05

Postback


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] );
like image 26
SRK - Khurram Avatar answered May 20 '26 06:05

SRK - Khurram