locked
DropDownList Selected Value RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code . In Label it shows No . When Dropdown appears by default No should be selected since it has Value No in Database. It just displays List

    protected void gvwBfbStructure_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlIS = (e.Row.FindControl("ddlISg") as DropDownList);
                    if (ddlIS != null)
                    {
                        ddlIS.DataSource = new List<string>() { "Yes", "No" };
                        ddlIS.DataBind();
                        ((DropDownList)e.Row.FindControl("ddlIS")).SelectedValue =
                            DataBinder.Eval(e.Row.DataItem, "IS").ToString();
                    }
                }
    
    
    <asp:TemplateField HeaderText = "IS">
                                    <ItemTemplate>
                                        <asp:Label ID="lblIS" runat="server" Text='<%# Convert.ToString(Eval("IS")) == "Y" ? "Yes" : "No" %>' CssClass="mytxtWidth30"></asp:Label>
                                        <asp:DropDownList ID="ddlIS" Visible = "false" runat="server"> </asp:DropDownList> 
                                    </ItemTemplate>
                                </asp:TemplateField>

    Thanks

    Sunday, August 26, 2018 1:03 PM

Answers

  • User2103319870 posted

    You have a tempalte column instead of BoundColumn, so you need to assign the value from controls inside template column. First find the label control and then set the selected value in dropdownlist

    protected void gvwBfbStructure_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlIS = (e.Row.FindControl("ddlISg") as DropDownList);
                    Label lbl = (e.Row.FindControl("lblIS") as Label);
                    if (ddlIS != null)
                    {
                        ddlIS.DataSource = new List<string>() { "Yes", "No" };
                        ddlIS.DataBind();
                        //Set the selected value from dropdownlist
                        ((DropDownList)e.Row.FindControl("ddlIS")).SelectedValue = lbl.Text;
                    }
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 26, 2018 1:34 PM