【问题标题】:ASP.NET GridView EditTemplate and find controlASP.NET GridView EditTemplate 和查找控件
【发布时间】:2010-01-06 05:59:51
【问题描述】:

在 GridView 中,我们使用了一个编辑按钮。单击编辑按钮后,编辑模板中的控件将与更新按钮显示在同一行中。该行有两个下拉列表控件。

工艺流程:

控件:d1 和 d2

d1 正在使用 sqldatasource 进行项目显示:工作正常。

d2 正在使用代码隐藏代码根据 d1 中的选定值加载项目:不工作

如何在编辑模板中找到控件来显示d2的item值?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我得到了答案。

    protected void GridView1_PreRender(object sender, EventArgs e)
     {
      if (this.GridView1.EditIndex != -1)
       {
         Button b = GridView1.Rows[GridView1.EditIndex].FindControl("Button1") as Button;
         if (b != null)
          {
          //do something
          }
       }
     }
    

    【讨论】:

      【解决方案2】:

      切换到编辑模式时,需要重新绑定网格才能生效。

      因此,您可以使用“RowDataBound”事件。

        void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
          if(e.Row.RowType == DataControlRowType.DataRow
                  && e.Row.RowIndex == MyGridView.EditIndex)
          {
            DropDownList d1 = e.Row.FindControl("d1") as DropDownList;
            if(d1 == null) return;
            //Now you have the drop down. Use it as you wish.
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        相关资源
        最近更新 更多