Search code examples
c#asp.netgridviewpageload

Avoid postback when getting dropdownlist value c# asp


I've dynamically created a dropdownlist within C# (No code in asp):

DropDownList myDDL = new DropDownList();

This can't be called from pageLoad() as it's not loaded everytime the page refreshes. However it's loaded everytime OnPostback(OnSelectedIndexChanged()) of another dll so I cant do !IsPostBack.


This dll is created when:

  • A value of another dropdownlist populates a GridView using OnSelectedIndexChanged()

    <asp:DropDownList id ="select1" name="assignGetForm"  runat="server" class="selectClass" AutoPostBack="True" OnSelectedIndexChanged="populateGridView">
    
  • On the GridView a function is run OnRowDatabound()(when the above OnIndexChanged() populates it) to populate myDDL inside the GridView

    <asp:GridView id="GridView1" name="GridView1" onrowdatabound="populateCellsDDL" runat="server"></asp:GridView>
    

I'm now trying to access the values in myDDL using a button onclick() event - however this always does postback and refreshes the page so myDDL dissapears and when I print the value to console it just gives my first value select activity and not the one i've actually selected.

How would I get these values when they are changed as this dll is not populated on pageLoad(). I've tried looking at AJAX however i'm unsure how to look at the c# value through this. I tried aswell viewstates but i dont have luck as again it doesnt populate on pageLoad().


Solution

  • I think, you need to rebind the grid in each postback, if you want to access values of dynamically created controls.

    I tried to simulate the situation with below sample code and I was able to access the value successfully.

    ASPX code

    <asp:DropDownList ID="select1" name="assignGetForm" runat="server" class="selectClass" AutoPostBack="True" />
    <asp:GridView ID="GridView1" name="GridView1" OnRowDataBound="GridView1_DataBound" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    

    C# code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            FillMainDll();
    
        if (!IsPostBack || GridView1.Rows.Count > 0)
            BindGrid();
    }
    
    private void FillMainDll()
    {
        select1.DataSource = new int[] { 1, 2, 3 };
        select1.DataBind();
    }
    
    private void BindGrid()
    {
        var dt = new DataTable();
    
        dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
    
        for (int i = 1; i < 5; i++)
        {
            DataRow dr = dt.NewRow();
    
            dr[0] = i;
            dr[1] = "Name - " + i.ToString();
    
            dt.Rows.Add(dr);
        }
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var myDDL = new DropDownList();
            myDDL.ID = "myDDL";
            myDDL.DataSource = GetGridRowDdlData();
            myDDL.DataBind();
            e.Row.Cells[1].Controls.Add(myDDL);
        }
    }
    
    private IEnumerable<string> GetGridRowDdlData()
    {
        var data = new List<string>();
    
        for (int i = 1; i < 4; i++)
        {
            data.Add("Name - " + i * int.Parse(select1.SelectedValue));
        }
    
        return data;
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        var sb = new System.Text.StringBuilder();
    
        foreach (GridViewRow row in GridView1.Rows)
        {
            var myDDL = row.FindControl("myDDL") as DropDownList;
            if (myDDL != null)
            {
                sb.AppendFormat("{0}<br/>", myDDL.SelectedValue);
            }
        }
    
        Response.Write(sb.ToString());
    }