繁体   English   中英

更改下拉列表选择时的GridView

[英]GridView on change of dropdown list selection

所以我有一个gridview。 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>

您可以使用更改的选定索引来假设这是网格内部的下拉列表

<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");

您必须使用GridView.RowCommand事件而不是On SelectedIndex Changed。 还可以添加AutoPostBack =“ true” ,如果需要,可以使用CommandArguments传递论据。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM