[英]How to populate a textbox based on the selected value of a dropdown list in an asp.net c# web forms application?
I have a dropdown list (doc_rvw_sub_recip_list) that is populated with names from tbl_ad_users. 我有一个下拉列表(doc_rvw_sub_recip_list),其中填充了来自tbl_ad_users的名称。 tbl_ad_users contains a field titled "domain_user".
tbl_ad_users包含一个名为“ domain_user”的字段。 I would like to populate an asp:hiddenfield (hdn_domain_user) with the "domain_user" based on the selected value in the doc_rvw_sub_recip_list dropdown list.
我想基于doc_rvw_sub_recip_list下拉列表中的选定值,使用“ domain_user”填充asp:hiddenfield(hdn_domain_user)。 For testing I used a dropdown list (domain_user_ddl) instead of a hidden field, and it works as needed.
为了进行测试,我使用了一个下拉列表(domain_user_ddl)而不是隐藏字段,它可以根据需要工作。 But I don't know how to get the value into a hidden field as opposed to using a dropdown list.
但是我不知道如何将值放入隐藏字段,而不是使用下拉列表。
The following is doc_rvw_sub_recip_list: 以下是doc_rvw_sub_recip_list:
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
The following is the sql data source for domain_user_ddl: 以下是domain_user_ddl的sql数据源:
<asp:SqlDataSource ID="sdc_domain_user_ddl" runat="server" ConnectionString='<%$ ConnectionStrings:idrfConnectionString %>' SelectCommand="SELECT [domain_user] FROM [tbl_ad_users] WHERE ([email_address] = @email_address)">
<SelectParameters>
<asp:ControlParameter ControlID="doc_rvw_sub_recip_list" PropertyName="SelectedValue" Name="email_address" Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
The following is domain_user_ddl: 以下是domain_user_ddl:
<asp:DropDownList ID="domain_user_ddl" runat="server" DataSourceID="sdc_domain_user_ddl" DataTextField="domain_user" DataValueField="domain_user" AutoPostBack="true"></asp:DropDownList>
How do I get this to work for hdn_domain_user just as it works for domain_user_ddl? 如何使它对hdn_domain_user起作用,就像对domain_user_ddl起作用一样?
ASPX ASPX
Add a OnSelectedIndexChanged event to your dropdown... 将OnSelectedIndexChanged事件添加到下拉菜单中...
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="doc_rvw_sub_recip_list_SelectedIndexChanged"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
Add your hidden field... 添加您的隐藏字段...
<asp:HiddenField runat="server" ID="hdn_domain_user" />
Code Behind 背后的代码
Handle the SelectedIndexChanged event... 处理SelectedIndexChanged事件...
protected void doc_rvw_sub_recip_list_SelectedIndexChanged(Object sender, EventArgs e)
{
// Populate the hidden field if the dropdown has a selected value
if (doc_rvw_sub_recip_list.SelectedValue != null)
hdn_domain_user.Value = doc_rvw_sub_recip_list.SelectedValue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.