How to set SelectedValue of DropDownList in GridView EditTemplate

后端 未结 5 1030
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 20:36

I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.

I tried to use AppendDataBou

5条回答
  •  暖寄归人
    2021-01-04 20:56

    The use of the GridView_DataBound event handler solves the problem.

    In your case you will need to add a HiddenField to store the PK_DepartmentId value:

    
      
        
          
            
            
            
            
            
          
          
            
            
          
        
        
      
    
    
    protected void gvExample_DataBound(object sender, EventArgs e)
    {
      foreach (GridViewRow gvRow in gvExample.Rows)
      {
        DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList;
        HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField;
    
        if (ddlDepartment != null && hfDepartmentId != null)
        {
          ddlDepartment.SelectedValue = hfDepartmentId.Value;
        }
      }
    }
    

提交回复
热议问题