C#, Findcontrol

Using FindControl() to find control

From within the masterpage:

var literal = (Literal)FindControl("MainLinks").FindControl("litNavLinks");
literal.Text = sb.ToString();

From within the view:

litNavLinks.Text = sb.ToString();

C# Find control, casting, elegant code

You can create a method using generic type parameter and the caller can specify the type of Control expecting back like:

protected TControl Returncontrol<TControl>(GridViewRow gvr, String ControlName) 
where TControl : Control
{
TControl control = gvr.FindControl(ControlName) as TControl;

return control;
}

Now you will be using it like:

TextBox txtBox = ReturnControl<TextBox>(grid1,"TextBox1");

and now you can access the properties and methods available on TextBox type :

if(txtBox!=null)
txtBox.Text ="Something";

You can also create an extension method on the GridViewRow type for this as an option like:

public static class GridViewRowExtensions
{
public static TControl Returncontrol<TControl>(this GridViewRow gvr, String ControlName) where TControl : Control
{
TControl control = gvr.FindControl(ControlName) as TControl;

return control;
}
}

and now you can directly call it using the instance of GridViewRow :

TextBox txtBox = gvr.ReturnControl<TextBox>("TextBox1");

if(txtBox!=null)
txtBox.Text="Some Text";

Hope it gives you idea on how to achieve what you are looking for.

C#, FindControl

Courtesy of Mr. Atwood himself, here's a recursive version of the method. I would also recommend testing for null on the control and I included how you can change the code to do that as well.

protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
Label Label1 = FindControlRecursive(Page, "Label1") as Label;
if(Label1 != null)
Label1.Text = "<b>The text you entered was: " + TextBox1.Text + ".</b>";
}
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label Label1 = FindControlRecursive(Page, "Label1") as Label;
if (Label1 != null)
Label1.Text = "<b>You chose <u>" + DropDownList1.SelectedValue + "</u> from the dropdown menu.</b>";
}

private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id) return root;
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null) return t;
}
return null;
}

C# Find Control

Unfortunately, FindControl doesn't find nested controls. From MSDN:

This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.

Example:

<asp:Panel ID="pnl" runat="server">
<asp:Label ID="lbl" runat="server" Text="I'm here!" />
</asp:Panel>

In the previous exampleif you look for the Label, FindControl won't find it. Instead, if you look for the Panel it will find it.

More info here:

Control.FindControl Method (String)

find control in page

To find the button on your content page you have to search for the ContentPlaceHolder1 control first.
Then use the FindControl function on the ContentPlaceHolder1 control to search for your button:

 ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
Response.Write(((Button)cph.FindControl("a")).Text);

FindControl for finding control on page that uses master page

You can't access content, please refer to this question.

Perhaps, you wanted to add "runat=server" to div and access ImageControls.

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="content">
<div class="mlists" runat="server" id="div">
<a href="?"><img id="i1" src="../img/1.png" runat="server" alt="1"/></a>
<a href="?"><img id="i2" src="../img/2.png" runat="server" alt="2"/></a>
<a href="?"><img id="i3" src="../img/3.png" runat="server" alt="3"/></a>

</div>
</asp:Content>

Then access controls in code behind like this:

var control = div.FindControl("i2");
((HtmlImage)control).Src = "../img/x.png";

C# 'FindControl' does not exist in the current context

As @Equalsk had said all I needed to use Controls.Find(...); I was simply using the wrong thing. Figured I should post this as an answer to ensure it stands out, as while it may have been a very simple thing to do, I (like others may be) was completely unaware of this feature.

`
static TextBox[] Values;

    public static TextBox[] TextBoxes(string query, Form Current)
{
String[] Vars = query.Split('@');

String[] NewVars = new String[Vars.Length - 1];
Values = new TextBox[Vars.Length - 1];

for (int x = 0; x < Vars.Length - 1; x++)
{
NewVars[x] = Vars[x + 1].Remove((Vars[x + 1].Length - 1));
Values[x] = Current.Controls.Find(NewVars[x] + "TextBox", true).FirstOrDefault() as TextBox;
}

return Values;
}

`

This is the updated code. The "Form Current" is simply to allow me to use this for any of my forms as I have it situated in a global class.

FindControl doesn't find control

You cannot find a BoundField since it's not a real control. You have to use row.Cells[index].Text:

outputString.Add(row.Cells[1].Text);


Related Topics



Leave a reply



Submit