要在formview中将控件的属性在页面初始加载的时候设置,如:要将formview的EidtTemplate中Label1的Text设置为111,
aspx
<asp:FormView ID="FormView1" Runat="server">
<EditItemTemplate>
<asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
</EditItemTemplate>
......
cs:
private void Page_Load(object sender, EventArgs e)
{
Label l1 = (Label)FormView1.FindControl("Label1");
if (l1 != null)
{
l1.Text = "111";
}
else
{
Response.Write("找不到控件");
}
}
l1总是为null,在csdn上搜索只有关于datagrid的,msdn上结合formview讲的只有找pagertemplate上的控件的,没有itemtemplate/inserttemplate/edittemplate找控件的, 在google上搜索也看到老外有这个问题没解决,忘高手们帮忙啊,在线等,解决马上结贴,顶者有分
aspx
<asp:FormView ID="FormView1" Runat="server">
<EditItemTemplate>
<asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
</EditItemTemplate>
......
cs:
private void Page_Load(object sender, EventArgs e)
{
Label l1 = (Label)FormView1.FindControl("Label1");
if (l1 != null)
{
l1.Text = "111";
}
else
{
Response.Write("找不到控件");
}
}
l1总是为null,在csdn上搜索只有关于datagrid的,msdn上结合formview讲的只有找pagertemplate上的控件的,没有itemtemplate/inserttemplate/edittemplate找控件的, 在google上搜索也看到老外有这个问题没解决,忘高手们帮忙啊,在线等,解决马上结贴,顶者有分
解决方案 »
- 在web.config中设置了Theme,如果某个页面不需要Theme,怎么办?
- Password字段名问题,无法UPDATE Access数据库中名为Password的字段
- 我有A和B两个网站,发布在不同的地方,可以直接相互POST数据吗?
- 最后3分...求助....为什么fileupload上传exe就会出错
- 关于ASP.NET内置的Profile对应到默认aspnetdb数据库的表
- Update 无法找到 TableMapping['Table'] 或 DataTable“Table”
- Nhibernate绑定Gridview的问题(一对多,多对一)
- 求思路,怎么生成淘宝那样的验证码.
- 请教一下服务器控件和JAVASCRIPT的使用~~~~~~~~~~
- 天哪, 我要下岗了, 请问ASP.NET和FrontPage2003怎么配合使用? 怎么把页面设计和后台设计区分给两个人做?
- HyperLink在datalist中如何携带两个参数?
- 有复制值就以"-"表示,这样的sql语句怎么样写呀?
System.Web.UI.Control.FindControl(string)
System.Web.UI.Control.FindControl(string, int)
System.Web.UI.Page.FindControl(string)
System.Web.UI.WebControls.CheckBoxList.FindControl(string, int)
System.Web.UI.WebControls.RadioButtonList.FindControl(string, int)我这里用到的是System.Web.UI.Control.FindControl(string),
public virtual System.Web.UI.Control FindControl(string id)
Member of System.Web.UI.ControlSummary:
Searches the current naming container for a server control with the specified id parameter.Parameters:
id: The identifier for the control to be found. Return Values:
The specified control, or null if the specified control does not exist.
我也觉得是层次的问题,但是不知道naming container是什么。
所以找不到
你在page_load里面去找它,应该:
for(int i=0;i<this.FormView1.Items.Count;i++)
{
Label Label1 = (Label)this.FormView1.Items[i].FindContr("Label1");
Label1.Text = "111";
}或者这样:
Label Label1 = (Label)this.FormView1.Items[0].FindContr("Label1");但前提是,你这个FormView1必须事先有数据,否则它是空的,根本没有任何控件.
Label l1 = (Label)FormView1.FindControl("Label1");
我也不知道FORMVIEW是怎么样的控件,也许不需要提供这个属性,但是如果有的话我想应该要加上
这些服务器控件,都会以子控件,孙控件,孙孙控件这样的形式出现,所以,你可以一层一层的自行判断,看看到底在哪个控件里面,才隐藏我们放进去的控件.
this.response.write(this.FormView1.Controls.Count.ToString());
不断地一层一层探测
还有
this.FormView1.Controls[0].GetType().ToString();
this.FormView1.Controls[0].Controls[0].GetType().ToString();
等,相信这样做,用不了半小时,就可以全部知道了.
问题2.控件是在数据行里的,即Formview.Row.Findcontrol("controlid")就可以了,代码如下
void FormView1_DataBound(object sender, EventArgs e)
{
FormViewRow row = FormView1.Row;
Label l1 = (Label)row.FindControl("Label3");
if (l1 != null)
{
l1.Text = "1111";
}
else
{
Response.Write("找不到控件");
}
}
我还没用过 hchxxzx(NET?摸到一点门槛) 的方法看过控件,现在试试
总之,非常感谢各位,以前问的几个问题都是asp.net2.0里的,很少有人光顾,今天这么多人来帮我, 很高兴!!!