我在访问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,但我更喜欢将其保留在Codebehind中。任何帮助表示赞赏!
答案 0 :(得分:0)
不确定,但也许这就是你要找的东西:
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
模式。
编辑:如果Button和TextBox共享相同的命名容器,这也应该有效:
LinkButton button = sender as LinkButton;
TextBox myText = button.NamingContainer.FindControl("Web") as TextBox;
webName = myText.Text;
这看起来像是ItemCommand