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

标签 c# asp.net listview drop-down-menu textbox

我有一个带有 ListView 的 asp.net paget。我想在选择同一 ListView 行的下拉列表中的某个值时自动更改文本框的文本。 如何触发事件并更改与下拉列表同一行的 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/

相关文章:

ASP.Net ListView 回发速度慢, ListView 中有大量数据

java - Jface:Listviewer 抛出 AssertionFailedException

android - 在ListView中添加多个Image并获取每个点击事件

asp.net - 在 asp :repeater i need to repeat the following 中的 vb.net 中重现此内容

c# - IoC 和 "hiding implementation details"

c# - 企业图书馆的优缺点

c# - 在手机上禁用缓存

c# - DropDownList 不调用 SelectedIndexChanged?

c# - "this file is blocked because it came from another computer"- ajax 权限问题

c# - X509Certificate2 访问私钥到安全 token