I believe many comrades have already encountered this problem. This is not surprising in itself. Let's discuss the issue in detail below.
I. Pre-rendered data cannot be changed
1. As you know, pre-rendered data can not be changed, may have been mentioned before, see demo here, customize a control
[DefaultProperty("Text")]
[ToolboxData("<{0}:JsControl runat=server></ {0}:JsControl>")]
public class JsControl : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void OnPreRender(EventArgs e)
{
Text = "hello,you can't change me";
base.OnPreRender(e);
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
ASPX page
protected void Button1_Click(object sender, EventArgs e)
{
JsControl1.Text = "I want to change the Text property";
}
You will find that you have not changed the attributes. This involves the execution of the control lifecycle. Why say this, because most of the script for the control is registered in the pre render.
What's wrong with that? Its own idea is good, the script registers this event (refers to OnPreRender), registers the script resource in the front (in the middle of the control clip), and the script initializes at the end. This conforms to the JavaScript usage principle, importing the script first, then having the tag, the initialization script must be placed behind the tag