【问题标题】:How to get reference of DropDownList into codebehind?如何在后面的代码中获取 DropDownList 的引用?
【发布时间】:2011-05-11 08:18:53
【问题描述】:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  Onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing">
    <Columns>
        <asp:TemplateField HeaderText="Test">
            <EditItemTemplate>
                <asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                            OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                            <asp:ListItem>Test1</asp:ListItem>
                            <asp:ListItem>Test2</asp:ListItem>
                            <asp:ListItem>Test3</asp:ListItem>
                        </asp:DropDownList>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("SS") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rate">
            <EditItemTemplate>
                <asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text="Label" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

当网格处于编辑模式时,如何在DropDownList1_SelectedIndexChanged 中获取DropDownList2 的引用?

【问题讨论】:

    标签: c# .net asp.net gridview updatepanel


    【解决方案1】:

    编辑您的问题后。你可以实现...

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList DropDownList2 = ((DropDownList)((DropDownList)sender).Parent.FindControl("DropDownList2"));
    }
    

    【讨论】:

    • 它在编辑模式之外不存在 - 它是您的编辑模板的一部分。
    • @james;那么您正在查看 RowUpdating 事件吗?我已经更新了我的答案。
    • @jams;更新了我的答案。立即尝试。
    【解决方案2】:

    您应该可以通过GridView1_RowEditing 访问它。您需要使用以下方式将其分配给控件:

    DropDownList list = e.Row.FindControl("DropDownList1") as DropDownList;
    

    编辑:根据新信息,应该这样做:

    int index = -1;
    
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
     index = e.Row.Index;
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
     DropDownList DropDownList2 = GridView1.Rows[index].FindControl("DropDownList2") as DropDownList;
    }
    

    【讨论】:

    • 网格处于编辑模式 DropDownList1 是可见的,当时我必须在网格外获取它的引用。
    • 它在编辑模式之外不存在 - 它是您的编辑模板的一部分。你的意思是你需要在它改变后从中获取价值吗?我在那里看到了自动回发。
    • @jesus.tesh:NULLreference 是返回。
    • 也许 DropDownList 上的自动回发会导致 GridView 不再处于编辑模式?我知道你在那里有 UpdatePanels,但我不明白为什么它会给出一个空引用? DropDownList2 与 DropDownList1 在同一行,所以我唯一能想到的是您不再处于 GridView 的编辑模式?
    【解决方案3】:

    我认为您无法在代码隐藏中获得对下拉列表的引用,因为它不存在。在您的示例中,下拉列表仅存在于更新模式中,因此您不走运。但是,您也许可以动态添加一个,即创建GridView 或在代码隐藏中添加模板。

    如果您提供有关您的实际问题的更多信息,我们可能会找到替代解决方案。

    【讨论】:

      猜你喜欢
      • 2017-01-12
      • 1970-01-01
      • 2011-11-11
      • 2015-05-14
      • 1970-01-01
      • 2017-03-05
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多