的FindControl()返回NULL [英] FindControl() return null

查看:245
本文介绍了的FindControl()返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建应用程序whad dynamicaly添加controlls。我有母版,我的ASP:内容是在这里:

I trying to create application whad add controlls dynamicaly. I have masterpage, my asp:Content is here:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server">
</asp:ScriptManager>   
<div style="margin: 10px">
    <asp:UpdatePanel ID="updatePanel1" runat="server">
        <ContentTemplate>
            <asp:PlaceHolder runat="server" ID="myPlaceHolder" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
</div>
<asp:Button ID="btnAdd" runat="server" Text="Add" />

点击在btnAdd后,我想补充两个文本框。我试图做到这一点像的http://jagdeepmankotia.word$p$pss.com/2010/01/30/dynamically-add-controls-in-asp-net-c/

After click in btnAdd I want to add two textboxes. I trying do it like in http://jagdeepmankotia.wordpress.com/2010/01/30/dynamically-add-controls-in-asp-net-c/

这是我的code:

    static int myCount = 1;
    private TextBox[] color;
    private TextBox[] text;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        color = new TextBox[myCount];
        text = new TextBox[myCount];

        for (int i = 0; i < myCount; i++)
        {
            TextBox tbColor = new TextBox();
            tbColor.ID = "colorTextBox" + i.ToString();
            myPlaceHolder.Controls.Add(tbColor);
            color[i] = tbColor;

            TextBox tbText = new TextBox();
            tbText.ID = "textTextBox" + i.ToString();
            myPlaceHolder.Controls.Add(tbText);
            text[i] = tbText;

            LiteralControl literalBreak = new LiteralControl("<br />");
            myPlaceHolder.Controls.Add(literalBreak);
        }
    }


    public Control GetPostBackControl(Page page)
    {
        Control control = null;
        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control mycontrol = page.FindControl(ctl);
                if (mycontrol is System.Web.UI.WebControls.Button)
                {
                    control = mycontrol;
                    // This gives you ID of which button caused postback                        
                    break;
                }
            }
        }
        return control;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if (myControl != null)
            if (myControl.ClientID.ToString() == "btnAdd")
                myCount = myCount + 1;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //handled in PreInit    

    }

当在loap的foreach功能GetPostBackControl()找我btnAdd,例如在点击率第一次迭代ctl00 $ $日程地址搜索Maincontent scriptManager1myControl为空...在下迭代也......所以,我的函数总是返回空值。有什么可以什么原因呢?

When in function GetPostBackControl() in loap foreach looking for my btnAdd, for example in first iteration for ctr "ctl00$MainContent$scriptManager1", myControl is null... In next iterations also... So my function always return null. What can be reason?

推荐答案

的FindControl 只搜索容器的直接子。既然你是在页面级出发,则需要通过子的UpdatePanel 控制递归去你的 btnAdd 控制。

FindControl only searches direct children of the container. Since you are starting off at the page level, you will need to recurse through the child UpdatePanel control to get to your btnAdd control.

这里看一看为例如何不做到这一点。

Have a look here for an example how to do do this.

编辑:
我不知道我理解你为什么你以这种方式按钮,寻找,因为只有一个在屏幕上的静态按钮 - 你不会需要使用的FindControl 在这种情况下。

<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />

(或code, btnAdd.OnClick + =新的EventHandler(btnAdd_Click);

就算你在表单多个按钮动态添加,你可以连接所有他们到同一个按钮点击处理程序,在这种情况下,发件人随后将包含这是点击的按钮控制。您通常会使用的FindControl凑出来的动态添加输入控件(文本框等)的数据,而不是看到哪个控件导致回传(如在适当的事件处理程序发件人会更容易些)

Even if you had multiple Buttons in your form added dynamically, you could wire ALL of them up to the same Button Click handler, in which case sender would then contain the Button Control which was clicked. You would typically use FindControl to scrape the data out of the dynamically added Input controls (text box etc), rather than to see which control caused the Postback (as 'sender' in an appropriate event handler would be easier)

编辑2:
您可以动态地添加按钮就像你的其他控件

Edit 2: You can add the buttons dynamically just like your other controls

    Button myButton = new Button();
    myButton.Text = "Click Me";
    myButton.Click += new EventHandler(btnAdd_Click);
    myPlaceHolder.Controls.Add(myButton); 

如果你想,你已经在两者之间的回传后,页面上和控制使视图状态,然后确保你只有一次添加控件没有回传,在的OnInit已添加到'留'的所有控制:

If you want all the controls that you've added already to 'stay' in between postbacks then enable viewstate on the page and on the controls, and then make sure that you only add the controls once without postback, in OnInit:

   base.OnInit(e);    
   if (!IsPostBack)
   { // ... Add controls here

您可以保留mycount的'的状态在一个隐藏字段(在相同的UpdatePanel,与视图状态启用) - 你需要每次它解析为int。或者你可以使用SessionState会跟踪它。

You can keep the state of 'mycount' in a hidden field (in the same updatepanel, and with viewstate enabled) - you'll need to parse it to an int each time. Or you can use SessionState to track it.

这篇关于的FindControl()返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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