Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 更改gridview templatefield itemtemplate中控件的值_Asp.net_Gridview - Fatal编程技术网

Asp.net 更改gridview templatefield itemtemplate中控件的值

Asp.net 更改gridview templatefield itemtemplate中控件的值,asp.net,gridview,Asp.net,Gridview,嗨,我有这样的gridview <asp:DropDownList ID="triggerDropDown" runat="server" AutoPostBack="true" onselectedindexchanged="triggerDropDown_SelectedIndexChanged"> <asp:GridView ID="myGridView" run="server"> <Columns> <asp:T

嗨,我有这样的gridview

<asp:DropDownList ID="triggerDropDown" runat="server" AutoPostBack="true" onselectedindexchanged="triggerDropDown_SelectedIndexChanged">

<asp:GridView ID="myGridView" run="server">
     <Columns>
          <asp:TemplateField HeaderText="Column 1">
               <ItemTemplate>
                   <asp:DropDownList ID="myDropDown1" runat="server" />
               </ItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="Column 2">
               <ItemTemplate>
                   <asp:DropDownList ID="myDropDown2" runat="server" />
               </ItemTemplate>
          </asp:TemplateField>
     </Columns>
</asp:GridView>

在事件方法中,您应该访问驻留在
GridView
的每一行中的
DropDownList
。这样,您可以将每个DropDownList绑定到所需的任何数据

此链接向您展示了如何执行此操作:

基本上:

  • 迭代GridView的每一行

  • 使用以下内容查找DropDownList控件:

    DropDownList mddl=GridView.Rows[2].FindControl(“myDropDown1”)

  • 将新数据绑定到mddl


    • gridview很可能不是您想要的。更改行中包含的控件值的方法通常是使用gridview的ItemDataBound事件中的e.Item.FindControl()获取所需控件的句柄

      这种方法的问题在于,您希望gridview(triggerDropDown)之外的控件与gridview的一行交互。您想为网格中的每个项目指定第一行、第一列、最后一行、第一列还是第一列?您最好将触发器下拉列表的目标放在gridview之外,并直接处理它


      如果您真的打算在网格中更改一行中的项目,请考虑在GRIDVIEW的ITEMDATABORKEY事件中这样做,您会发现很多示例。

      实际上,我可以使用<代码> GRIDVIEWLROW :)我只需要找到GRIDVIEW控件,得到它的<代码>行< /COD>属性,它是<代码> GRIDVIEW行< /代码>,现在我可以。每行做一次

      foreach (GridViewRow gridViewRow in (this.FindControl("myGridView") as GridView).Rows)
      {
          // I can see all elements of my row here as if I am traversing on GridViewEvents
      }
      

      实际上,事件不是gridview的一部分,而是gridview的外部。在我的例子中,gridview已经有界,gridview中的下拉列表也有界。如果外部下拉列表发生更改,我只想重新绑定gridview内部的下拉列表。@rob w:您的gridview中会有超过一行吗?这将决定您必须从中选择的潜在解决方案。您可以在gridview的ItemDataBound事件中检查e.rowindex属性,使用e.Item.FindControl引用所需行的所需控件,并直接按控件名称检查触发器dd的当前选定值,以执行您尝试执行的操作。
      foreach (GridViewRow gridViewRow in (this.FindControl("myGridView") as GridView).Rows)
      {
          // I can see all elements of my row here as if I am traversing on GridViewEvents
      }