如何在回发后从动态生成的控件中获取值? [英] how to get value from dynamic generated controls after postback ?

查看:106
本文介绍了如何在回发后从动态生成的控件中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,可以在click上生成3个控件。控件是Textbox,Dropdownlist和FileUpload控件。

当我第一次点击添加按钮时它对我来说很好但是当我填写所有三个控件,然后单击添加按钮,它生成回发,我在控件和控件中松开我填充的数据重新生成。



请给出这种类型的解决方案情况。

这里我的代码:



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.:

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

                                       OnClick="btnAddmoreDocs_Click" /> </td>





代码隐藏代码:



Code behind code :

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);
    }

推荐答案

在生成控件时添加标志并在加载时检查

例如: -

bool flag = false; //在页面加载之前添加甚至i,e在页面的全局级别

protected void btnAddmoreDocs_Click(object sender,EventArgs e)

{

if(flag = false)

{



AddUserControl( );

}



}



private void AddUserControl()

{

//你的代码

flag = true;

}
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;
}


这篇关于如何在回发后从动态生成的控件中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆