How to make DropDownList automatically be selected based on the Label.Text

Posted by Archana B.R on Stack Overflow See other posts from Stack Overflow or by Archana B.R
Published on 2012-10-12T15:28:38Z Indexed on 2012/10/12 15:37 UTC
Read the original article Hit count: 216

Filed under:
|
|

I have a DropDownlist in the GridView, which should be visible only when edit is clicked. I have bound the DropDownList from code behind. When I click on Edit, the label value of that cell should automatically get selected in the DropDownList.

The code I have tried is:

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SqlCommand cmd = new SqlCommand("SELECT Location_Name FROM Location_Table");
            DropDownList bind_drop = (e.Row.FindControl("DropList") as DropDownList);
            bind_drop.DataSource = this.ExecuteQuery(cmd, "SELECT");
            bind_drop.DataTextField = "Location_Name";
            bind_drop.DataValueField = "Location_Name";
            bind_drop.DataBind();

            string Loc_type = (e.Row.FindControl("id2") as Label).Text.Trim();               
            bind_drop.Items.FindByValue(Loc_type).Selected = true;
        }
    }

When I run the code, it gives an exception error Object reference not set in the last line of the above code. Cannot find out whats wrong. Kindly help

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET