简体   繁体   中英

Find control inside ContentPlaceHolder inside a repeater

I'm having some trouble being able to access a control inside my repeater which is found in the ContentPlaceHolder. My previous method without the master page was working fine but now that I included a master page it threw a lot of things off mainly because of the naming. Here is the code that I previously had that worked without the master page:

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;
    }
}

The name of my ContentPlaceHolder is ContentPlaceHolderBody. I can probably find a way to do this using jQuery but I would prefer to keep this in the Codebehind. Any help is appreciated!

Not sure, but maybe this is what your looking for :

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;
    }
}

Anyway, I suggest you post more code and explain what you want to achieve. As your code seems fit for the ItemCommand pattern.

Edit : if the Button and the TextBox share the same naming container, this should also work :

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

This really seems like ItemCommand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM