[英]C# Asp.Net User Control button click
In my code I load a user control (uc) in the page_load event. 在我的代码中,我在page_load事件中加载了一个用户控件(uc)。 The uc contains a button which sets a label text to the textbox value.
uc包含一个按钮,用于将标签文本设置为文本框值。 (
lblTest.Text = txtText.Text
) . (
lblTest.Text = txtText.Text
)。 This works fine in the load event of my page. 这在我的页面的加载事件中工作正常。 But when I set the loading of the uc in my Init event, the label isn't set.
但是当我在我的Init事件中设置uc的加载时,标签没有设置。 When I set breakpoints, I see the button event is fired, the text is set (in the code), but when the page is finished loading, the label is empty.
当我设置断点时,我看到按钮事件被触发,文本被设置(在代码中),但是当页面加载完毕后,标签为空。 How is that possible?
怎么可能?
Check further down in the code to ensure that the control's text isn't being overwritten or set back to empty text. 在代码中进一步检查以确保控件的文本没有被覆盖或设置回空文本。 (Specifically, look further in Page_Init, functions that Page_Init invokes, and Page_Load and its invoked functions.)
(具体来说,请进一步查看Page_Init,Page_Init调用的函数,以及Page_Load及其调用的函数。)
If you're seeing that it's being set in the debugger to the value that you expect, then chances are pretty good that something else is changing it after the fact. 如果你看到它在调试器中被设置为你期望的值,那么事后很可能还有其他东西在改变它。 That's going to be either your code, ViewState, or something else outside of your immediate control.
这将是您的代码,ViewState或其他您无法立即控制的内容。 Find that, and you should be able to do something about it.
找到它,你应该能够做些什么。
I dont think the control fully exists at init. 我不认为控件完全存在于init。
This article explains the page event cycle and may explain the issue you have: http://www.15seconds.com/issue/020102.htm 本文解释了页面事件周期,并可能解释您遇到的问题: http : //www.15seconds.com/issue/020102.htm
To further elaborate a bit on the other answers. 进一步详细说明其他答案。
Whenever an ASP.Net site launches, five things happen in a very specific order . 每当ASP.Net站点启动时,就会以非常特定的顺序发生五件事。 Read Luke's link to find out that order and plot accordingly.
阅读Luke的链接,找出该订单并相应地进行绘图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.