我有一个带有 ListView 的 asp.net paget。我想在选择同一 ListView 行的下拉列表中的某个值时自动更改文本框的文本。 如何触发事件并更改与下拉列表同一行的 textbox.text?
最佳答案
您可以通过将 sender
的 NamingContainer
强制转换回 ListView DataItem 并使用 FindConrol 来定位 TextBox 来实现此目的。
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>Item A</asp:ListItem>
<asp:ListItem>Item B</asp:ListItem>
<asp:ListItem>Item C</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:ListView>
代码隐藏。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//cast the sender back to a dropdownlist
DropDownList ddl = sender as DropDownList;
//get the current listview dataitem from the dropdownlist namingcontainer
ListViewDataItem item = ddl.NamingContainer as ListViewDataItem;
//find the textbox in the item with findcontrol
TextBox tb = item.FindControl("TextBox1") as TextBox;
//set the text
tb.Text = ddl.SelectedValue;
}
关于c# - 如何通过更改同一 ListView 行中下拉列表的项目来更新文本框中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53875379/