Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find the control by its id. I have a drop down list and i have its id also. I use its id to print the control name. But the code prints : Control not found.

This is the below code:



Control c = new Control();
Control fc = c.FindControl("ddl1");
if (fc != null)
{
Control c2 = fc.Parent;
Response.Write("parent of the textbox is :" + c2.ID);

}
else
{
Response.Write("control not found");

}
Posted

if you want to find a control within the same page, you can use like this
C#
Control fc = FindControl("ddl1");
if (fc != null)
{
Control c2 = fc.Parent;
Response.Write("parent of the textbox is :" + c2.ID);
 
}
else
{
Response.Write("control not found");
 
} 
 
Share this answer
 
Hi,

you can get control this way.

private void Button1_Click(object sender, EventArgs MyEventArgs)
{
// Find control on page.
Control myControl1 = FindControl("TextBox2");
if(myControl1!=null)
{
// Get control's parent.
Control myControl2 = myControl1.Parent;
Response.Write("Parent of the text box is : " + myControl2.ID);
}
else
{
Response.Write("Control not found");
}
}

http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol(v=vs.71).aspx[^]
 
Share this answer
 
private void btnsave_Click(object sender, EventArgs MyEventArgs)
{
Control myControl1 = FindControl("ddl1");
if(myControl1!=null)
{
Control myControl2 = myControl1.Parent;
Response.Write("Parent of the text box is : " + myControl2.ID);
}
else
{
Response.Write("Control not found");
}
}
 
Share this answer
 
v2
Put the dropdown control inside a panel, say with ID "pnl".
XML
<asp:panel id="pnl" runat="server">
  <asp:DropDownList ID="ddl1" runat="server" />
</asp:panel>

Then use below code:
C#
Control ctrl = pnl.FindControl("ddl1");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900