Need help with FindControl function
I am playing with a form using a relay that I was working on and I am currently trying to write an event handler that will get the value of certain controls inside the relay after the form that contains the relay. I assumed that something similar to this would work if I wanted to extract a label from the first repeater element and then place its text within the text of another label:
protected void SubmitMessage_Click(object sender, EventArgs e)
{
Label MyLabel = (Label)MyRepeater.Items[0].FindControl("MessageID");
MyLabel2.Text = MyLabel.Text;
}
Using this, it looks like MyLabel.Text is empty. In testing, if MyLabel is null after setting it, the result will be false, so I'm trying to figure out why MyLabel didn't have the same content as the relay element I'm trying to set. MyLabel2 is already declared at the top of the class. Thanks in advance.
EDIT: The label text in the .aspx file is not a database binding inside the text attribute, but rather in the container of the label itself. I'll try another method on Tuesday morning as soon as I can get back into my code, as suggested by Benjamin.
a source to share
It boils down to how you bind data to your tag on a repeater.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"><%# DataBinder.Eval(Container.DataItem,"CustomerName") %></asp:Label>
<asp:Label ID="Label2" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"Country") %>></asp:Label>
</ItemTemplate>
</asp:Repeater>
Label1 has no Text attribute set, and data is bound client-side to the control in the label tag. is written to the span tag after the Label is rendered on the page and never directly bound to the label control. This only happens when you use a script line in the label body, if it was just text, the label body will always overwrite the value of the label text attribute.
For Label2, the Text attribute is set , so the binding happens on the server , causing it to evaluate the inline script before it finishes rendering the label and so your other controls are available.
Without actually looking at your repeater, I suspect this is how your data is related.
Here are some test codes to prove this case to you.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Customer");
dt.Columns.Add("CustomerName", Type.GetType("System.String"));
dt.Columns.Add("Country", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr[0] = "Testcustomer1";
dr[1] = "USA";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Testcustomer2";
dr[1] = "UK";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Testcustomer3";
dr[1] = "GERMANY";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Testcustomer4";
dr[1] = "FRANCE";
dt.Rows.Add(dr);
//Bind the data to the Repeater
Repeater1.DataSource = ds;
Repeater1.DataMember = "Customer";
Repeater1.DataBind();
}
}
protected void SubmitMessage_Click(object sender, EventArgs e)
{
Label MyLabel = (Label)Repeater1.Items[0].FindControl("Label1");
Label MyLabel2 = (Label)Repeater1.Items[0].FindControl("Label2");
//Empty String Text was never set server side
Label3.Text = MyLabel.Text;
//String is found because Text was set
Label4.Text = MyLabel2.Text;
}
plug it in and add some extra shortcuts and you can see Label4 has the data you are looking for while Label3 is empty.
a source to share