我正试着做一个汽车维修项目,我有个小问题。该项目类似于:
4种不同车型的维修方案。
CarA ( MirrorA, etc etc )
CarB ( MirrorB, etc etc )
CarC ( MirrorC, etc etc )
CarD ( MirrorD, etc etc )我想做的是,当我选择一个Car (从一个DropDownList),程序选择正确的维修计划的汽车!
SqlCommand cmd = new SqlCommand("Select id, description from accauto_maps", con);
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "description";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind(); 现在我被困住了。
发布于 2015-03-31 18:14:35
现在,您需要在这个列表中设置SelectedValue和/或定义事件OnSelectedIndexChanged,在其中处理用户的选择
下面是我们如何做到这一点的例子:
<asp:DropDownList ID="ddlStatus" AutoPostBack="True" runat="server" DataSourceID="EDSLookStatus"
DataValueField="cd"
DataTextField="lookupName"
OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" />
<asp:TextBox ID="txtBxStatus" runat="server" Text='<%# Bind("status") %>' Visible="False" />诀窍是:添加不可见的文本框,链接到"status“。当ddl更改值时,它将新值设置为此txtBx:
protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = FormViewClient.FindControl("ddlStatus") as DropDownList;
TextBox txtBx = FormViewClient.FindControl("txtBxStatus") as TextBox;
if (ddl != null && txtBx != null)
{ txtBx.Text = ddl.SelectedValue; }
}若要设置选定的值:
<asp:FormView ... OnDataBound="FormViewClient_DataBound" >和
protected void FormViewClient_DataBound(object sender, EventArgs e)
{
...
DropDownList ddl = FormViewClient.FindControl("ddlStatus") as DropDownList;
TextBox txtBx = FormViewClient.FindControl("txtBxStatus") as TextBox;
if (ddl != null && txtBx != null)
{ ddl.SelectedValue = txtBx.Text; }
...
}https://stackoverflow.com/questions/29374432
复制相似问题