查找动态添加到页面的控件 [英] Find a control dynamically added to a page

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

问题描述

在内容页面中,我添加了一些链接按钮.链接按钮的数量取决于页面中的数据.数据不在网格或任何其他数据控件中.
我需要访问用户单击的链接按钮.
我使用了以下代码,

In a content page i have added some linkbuttons. the number of linkbuttons varies depending on the data in the page. data is not in grid or any other data-controls.
i need to access the linkbutton which user clicks.
i used the following code ,

int PageNumber = Convert.ToInt16(e.CommandArgument.ToString());
            string str = e.CommandName;
            LinkButton lbl = (LinkButton)FindControl(str);
            lbl.ForeColor = System.Drawing.Color.LightGreen;
            lbl.Font.Bold = true;


CommandName和id也相同.
已正确接收CommandName,但找不到控件.

请提出解决方案


CommandName and id is also same.
CommandName is correctly received but the control could not be found.

Please suggest a solution

推荐答案

您在哪里添加控件?到一个占位符?我在下面给出了一个大概的方法:

添加一个带有标签的占位符:

Where do you add the controls? to a place holder? I have given below a rough idea of how you could do this:

Add a place holder that will have the labels:

<form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="plcMain" runat="server">

        </asp:PlaceHolder>
        <asp:Label runat="server" ID="lblInfo"></asp:Label>
    </div>
    </form>



代码-后面:



Code - behind:

protected void Page_Load(object sender, EventArgs e)
{
    AddLinks();
}

private void AddLinks()
{
    for (int i=0;i<10;i++)
    {
        var linkButton = new LinkButton
                                    {
                                        ID = "lnkBtn" + i.ToString(),
                                        CommandArgument = i.ToString(),
                                        Text = "Link " + i.ToString()
                                    };
        linkButton.Click += new EventHandler(linkButton_Click);
        plcMain.Controls.Add(linkButton);
    }
}

protected void linkButton_Click(object sender, EventArgs e)
{
    var btn = sender as LinkButton;
    lblInfo.Text = "Link's command argument = " + btn.CommandArgument;
}



如果您注意到了,您甚至都不需要FindControl方法,只需linkButton_Click事件处理程序.如果您坚持使用FindControl方法,则可以执行



If you notice, you don''t even need the FindControl method, just the linkButton_Click event handler. If you insist on using the FindControl method, you can do something like

LinkButton control = plcMain.FindControl("lnkBtn1") as LinkButton; // Find "Link 1"
// Do something w/ "control"



在您的情况下,请使用页面上的FindControl 方法.如果您不想使用某个控件开始搜索,请尝试递归FindControl



In your case, you use the FindControl method on the page. Try a recursive FindControl if you don''t wish to start the search with a certain control http://weblogs.asp.net/palermo4/archive/2007/04/13/recursive-findcontrol-t.aspx[^]


控制.FindControl [
Control.FindControl[^] should do the trick for you. Dynamic controls should not matter.


ContentPlaceHolder contentPage = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
            LinkButton lnkBtn = contentPage.FindControl("LinkButtonId") as LinkButton;
     if (lnkBtn!= null)  
     {  
     		//Do some action
     }



这正常工作



This worked correctly


这篇关于查找动态添加到页面的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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