使用id在asp.net中通过FindControl查找控件名称 [英] Using the id to find the control name by FindControl in asp.net

查看:192
本文介绍了使用id在asp.net中通过FindControl查找控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过它的id找到控件。我有一个下拉列表,我也有它的ID。我使用它的id来打印控件名称。但代码打印:未找到控件。



这是下面代码:







控制c =新控制();

控制fc = c.FindControl(ddl1);

if(fc!= null)

{

控制c2 = fc.Parent;

Response.Write(文本框的父级是:+ c2.ID);



}

else

{

Response.Write(找不到控件) ;



}

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");

}

推荐答案

如果你想在同一页面内找到一个控件,你可以使用像这样

if you want to find a control within the same page, you can use like this
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");
 
} 






你可以控制这个方式。



private void Button1_Click(对象发送者,EventArgs MyEventArgs)

{

//查找控件页面。

控制myControl1 = FindControl(TextBox2);

if(myControl1!= null)

{

//获取控件的父级。

控制myControl2 = myControl1.Parent;

Response.Write(文本框的父级是:+ myControl2.ID);

}

else

{

Response.Write(未找到控件);

}

}



http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol(v=vs .71).aspx [ ^ ]


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


这篇关于使用id在asp.net中通过FindControl查找控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆