加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – FindControl()返回null

发布时间:2020-12-15 22:33:53 所属栏目:asp.Net 来源:网络整理
导读:我试图创建应用程序whad动态添加控件.我有主页,我的asp:内容在这里: asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"asp:ScriptManager ID="scriptManager1" runat="server"/asp:ScriptManager div style="margin: 10px" as
我试图创建应用程序whad动态添加控件.我有主页,我的asp:内容在这里:
<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.wordpress.com/2010/01/30/dynamically-add-controls-in-asp-net-c/那样做

这是我的代码:

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    

    }

当函数GetPostBackControl()在loap foreach中寻找我的btnAdd时,例如在ctr“ctl00 $MainContent $scriptManager1”的第一次迭代中,myControl为null …在下一次迭代中也…所以我的函数总是返回null.可能是什么原因?

解决方法

FindControl仅搜索容器的直接子项.由于您是在页面级别开始的,因此您需要通过子UpdatePanel控件来递归到您的btnAdd控件.

看看here为例如何做到这一点.

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

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

(或在代码中,btnAdd.OnClick = new EventHandler(btnAdd_Click);)

即使您的表单中有多个动态添加的按钮,您也可以将它们连接到同一个按钮单击处理程序,在这种情况下,发送方将包含单击的按钮控件.您通常会使用FindControl从动态添加的输入控件(文本框等)中删除数据,而不是查看哪个控件导致回发(在适当的事件处理程序中作为’sender’会更容易)

编辑2:
您可以像其他控件一样动态添加按钮

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

如果您希望已经添加的所有控件都“保留”在回发之间,那么在页面和控件上启用viewstate,然后确保在OnInit中只添加一次没有回发的控件:

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

您可以将’mycount’的状态保存在隐藏字段中(在同一个updatepanel中,并启用viewstate) – 您每次都需要将其解析为int.或者您可以使用SessionState来跟踪它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读