gpt4 book ai didi

c# - 如何通过更改同一 ListView 行中的下拉列表项来更新文本框中的文本

转载 作者:行者123 更新时间:2023-11-30 22:57:04 27 4
gpt4 key购买 nike

我有一个带有 ListView 的 asp.net 页面。 I want automatically change the text of an textbox when a certain value in a dropdownlist of the same listview- row is selected.如何触发事件并更改与下拉列表同一行的 textbox.text?

最佳答案

您可以通过将 senderNamingContainer 转换回 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com