Search code examples
c#asp.netgridviewbindingdropdownlistfor

Dropdown list default value on button click


I'm working on an asp project. I have a situation where I need to search files and display in gridview. The gridview have three drop down list, my problem now is how can I show the default value of my dropdown based on the result set returned when I click the search button, since I already set the default value of the drop down ("Please Select") on data row bound. Because at first load my drop down should show "please select" value. Thanks a lot for the help. below is my code.

 protected void btnSearch_Click(object sender, EventArgs e)
{
    int uFlag = 0;
    string uploadFlag = this.ddlUploadDate.SelectedValue;
    string fileName = this.txtSearchText.Text;
    string uploadDt = this.txtDate.Text;
    string status = this.ddlStatus.SelectedValue.ToString();

    List<EventFile> fileSearch = new List<EventFile>();
    fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status);

    gvwAssociation.DataSource = fileSearch;
    gvwAssociation.DataBind();
}

 protected void gvwAssociation_RowDataBound(object sender, GridViewRowEventArgs e)
{
    ListItem Item = new ListItem();
    Item.Text = "Please Select";
    Item.Value = "0";
    Item.Selected = true;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddlpool = (DropDownList)e.Row.FindControl("ddlpool");
        DropDownList ddlyear = (DropDownList)e.Row.FindControl("ddlyear");
        DropDownList ddlevent = (DropDownList)e.Row.FindControl("ddlevent");

        ddlpool.DataSource = CoMailAssociationDAL.GetCoBindEvents();
        ddlpool.DataBind();
        ddlpool.Items.Insert(0, Item);

        ddlevent.DataSource = CoMailAssociationDAL.GetCoBindEvents();
        ddlevent.DataBind();
        ddlevent.Items.Insert(0, Item);

        for (int intCount = 2013; intCount <= 2020; intCount++)
        {
            ddlyear.Items.Add(intCount.ToString());   
            ddlyear.SelectedIndex = 1;             
        }
    }
}

Solution

  • You can do it by creating a condition in your grid RowDataBound event by taking one hiddenfield in itemtemplate for value from database and find that as

    for (int intCount = 2013; intCount <= 2020; intCount++)
        {
            ddlyear.Items.Add(intCount.ToString());   
            HiddenField result= GridView1.Rows[e.RowIndex].FindControl("hdnpool") as HiddenField;
            if(result!=null)
             ddlyear.SelectedIndex =result.value;
            else
             ddlyear.SelectedIndex = 1;             
        }
    

    In grid please use below for each dropdown:

      <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="ddpool" runat="server"></asp:DropDownList>
                            <asp:HiddenField ID="hdnpool" Value="<%# Eval("PoolColumninDB") %>" runat="server"></asp:HiddenField>
                        </ItemTemplate>
                    </asp:TemplateField>