无法在asp.net中动态地添加多个服务器控件 [英] unable to add more than one server control dynamically in asp.net

查看:141
本文介绍了无法在asp.net中动态地添加多个服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我真的很粘起来,因为几天我的开发任务之一,我无法找出究竟怎么回事。请帮助。

我试图动态地添加行网页如下。
我想使用服务器控件。但我无法添加多行多。

即使我使用会话变量没有运气。请帮助请:)

----------------- aspx文件---------------

 < D​​IV>
    < ASP:表ID =TBL=服务器>
        < ASP:的TableRow ID =RW0>
            < ASP:TableCell的ID =C01WIDTH =100px的>
                < ASP:复选框=服务器ID =chk0/>
            < / ASP:TableCell的>
            < ASP:TableCell的ID =C02WIDTH =100px的>
                < ASP:文本框=服务器ID =txt0/>
            < / ASP:TableCell的>< / ASP:&的TableRow GT;
        < ASP:的TableRow ID =RW1>
            < ASP:TableCell的ID =C11WIDTH =100px的>
                < ASP:复选框ID =CHK1=服务器/>
            &所述; / ASP:TableCell的>&下; ASP:TableCell的ID =C12宽度=100像素>
                < ASP:文本框=服务器ID =TXT1/>
            < / ASP:TableCell的>< / ASP:&的TableRow GT;
    < / ASP:表>
    < ASP:按钮的ID =BTN1=服务器文本=添加行的OnClick =addRow/>
< / DIV>

--------------------- C#code后面-------------------- ----------

 保护无效addRow(对象发件人,EventArgs的发送)
    {        INT num_row = INT新(); //检查点
        num_row =(tbl.Rows).Count之间;        如果(会话[表]!= NULL)
        {
            TBL =(表)会议[表];
        }
        连续的TableRow =新的TableRow();
        TableCell的CELL1 =新的TableCell();
        TableCell的CELL 2 =新的TableCell();
        文本框TB =新的TextBox();
        复选框CB =新的复选框();        row.ID =RW+ num_row;        cell1.ID =C+ num_row +1;
        cell2.ID =C+ num_row +2;        tb.ID =TXT+ num_row;
        tb.EnableViewState = TRUE;
        cb.ID =CHK+ num_row;        cell1.Controls.Add(TB);
        cell2.Controls.Add(CB);        row.Cells.Add(CELL1);
        row.Cells.Add(CELL 2);        tbl.Rows.Add(行);
        会话[表] = TBL;
    }


解决方案

您能看到的只有一行添加每次因为,动态创建的控件将无法在回传可用。

当你点击添加行第一次回发发生,第三行加入。当你添加行第二次点击,已添加的行不会提供新的行被再次添加。最后,你将能够看到每次只有一排。

底线是您必须每次重新创建动态添加控件在Page_Load事件回发,原因是动态添加服务器sontrols DONOT在回传坚持

引用<一个href=\"http://stackoverflow.com/questions/10434350/why-dynamically-created-user-controls-disappear-when-controls-are-not-doing-full\">Why动态创建的用户控件消失的时候控制没有做满回发?还

要保持你生成控件的ID动态添加控件的视图状态。当你回发期间重新创建控件,用同样的ID创建它们使视图状态得以保持。

使用一些标准的逻辑来产生用于控制的id。希望这有助于。

更新:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            如果(的IsPostBack)
            {
                INT num_row =(int)的会议[No_of_Rows];
                的for(int i = 2; I&LT; num_row;我++)
                {
                    连续的TableRow =新的TableRow();
                    TableCell的CELL1 =新的TableCell();
                    TableCell的CELL 2 =新的TableCell();
                    文本框TB =新的TextBox();
                    复选框CB =新的复选框();                    row.ID =RW+ I;                    cell1.ID =C+ 1 +1;
                    cell2.ID =C+ 1 +2;                    tb.ID =TXT+我;
                    tb.EnableViewState = TRUE;
                    cb.ID =CHK+我;                    cell1.Controls.Add(CB);
                    cell2.Controls.Add(TB);                    row.Cells.Add(CELL1);
                    row.Cells.Add(CELL 2);                    tbl.Rows.Add(行);
                }
            }
            其他
            {
                会话[No_of_Rows] = 2;
            }
        }        保护无效addRow(对象发件人,EventArgs的发送)
        {
            INT num_row =(int)的会议[No_of_Rows] + 1;
            连续的TableRow =新的TableRow();
            TableCell的CELL1 =新的TableCell();
            TableCell的CELL 2 =新的TableCell();
            文本框TB =新的TextBox();
            复选框CB =新的复选框();            row.ID =RW+ num_row;            cell1.ID =C+ num_row +1;
            cell2.ID =C+ num_row +2;            tb.ID =TXT+ num_row;
            tb.EnableViewState = TRUE;
            cb.ID =CHK+ num_row;            cell1.Controls.Add(CB);
            cell2.Controls.Add(TB);            row.Cells.Add(CELL1);
            row.Cells.Add(CELL 2);            tbl.Rows.Add(行);
            会话[No_of_Rows] = tbl.Rows.Count;
        }

Hi I am really stuck up in one of my development tasks since couple of days and I am unable to find out the exactly whats going on. Please help.

I am trying to add rows dynamically to the webpage as follows. I want to use server control. But I am unable to add more than one row.

No luck even though I used session variable. Kindly help please :)

-----------------aspx file---------------

<div>
    <asp:Table ID="tbl" runat="server">
        <asp:TableRow ID="rw0">
            <asp:TableCell ID="c01" Width="100px">
                <asp:CheckBox runat="server" ID="chk0" />
            </asp:TableCell>
            <asp:TableCell ID="c02" Width="100px">
                <asp:TextBox runat="server" ID="txt0" />
            </asp:TableCell></asp:TableRow>
        <asp:TableRow ID="rw1">
            <asp:TableCell ID="c11" Width="100px">
                <asp:CheckBox ID="chk1" runat="server" />
            </asp:TableCell><asp:TableCell ID="c12" Width="100px">
                <asp:TextBox runat="server" ID="txt1" />
            </asp:TableCell></asp:TableRow>
    </asp:Table>
    <asp:Button ID="btn1" runat="server" Text="Add Row" OnClick="addRow" />
</div>

---------------------C# code behind------------------------------

protected void addRow(object sender, EventArgs e)
    {

        int num_row = new int(); //checkpoint
        num_row = (tbl.Rows).Count;

        if (Session["tables"] != null)
        {
            tbl =  (Table)Session["tables"];
        }


        TableRow row = new TableRow();
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        TextBox tb = new TextBox();
        CheckBox cb = new CheckBox();

        row.ID = "rw" + num_row;

        cell1.ID = "c" + num_row + "1";
        cell2.ID = "c" + num_row + "2";

        tb.ID = "txt" + num_row;
        tb.EnableViewState = true;
        cb.ID = "chk" + num_row;

        cell1.Controls.Add(tb);
        cell2.Controls.Add(cb);

        row.Cells.Add(cell1);
        row.Cells.Add(cell2);

        tbl.Rows.Add(row);
        Session["tables"] = tbl;


    }

解决方案

You are able to see only one row added everytime because, dynamically created controls will not be available across postbacks.

When you click on add row first time, a post back happens, a third row is added. when you click in add row second time, the already added row would not be available and a new row gets added again. Finally you will be able to see only one row everytime.

Bottom line is you have to recreate the dynamically added controls everytime on postback in the page_load event, the reason being dynamically added server sontrols donot persist across postbacks

refer Why dynamically created user controls disappear when controls are not doing full postbacks? also

To maintain the viewstate of dynamically added controls you have to generate ids for the controls. When you recreate the controls during postback, recreate them with the same ids so that view state is maintained.

Use some standard logic to generate the ids for the controls. hope this helps.

Update:

protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                int num_row = (int)Session["No_of_Rows"];
                for (int i = 2; i < num_row; i++)
                {
                    TableRow row = new TableRow();
                    TableCell cell1 = new TableCell();
                    TableCell cell2 = new TableCell();
                    TextBox tb = new TextBox();
                    CheckBox cb = new CheckBox();

                    row.ID = "rw" + i;

                    cell1.ID = "c" + i + "1";
                    cell2.ID = "c" + i + "2";

                    tb.ID = "txt" + i;
                    tb.EnableViewState = true;
                    cb.ID = "chk" + i;

                    cell1.Controls.Add(cb);
                    cell2.Controls.Add(tb);

                    row.Cells.Add(cell1);
                    row.Cells.Add(cell2);

                    tbl.Rows.Add(row);
                }
            }
            else
            {
                Session["No_of_Rows"] = 2;
            }
        }

        protected void addRow(object sender, EventArgs e)
        {
            int num_row = (int)Session["No_of_Rows"]+1;
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TextBox tb = new TextBox();
            CheckBox cb = new CheckBox();

            row.ID = "rw" + num_row;

            cell1.ID = "c" + num_row + "1";
            cell2.ID = "c" + num_row + "2";

            tb.ID = "txt" + num_row;
            tb.EnableViewState = true;
            cb.ID = "chk" + num_row;

            cell1.Controls.Add(cb);
            cell2.Controls.Add(tb);

            row.Cells.Add(cell1);
            row.Cells.Add(cell2);

            tbl.Rows.Add(row);
            Session["No_of_Rows"] = tbl.Rows.Count;
        }

这篇关于无法在asp.net中动态地添加多个服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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