Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created dynamic controls on button click and I'm not able to retrieve values of dynamically created controls. I am getting values of dynamic controls inside a panel.

pnlDepartment is the Panel ID.



The strDDLValue has only first dropdown values and when it loops for the second time it still takes the first dropdown values and unable to get dynamic control values.

Please correct me if I'm making a mistake somewhere.


What I have tried:

Code:

protected void btnValues_Click(object sender, EventArgs e)
    {
            string strDDLValue = string.Empty;
            foreach (DropDownList ddl in pnlDepartment.Controls.OfType<DropDownList>())
            {
                strDDLValue = ddlName.SelectedItem.Text + "," + ddlLocation.SelectedItem.Text;
            }
    } 
Posted
Updated 25-Apr-18 22:48pm
Comments
Kornfeld Eliyahu Peter 8-Jan-18 3:36am    
1. Dynamically created controls must be re-created on each and every post-back (full or partial)
2. You provided us with far less information than needed!!!
ZurdoDev 8-Jan-18 8:14am    
Peter is correct. We need to see more of the relevant code.
Anuragintit 10-May-19 8:23am    
<asp:gridview id="GvList" runat="server" autogeneratecolumns="False" onrowdatabound="GvList_RowDataBound" showfooter="True">
<columns>

<asp:templatefield headertext="Measurement Center Id">
<itemtemplate>

<asp:label id="CenterId" runat="server" text="<%#Eval("CenterId") %>" enabled="false">


<headerstyle width="4%" horizontalalign="Center" cssclass="thead_cell">
<itemstyle horizontalalign="Center" width="4%">



<asp:templatefield headertext="Today Data">
<itemtemplate>

<asp:textbox id="txt_lastyearonDate_Rain" runat="server" text="<%#Eval("py_raintoday") %>" ondrop="return false;" onpaste="return false;"

autocomplete="off">


<controlstyle width="90%">
<headerstyle width="4%" horizontalalign="Center" cssclass="thead_cell">
<itemstyle horizontalalign="Center" width="4%">

<footertemplate>
<asp:label id="lbl_pytoday" runat="server" forecolor="Black" style="display:block; padding:0; text-align: center">



<asp:templatefield headertext="Select Type">
<itemtemplate>

<asp:dropdownlist id="ddl_type" runat="server">


<controlstyle width="100%">
<headerstyle width="3%" horizontalalign="Center" cssclass="thead_cell">
<itemstyle horizontalalign="Center" width="3%">




------------------------------------------------------------------------------------------------
Code Side

protected void GvList_DataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

TableHeaderCell cell = new TableHeaderCell();
cell.Text = "Details";
cell.ColumnSpan = 3;
row.Controls.Add(cell);


cell = new TableHeaderCell();
cell.Text = "Year 2018";
cell.ColumnSpan = 4;
row.Controls.Add(cell);

cell = new TableHeaderCell();
cell.ColumnSpan = 4;
cell.Text = "Year 2019";
row.Controls.Add(cell);

cell = new TableHeaderCell();
cell.Text = "Others";
cell.ColumnSpan = 4;
row.Controls.Add(cell);

// row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
GvList.HeaderRow.Parent.Controls.AddAt(0, row);
}

------------------------Submit click--------------------
foreach (GridViewRow row in GvList.Rows)

{
Label CenterId = (Label)row.FindControl("CenterId") as Label;
string CenterId_no = CenterId.Text;

DropDownList ddl = (DropDownList)row.FindControl("ddl_type") as DropDownList;
string dropdown_value = ddl.SelectedValue;

TextBox txt_lastyearonDate_Rain = (TextBox)row.FindControl("txt_lastyearonDate_Rain") as TextBox;

float py_ondte = float.Parse(txt_lastyearonDate_Rain.Text);

}

-----------------

1 solution

Dynamically created controls should be re-created on each and every postbacks to persist them on the page. Also, dynamic control values can be accessed through Request.Forms collection. Check this reference for example: [Creating Dynamic DropDownList Controls in ASP.Net]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900