Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a button which generates 3 controls on click.the controls are Textbox,Dropdownlist and a FileUpload control.
WHen i click Add button first time it works fine for me but when I filled all the three controls and click on Add button, it generates postback and i loos my filled data in controls and controls are regenerated.

Please give a solution for this type of situation.
Here i smy code.:

XML
<td>
<asp:Button ID="btnAddmoreDocs" runat="server" CssClass="Button" Text="Add More"
                                       OnClick="btnAddmoreDocs_Click" /> </td>


Code behind code :
C#
protected void btnAddmoreDocs_Click(object sender, EventArgs e)
    {
        //if (Convert.ToString(ViewState["Generated"]) != "true")
        {
            AddUserControl();
           
        }
    }

private void AddUserControl()
    {
        Table tbldynamic = new Table();
        int i=1;
        if (ViewState["i"] == "0" || ViewState["i"] == "" || ViewState["i"] == null)
        {
            ViewState["i"] = 1;

        }
        else
        {
            i = Convert.ToInt16(ViewState["i"]) + 1;
        }
         for (int j = 1; j <= i; j++)
        {
            TableCell tc = new TableCell();
            TableCell tclabel = new TableCell();
            TableCell tclabelDocName = new TableCell();
            TableCell tcTextDocName = new TableCell();
            TableCell tcFileUpld = new TableCell();
            TableRow tr = new TableRow();
            //CheckBox _checkbox = new CheckBox();
            Label LbelType = new Label();
            LbelType.ID = "lbltype" + j;
            LbelType.Text = "Document Type :";

            Label lblDocName = new Label();
            lblDocName.ID = "lblDocName" + j;// ViewState["i"];
            lblDocName.Text = "Document Name :";

            DropDownList ddltype = new DropDownList();
            ddltype.ID = "ddlDocumntType" + j;// ViewState["i"];
            ddltype.Text = "Document Type";
             
            TextBox txtDocName = new TextBox();
            txtDocName.ID = "txtDocnm" +j;
            Session[txtDocName.ID] = txtDocName;
            FileUpload FileUpd = new FileUpload();
            FileUpd.ID = "filUpldDoc" + j;// ViewState["i"];
            // _checkbox.ID = "chkDynamicCheckBox" + i;
            //_checkbox.Text = "Checkbox" + i;
            tc.Controls.Add(ddltype);
            tclabel.Controls.Add(LbelType);
            tclabelDocName.Controls.Add(lblDocName);
            tcTextDocName.Controls.Add(txtDocName);
            tcFileUpld.Controls.Add(FileUpd);
            tr.Cells.AddAt(0, tclabel);

            tc.Width = 100;
            tr.Cells.AddAt(1, tc);
            tr.Cells.AddAt(2, tclabelDocName);
            string textboxID = "txtDocnm" + j;
            TextBox checkIfTextBox = (TextBox)Session[textboxID];
            if (checkIfTextBox != null)
            {
                txtDocName.Text = checkIfTextBox.Text;
            }
            //if (string.IsNullOrEmpty(Request.Form["txtDocnm" + j.ToString()]))
            //{
            //    txtDocName.Text = Request.Form["txtDocnm" + j.ToString()];
            //}
            tr.Cells.AddAt(3, tcTextDocName);
            tr.Cells.AddAt(4, tcFileUpld);
            tbldynamic.Rows.Add(tr);
            ViewState["i"] = j;// (Convert.ToInt16(ViewState["i"]) + 1).ToString();
        }
        pnlStudDocumnts.Controls.Add(tbldynamic);
    }
Posted

1 solution

add a flag when your controls are generated and check it on load
eg:-
bool flag=false;//add before page load even i,e at global level of your page
protected void btnAddmoreDocs_Click(object sender, EventArgs e)
{
if(flag=false)
{

AddUserControl();
}

}

private void AddUserControl()
{
// your code
flag=true;
}
 
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