daniloquio
daniloquio

Reputation: 3902

How to get full ClientID for a control from server side code

I have a TextBox inside ItemTemplate of a Repeater that is also inside a ItemTemplate.

Server side I have:

protected void txtValorMaterialAsign_TextChanged(object sender, EventArgs e)
{
    string controlId = ((TextBox)sender).ClientID;
}

which results in:

rptFilasDeUnMaterial_ctl04_txtValorMaterialAsign

But client side the Id for that control is (according to Chrome's F12):

ctl00_contenido_wucMateriales_rptMateriales_ctl00_rptFilasDeUnMaterial_ctl01_txtValorMaterialAsign

How can I get the full Id from server side code?

Upvotes: 1

Views: 13040

Answers (3)

bugnuker
bugnuker

Reputation: 3968

This worked for me:

var myResult1 = FindControl("ctl00").FindControl("MainContent")
                                    .FindControl("text1").UniqueID;

returns:

ctl00$MainContent$text1

Try UniqueID on your simple findControl. If that does not work, nest the findControls.

Upvotes: 3

MethodMan
MethodMan

Reputation: 18843

you can try something like this ..

To find a control in the header:

textcontrol = repeater1.Controls[0].Controls[0].FindControl("txtValorMaterialAsign");

To find a control in the footer:

textcontrol = repeater1.Controls[repeater1.Controls.Count - 1].Controls[0].FindControl("txtValorMaterialAsign");

With extension methods

public static class RepeaterExtensionMethods
{
    public static Control FindControlInHeader(this Repeater repeater, string controlName)
    {
        return repeater.Controls[0].Controls[0].FindControl(controlName);
    }

    public static Control FindControlInFooter(this Repeater repeater, string controlName)
    {
        return repeater.Controls[repeater.Controls.Count - 1].Controls[0].FindControl(controlName);
    }
}

Upvotes: 1

Hanlet Escaño
Hanlet Escaño

Reputation: 17380

Maybe this gives you an idea:

HTML:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <asp:Repeater ID="repPeople" runat="server">
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtName" OnDataBinding="text_databinding" Text='<%# Eval("Name")%>'></asp:TextBox>
        </ItemTemplate>
  </asp:Repeater>
</asp:Content>

Code Behind:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Person> tmp = new List<Person>();
        tmp.Add(new Person() { LastName = "Escano", Name = "Hanlet" });
        tmp.Add(new Person() { LastName = "Escano", Name = "Hanlet" });
        tmp.Add(new Person() { LastName = "Escano", Name = "Hanlet" });
        tmp.Add(new Person() { LastName = "Escano", Name = "Hanlet" });
        tmp.Add(new Person() { LastName = "Escano", Name = "Hanlet" });
        this.repPeople.DataSource = tmp;
        this.repPeople.DataBind();
    }
    public void text_databinding(object sender, EventArgs e)
    { 
        Response.Write(((TextBox)sender).ClientID + "<br />") ;
    }
}

public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
}

Output:

MainContent_repPeople_txtName_0
MainContent_repPeople_txtName_1
MainContent_repPeople_txtName_2
MainContent_repPeople_txtName_3
MainContent_repPeople_txtName_4

So pretty much use the OnDataBinding of your textboxes, and then cast the sender to TextBox, you won't even need to use FindControl().

Upvotes: 2

Related Questions