我有一个带有ListView的asp.net paget。当在同一listview-row的下拉列表中选择某个值时,我想自动更改文本框的文本。 如何触发事件并更改与下拉列表相同行的textbox.text?
答案 0 :(得分:2)
您可以通过将NamingContainer
中的sender
投射回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;
}