在中继器内的ContentPlaceHolder中查找控件

本文关键字:查找 控件 ContentPlaceHolder 中继器 | 更新日期: 2023-09-27 18:29:23

我在访问位于ContentPlaceHolder中的中继器内的控件时遇到了一些问题。我以前没有母版页的方法工作得很好,但现在我包含了一个母版页,主要是因为命名,它抛出了很多东西。这是我以前在没有主页的情况下工作的代码:

LinkButton button = sender as LinkButton;
// Get index of LinkButton that was clicked
int repeaterItemIndex = ((RepeaterItem)button.NamingContainer).ItemIndex;
foreach (RepeaterItem myRepeater in rptrWebsites.Items)
{
    TextBox myText = myRepeater.FindControl("Web") as TextBox;
    var id = myText.ClientID;
    // Get just the number part of the ID
    id = id.Replace("rptrWebsites_Web_","");
    if (repeaterItemIndex.ToString() == id)
    {
        webName = myText.Text;
    }
}

我的ContentPlaceHolder的名称是ContentPlaceHolderBody。我可能可以使用jQuery找到一种方法来实现这一点,但我更愿意将其保留在Codebehin中。感谢您的帮助!

在中继器内的ContentPlaceHolder中查找控件

不确定,但也许这就是您想要的:

LinkButton button = sender as LinkButton;
// Get index of LinkButton that was clicked
int repeaterItemIndex = ((RepeaterItem)button.NamingContainer).ItemIndex;
foreach (RepeaterItem myRepeater in rptrWebsites.Items)
{
    if (repeaterItemIndex == myRepeater.ItemIndex)
    {
        TextBox myText = myRepeater.FindControl("Web") as TextBox;
        webName = myText.Text;
    }
}

无论如何,我建议你发布更多的代码,并解释你想要实现的目标。因为您的代码似乎适合ItemCommand模式。

编辑:如果按钮和文本框共享相同的命名容器,这也应该起作用:

LinkButton button = sender as LinkButton;   
TextBox myText = button.NamingContainer.FindControl("Web") as TextBox;
webName = myText.Text;

这看起来真的像ItemCommand