Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET GridView EditTemplate and find control

In the GridView we are using an edit button. Once the edit button is clicked Controls in the edit template will display in the same row with update button. That row has two dropdownlist controls.

Process flow:

controls:d1 and d2

d1 is using sqldatasource for item display : working fine.

d2 is using codebehind code to load the item based on the selected value in the d1 : Not working

How to find the control in the edit template to display item value for d2?

like image 989
Geeth Avatar asked Jan 22 '23 19:01

Geeth


2 Answers

I got the answer.

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
      }
   }
 }
like image 162
Geeth Avatar answered Jan 26 '23 13:01

Geeth


When you switch to the edit mode, you need to rebind the grid for this to take effect.

So, you can use the 'RowDataBound' event.

  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.
    }
  }
like image 37
Meligy Avatar answered Jan 26 '23 13:01

Meligy