开发者

How to change view-state value dynamically?

I have textbox and button controls in my page. For textbox I have enabled view state, page-load event I am setting text box value “Hello Mr!”. Now I want to change the view state value for text box to “Hello Mr Pradeep!” when post back occurs, how can I do that? And in which all page events I can do that.

开发者_如何学Go
<asp:TextBox ID="TextBox1" runat="server" EnableViewState= "true"/>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button"  />

protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Text = "Hello Mr!";
        }

Thanks,

Pradeep


As far as i understand your question you can do this using javascript on page load event


Your page is retrieving viewstate between the init of the page and just before the page load event. So the more early attempt you can make to change the view State is in the page load. Because if you were to modify it before its retrieving your changes would be lost.

 protected override void OnInit(EventArgs e)
        {
            if (IsPostBack)
            {
                //on postback ViewSate["test"] is null
                ViewState["test"] = "Valuepostback";
                //Now ViewSate["test"] is Valuepostback

            }
            base.OnInit(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            if (IsPostBack)
            {
                //on postback ViewState has been reloaded from the page sent and therefore the initial value set in the oninit does not exists anymore
                //ViewState["test"] is MyValue
                //if  you want to cahnge specifically the view state do it here
            }

            if (!IsPostBack)
                ViewState["test"] = "MyValue";

            base.OnLoad(e);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜