3

我有一个绑定到数据表的下拉列表,如下所示:

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" />

代码:

protected void UpdateDetails(object sender, EventArgs e)
{
 Label_StaticLockGroupName.Text = DropDownList_MAT.SelectedItem.Value;
}

我指定为 DataTextField 的 MatGroupID 和 DataValueField 是 MatName。当我在下拉列表中选择一个值时,我想要文本字段值字段以及 MatID。当用户每次选择下拉值时,我如何维护 MatID

4

2 回答 2

2

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];
于 2013-04-25T05:32:20.220 回答
1

最简单的解决方案是:

在 Value 属性中填充多个列,由 | 分隔 (管道)

string SQL="SELECT ID +' | '+ Field3 as ValField, Field2 as DispField FROM Table1";

ddl.DataSource = GetDataTable(SQL);
ddl.DataTextField = "DispField";
ddl.DataValueField = "ValueField";
ddl.DataBind();

现在处理回发时的值

string[] values = ddl.SelectedValue.Split('|');
Response.Write ( values[0] );
Response.Write ( values[1] );
于 2017-07-02T07:50:47.170 回答