Mantain ViewState

Discussion in 'ASP .Net' started by Jonathan, Mar 9, 2005.

  1. Jonathan

    Jonathan Guest

    Hello,
    I'm writed a WebCustomControl but I can't mantain the viewstate this is the
    code of my WebCustomContol:

    public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,
    IPostBackEventHandler
    {
    public event EventHandler DoPostBack;
    protected override void Render(HtmlTextWriter output)
    {
    output.Write(GetControlHTML());
    }
    private string GetControlHTML()
    {
    StringWriter TextBoxWriter = new StringWriter();
    HtmlTextWriter ControlWriter = new HtmlTextWriter(TextBoxWriter);
    TextBox box = new TextBox();
    box.ID = "txtViewState";
    if(ViewState["txtViewState"] == null)
    ViewState.Add("txtViewState","Testeando");
    box.Text = (string)ViewState["txtViewState"];
    box.RenderControl(ControlWriter);
    Button button = new Button();
    button.Text = "DoPostBack";

    button.Attributes.Add("onclick",Page.GetPostBackClientEvent(button,"doPostBa
    ck"));
    button.RenderControl(ControlWriter);
    return TextBoxWriter.ToString();
    }
    public void RaisePostBackEvent(string eventArgument)
    {
    if(eventArgument == "doPostBack")
    {
    if(this.DoPostBack != null)
    this.DoPostBack(this,EventArgs.Empty);
    }
    }
    }

    Somebody knows how is the correct way to mantain the ViewState

    Thanks!
     
    Jonathan, Mar 9, 2005
    #1
    1. Advertising

  2. From the code that you have displayed, it looks that you are using the
    ViewState ok, although you are doing things the hard way. You can, instead,
    just create a button and a text box and handle the button event without
    worrying about postbacks.

    Two things you should look for. First, make sure you have the ViewState
    enabled (you can check by Page.EnableViewState). Second, make sure that your
    page is not being posted back more than once or re-directed. Best way to look
    for that is to enable tracing on the containing page. You should then be able
    to see if the page posted back once or more times. You will also be able to
    see the size of the viewstate for each control.

    Regards,
    -Visar

    "Jonathan" wrote:

    > Hello,
    > I'm writed a WebCustomControl but I can't mantain the viewstate this is the
    > code of my WebCustomContol:
    >
    > public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,
    > IPostBackEventHandler
    > {
    > public event EventHandler DoPostBack;
    > protected override void Render(HtmlTextWriter output)
    > {
    > output.Write(GetControlHTML());
    > }
    > private string GetControlHTML()
    > {
    > StringWriter TextBoxWriter = new StringWriter();
    > HtmlTextWriter ControlWriter = new HtmlTextWriter(TextBoxWriter);
    > TextBox box = new TextBox();
    > box.ID = "txtViewState";
    > if(ViewState["txtViewState"] == null)
    > ViewState.Add("txtViewState","Testeando");
    > box.Text = (string)ViewState["txtViewState"];
    > box.RenderControl(ControlWriter);
    > Button button = new Button();
    > button.Text = "DoPostBack";
    >
    > button.Attributes.Add("onclick",Page.GetPostBackClientEvent(button,"doPostBa
    > ck"));
    > button.RenderControl(ControlWriter);
    > return TextBoxWriter.ToString();
    > }
    > public void RaisePostBackEvent(string eventArgument)
    > {
    > if(eventArgument == "doPostBack")
    > {
    > if(this.DoPostBack != null)
    > this.DoPostBack(this,EventArgs.Empty);
    > }
    > }
    > }
    >
    > Somebody knows how is the correct way to mantain the ViewState
    >
    > Thanks!
    >
    >
    >
     
    =?Utf-8?B?VmlzYXIgR2FzaGk=?=, Mar 9, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. billie
    Replies:
    3
    Views:
    306
    Sybren Stuvel
    Oct 5, 2005
  2. Mike

    Mantain Property

    Mike, Mar 14, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    109
  3. Jonathan

    Mantain ViewState

    Jonathan, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    103
    Jonathan
    Mar 10, 2005
  4. Mike

    Mantain Property

    Mike, Mar 14, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    109
    Brock Allen
    Mar 14, 2005
  5. Alessandro Benedetti

    How to mantain the state between 2 call of the same WebService?

    Alessandro Benedetti, Mar 17, 2005, in forum: ASP .Net Web Services
    Replies:
    13
    Views:
    228
    Brock Allen
    Mar 17, 2005
Loading...

Share This Page