在DataList中有几个控件,假如一个Label1,一个TextBox1.
代码如下:
TextBox TextBox1= (TextBox)dtlchanginfo.Controls[0].FindControl("TextBox1");
string txt1 = TextBox1.Text;Label Label1= (Label)dtlchanginfo.Controls[0].FindControl("Label1");
string lbl1= Label1.Text ;
这样能够获取TextBox1中的值,但是不能获取Label的值!经过调试能够找到Label1的值,但是其值为空.
请教!
代码如下:
TextBox TextBox1= (TextBox)dtlchanginfo.Controls[0].FindControl("TextBox1");
string txt1 = TextBox1.Text;Label Label1= (Label)dtlchanginfo.Controls[0].FindControl("Label1");
string lbl1= Label1.Text ;
这样能够获取TextBox1中的值,但是不能获取Label的值!经过调试能够找到Label1的值,但是其值为空.
请教!
如果返回null,说明ID输错了或者根本没有这个Label。
如果说类型不能转换,你就是用了一个别的控件。
你得到的值是什么?显示的是什么?
实在不行,你就用TextBox,只是把它的样式设置的跟Label看上去一样就可以了。
通过写这个dtlchanginfo.Controls[0].FindControl("lblrole")调试
得到下面的:
{Text = ""}
[System.Web.UI.WebControls.Label]: {Text = ""}
AppRelativeTemplateSourceDirectory: "~/userpage/"
BindingContainer: {System.Web.UI.WebControls.DataListItem}
ClientID: "dtlchanginfo_ctl00_lblrole"
Controls: {System.Web.UI.ControlCollection}
EnableTheming: true
EnableViewState: true
ID: "lblrole"
NamingContainer: {System.Web.UI.WebControls.DataListItem}
Page: {ASP.userpage_userinfo_aspx}
Parent: {System.Web.UI.WebControls.DataListItem}
Site: null
SkinID: ""
TemplateControl: {ASP.userpage_userinfo_aspx}
TemplateSourceDirectory: "/Web/userpage"
UniqueID: "dtlchanginfo$ctl00$lblrole"
Visible: true
控件是找到了。而值就没了!