如何让动态填充控件的值 [英] how to get the value of dynamically populated controls

查看:131
本文介绍了如何让动态填充控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div Conatining在aspx页面板

I have a div Conatining a Panel in aspx page

<div id="divNameofParticipants" runat="server">
    <asp:Panel ID="panelNameofParticipants" runat="server">
    </asp:Panel>
</div>

我是动态地从codebehind填充面板下面code:

I am populating the panel dynamically from codebehind with the following code:

void btnSubmitCountParticipant_Click(object sender, EventArgs e)
    {
        StringBuilder sbparticipantName=new StringBuilder();

        try
        {
            int numberofparticipants = Convert.ToInt32(drpNoofparticipants.SelectedValue);
            ViewState["numberofparticipants"] = numberofparticipants;
            Table tableparticipantName = new Table();
            int rowcount = 1;
            int columnCount = numberofparticipants;
            for (int i = 0; i < rowcount; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < columnCount; j++)
                {
                    TableCell cell = new TableCell();
                    TextBox txtNameofParticipant = new TextBox();
                    txtNameofParticipant.ID = "txtNameofParticipant" + Convert.ToString(i);
                    cell.ID = "cell" + Convert.ToString(i);
                    cell.Controls.Add(txtNameofParticipant);
                    row.Cells.Add(cell);


                }
                tableparticipantName.Rows.Add(row);
                panelNameofParticipants.Controls.Add(tableparticipantName);

            }

        }
        catch(Exception ex)
        {


        }
    }

现在我要访问在codebehind.for我我的code是因为在这些动态生成的文本框的值:

Now I want to access the value of these dynamically generated textbox in the codebehind.for which i my code is as under:

public void CreateControls()
    {

        try
        {
            //String test1 = test.Value;
            List<string> listParticipantName = new List<string>();
            if (ViewState["numberofparticipants"] != null)
            {
                int numberofparticipants = Convert.ToInt32(ViewState["numberofparticipants"]);
                for (int i = 0; i < numberofparticipants; i++)
                {
                    string findcontrol = "txtNameofParticipant" + i;
                    TextBox txtParticipantName = (TextBox)panelNameofParticipants.FindControl(findcontrol);
                    listParticipantName.Add(txtParticipantName.Text);

                }
            }
        }
        catch (Exception ex)
        {

        }
    }

但我不能够得到codebehind的值。

but I am not able to get the values in codebehind.

 TextBox txtParticipantName = (TextBox)panelNameofParticipants.FindControl(findcontrol);

以上code是无法找到控制其总是给空。什么我做错wrong.i重新在页面加载也控制,因为回发是无状态的,但仍然没有成功。

the above code is not able to find the control and its always giving null.what am i doing wrong.i recreated the controls in page load also since postback is stateless but still no success.

由于提前

推荐答案

您需要preINIT中的OnLoad创建动态控件没有。在这里看到的文档:<一href=\"https://msdn.microsoft.com/en-us/library/ms178472.aspx\">https://msdn.microsoft.com/en-us/library/ms178472.aspx

You need to create dynamic controls in PreInit not in OnLoad. See documentation here: https://msdn.microsoft.com/en-us/library/ms178472.aspx

RE /在页面加载创建控件将导致错误,因为视图状态绑定之前的OnLoad。碰巧不绑定的控件的ViewState

Re/Creating the controls on page load will cause the ViewState not to be bound to the controls because viewstate binding happens before OnLoad.

这篇关于如何让动态填充控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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