【问题标题】:GridView on change of dropdown list selection更改下拉列表选择时的 GridView
【发布时间】:2014-01-31 07:22:47
【问题描述】:

所以我有一个网格视图。 gridview 中的一列具有所有行的下拉列表。更改任何 gridview 下拉框选择时是否可以触发方法?

我尝试在项目模板中为下拉列表添加 onselectedindexchange,但没有成功。

有什么想法吗?

<Gridview>
   <Columns>
      <asp:TemplateField>
         <ItemTemplate>
            <asp:DropDownList runat="server">
               <asp:ListItem Value="Yes">Yes</asp:ListItem>
               <asp:ListItem Value="No">No</asp:ListItem>
            </asp:DropDownList>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</GridView>

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以使用已更改的选定索引来假设这是您的网格内的下拉菜单

    <Gridview>
       <Columns>
          <asp:TemplateField>
             <ItemTemplate>
                 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                      onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                   <asp:ListItem>4</asp:ListItem>
                   <asp:ListItem>3</asp:ListItem>
                  </asp:DropDownList>
             </ItemTemplate>
          </asp:TemplateField>
       </Columns>
    </GridView>
    

    你可以有如下功能

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
         DropDownList ddl = (DropDownList)sender;
         GridViewRow row = (GridViewRow)ddl.Parent.Parent;
         int idx = row.RowIndex;
         // TextBox txtECustCode = (TextBox)row.Cells[0].FindControl("txtECustCode");
    

    【讨论】:

    • 也感谢阿迪尔。当我最初尝试时,我只是错过了自动回发。
    • 在我的代码中,我有两个连续的下拉列表,在页面加载第一个下拉列表中有项目,在索引更改后第二个下拉列表项目需要绑定。为您的回答 +1。
    【解决方案2】:

    您必须使用 GridView.RowCommand 事件而不是 On SelectedIndex Changed。还可以加AutoPostBack="true",需要的可以用CommandArguments传参数。

    void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
          DropDownList DropDownList1 = (DropDownList)row.FindControl("DropDownList1");
    }
    

    【讨论】:

    • 嗯,好的。 rowCommand 是在下拉列表还是网格视图中?我会试一试的。
    • 我无法将 CommandName 添加到下拉列表中。其他一切都很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2017-01-14
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    相关资源
    最近更新 更多