Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get a selected value of Dynamic Control which is inside placeholder

The below code is how i have created dynamic dropdown controls

C#
string strQuery = "select distinct(TXT_BRANCH_NAME) from TBL_BRANCH_MASTER;";
            DataTable dt_data = new DataTable();
            dt_data = objdBConnection.getDataTableQuery(strQuery);
            DropDownList ddlBranchList = new DropDownList();
            if (dt_data.Rows.Count > 0)
            {
                int rowCount = dt_data.Rows.Count;
                string[] assetList = new string[rowCount];
                ddlBranchList.ID = "ddlBranchList";
                ddlBranchList.Items.Add(new ListItem("Select", ""));
                ddlBranchList.CssClass = "form-control form-control-line";
                for (int j = 0; j < rowCount; j++)
                {
ddlBranchList.Items.Add(new ListItem(dt_data.Rows[j]["TXT_BRANCH_NAME"].ToString(), dt_data.Rows[j]["TXT_BRANCH_NAME"].ToString()));
                }
               
                ddlBranchList.DataBind();
            }


What I have tried:

Here Below i m fetching selected value of dropdown


C#
string ddlBranch = "";

DropDownList ddlist = ((DropDownList)plcBranchddl.FindControl("ddlBranchList"));

ddlBranch = ddlist.SelectedValue.ToString();



but at last i m getting a null in ddllist dropdown references
Posted
Updated 20-May-20 8:07am
v3

1 solution

You have to re-create your dynamic controls on postback

C#
protected void Page_Load(object sender, EventArgs e)
{
    DropDownList ddlBranchList = new DropDownList();
    ddlBranchList.ID = "ddlBranchList";
    ddlBranchList.Items.Add(new ListItem("Select", ""));
    ddlBranchList.CssClass = "form-control form-control-line";
    ddlBranchList.Items.Add(new ListItem("One", "1"));
    ddlBranchList.Items.Add(new ListItem("Two", "2"));
    ddlBranchList.Items.Add(new ListItem("Three", "3"));
    plcBranchddl.Controls.Add(ddlBranchList);
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string ddlBranch = "";

    DropDownList ddlist = ((DropDownList)plcBranchddl.FindControl("ddlBranchList"));

    ddlBranch = ddlist.SelectedValue.ToString();
}
 
Share this answer
 
Comments
Member 14838600 20-May-20 14:00pm    
i m fetching the selected in one method so that i can insert it into table after click the submit button.

but not working. i have did the same you do but not working

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