UserControl 中未分配 TextBox.Text 属性
我的 UserControl 代码如下,我在 UserControl 中有一个 TextBox,并且想从网页访问 TextBox.Text 属性。
UcUserForm 用户控件插入 myform.aspx 网页中。
在 myform.aspx PageLoad 上,我为 textBox 设置了值,如下所示
ucUserForm.TbFirstName.Text = "Tomas";
一切正常。加载网页时,我在文本框中看到名称。然后我将值从 Tomas 更改为 Jonas。
在 myform.aspx ButtonClick 上,我尝试读取值,
var mynewname = ucUserForm.TbFirstName.Text;
尽管网页上的文本框中的名称已从 Tomas 更改为 Jonas,但我仍然使用旧名称 Tomas。无法理解问题出在哪里。
后面的UserControl代码
public partial class UcUserForm: System.Web.UI.UserControl
{
public TextBox TbFirstName
{
get { return tbFirstName; }
}
}
UserControl网页
<asp:TextBox ID="tbFirstName" autocomplete="off" MaxLength="25" runat="server"></asp:TextBox>
在default.aspx中注册用户控件代码
<%@ Register Src="ucUserForm.ascx" TagName="ucUserForm" TagPrefix="uc1" %>
<uc1:ucUserForm ID="ucUserForm" runat="server" />
My UserControl code is below, I have one TextBox in UserControl and would like to access TextBox.Text property from web page.
UcUserForm user control is inserted in myform.aspx web page.
On myform.aspx PageLoad I set value for textBox like this
ucUserForm.TbFirstName.Text = "Tomas";
Everything works fine. When web page is loaded I see name inside textbox. Then I change value from Tomas to Jonas.
On myform.aspx ButtonClick I am trying to read value
var mynewname = ucUserForm.TbFirstName.Text;
despite that name is changed from Tomas to Jonas in TextBox on web page I still get the old name Tomas. Can't understand where is the problem.
UserControl code behind
public partial class UcUserForm: System.Web.UI.UserControl
{
public TextBox TbFirstName
{
get { return tbFirstName; }
}
}
UserControl web page
<asp:TextBox ID="tbFirstName" autocomplete="off" MaxLength="25" runat="server"></asp:TextBox>
Registration user control code in default.aspx
<%@ Register Src="ucUserForm.ascx" TagName="ucUserForm" TagPrefix="uc1" %>
<uc1:ucUserForm ID="ucUserForm" runat="server" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
您每次加载页面时都会设置它。在Init阶段尝试。
与此类似,
Init 在页面的生命周期中仅发生一次
http://msdn。 microsoft.com/en-us/library/ms178472.aspx
You set it every time you load the page. Try in Init stage.
Similar to this
The Init just happens once in the Page's life cycle
http://msdn.microsoft.com/en-us/library/ms178472.aspx