How to set SelectedValue of DropDownList in GridView EditTemplate

后端 未结 5 1015
被撕碎了的回忆
被撕碎了的回忆 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 21:02

    Why are you guys suggesting to use loops, when there is a GridView method specifically made for when a row's condition changes - the RowDataBound()?

    protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                GridViewRow gvRow = (GridViewRow)e.Row;
                HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID");
                if (hfAgentID != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent");
                        ddlAgent.SelectedValue = hfAgentID.Value;
                    }
                }
            }
    

提交回复
热议问题