maintaining viewstate in web user control

Discussion in 'ASP .Net Web Controls' started by steven scaife, Jun 28, 2006.

  1. I have created a web user control for use in a web site. Basically it pulls
    all the suppliers from a table and lists them in a drop down list.

    However when the page posts back to itself the drop down list loses the item
    that was selected. I tried adding a public property to the control and using
    a request.form to get the selected item and set the value that way but it
    fails saying Object reference not set to an instance of an object.

    How can i get it so that the control persists state. I have checked that
    everything has viewstate enabled. the control, the page, the panel

    TIA
    steven scaife, Jun 28, 2006
    #1
    1. Advertising

  2. steven scaife

    John Blair Guest

    Hi Steven,

    I had similar problem in my custom control.
    Essentially you need to know that view state starts tracking AFTER you add
    your childe control to your custom control e.g.

    DropDownList ddl = new DropDownList();

    //Any loading of the ddl list here will not be retained in viewstate...only
    AFTER you add it to the container's child controls.

    //Begin tracking viewstate.

    c.Controls.AddAt(c.Controls.Count,ddl);

    //Populate dropdownlist now for it to be retained in viewstate.



    "steven scaife" <> wrote in message
    news:...
    >I have created a web user control for use in a web site. Basically it
    >pulls
    > all the suppliers from a table and lists them in a drop down list.
    >
    > However when the page posts back to itself the drop down list loses the
    > item
    > that was selected. I tried adding a public property to the control and
    > using
    > a request.form to get the selected item and set the value that way but it
    > fails saying Object reference not set to an instance of an object.
    >
    > How can i get it so that the control persists state. I have checked that
    > everything has viewstate enabled. the control, the page, the panel
    >
    > TIA
    John Blair, Jun 28, 2006
    #2
    1. Advertising

  3. Ok I am a bit confused. Am I right in thinking the code you gave me has got
    to be placed within the web user control.

    The control has been dropped on the form in the designer then in the page
    load i populate the control with the following code.

    Dim conn As SqlConnection = Nothing
    Dim dr As SqlDataReader = Nothing
    Dim cmd As New SqlCommand

    Try
    conn = New
    SqlConnection(ConfigurationSettings.AppSettings("ConnString")) 'set the
    connection props
    cmd.Connection = conn
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = ("usr_SP_GetSuppliers")

    conn.Open()
    dr = cmd.ExecuteReader

    Suppliers.DataSource = dr
    Suppliers.DataTextField = "Supplier_Name"
    Suppliers.DataValueField = "SupplierID"
    Suppliers.DataBind()
    Suppliers.Items.Insert(0, New ListItem("Please Select One",
    "Please Select One"))
    Suppliers.Items(0).Selected = True

    Catch ex As Exception
    If Not dr Is Nothing Then
    dr.Close()
    End If
    If Not conn Is Nothing Then
    conn.Close()
    End If

    Finally
    If Not dr Is Nothing Then
    dr.Close()
    End If
    If Not conn Is Nothing Then
    conn.Close()
    End If
    End Try

    However I am unsure of where i add the code that you suggested. Do i add it
    to the page load in the web user control, or do i add it to the aspx page
    load. Or does the code need to go somewhere else.

    Thanks for the help

    "John Blair" wrote:

    > Hi Steven,
    >
    > I had similar problem in my custom control.
    > Essentially you need to know that view state starts tracking AFTER you add
    > your childe control to your custom control e.g.
    >
    > DropDownList ddl = new DropDownList();
    >
    > //Any loading of the ddl list here will not be retained in viewstate...only
    > AFTER you add it to the container's child controls.
    >
    > //Begin tracking viewstate.
    >
    > c.Controls.AddAt(c.Controls.Count,ddl);
    >
    > //Populate dropdownlist now for it to be retained in viewstate.
    >
    >
    >
    > "steven scaife" <> wrote in message
    > news:...
    > >I have created a web user control for use in a web site. Basically it
    > >pulls
    > > all the suppliers from a table and lists them in a drop down list.
    > >
    > > However when the page posts back to itself the drop down list loses the
    > > item
    > > that was selected. I tried adding a public property to the control and
    > > using
    > > a request.form to get the selected item and set the value that way but it
    > > fails saying Object reference not set to an instance of an object.
    > >
    > > How can i get it so that the control persists state. I have checked that
    > > everything has viewstate enabled. the control, the page, the panel
    > >
    > > TIA

    >
    >
    >
    steven scaife, Jun 29, 2006
    #3
  4. nevermind i sorted it. I put your code in the page_init. and called the
    request.form from inside my web control and it worked.

    thanks for the help

    "John Blair" wrote:

    > Hi Steven,
    >
    > I had similar problem in my custom control.
    > Essentially you need to know that view state starts tracking AFTER you add
    > your childe control to your custom control e.g.
    >
    > DropDownList ddl = new DropDownList();
    >
    > //Any loading of the ddl list here will not be retained in viewstate...only
    > AFTER you add it to the container's child controls.
    >
    > //Begin tracking viewstate.
    >
    > c.Controls.AddAt(c.Controls.Count,ddl);
    >
    > //Populate dropdownlist now for it to be retained in viewstate.
    >
    >
    >
    > "steven scaife" <> wrote in message
    > news:...
    > >I have created a web user control for use in a web site. Basically it
    > >pulls
    > > all the suppliers from a table and lists them in a drop down list.
    > >
    > > However when the page posts back to itself the drop down list loses the
    > > item
    > > that was selected. I tried adding a public property to the control and
    > > using
    > > a request.form to get the selected item and set the value that way but it
    > > fails saying Object reference not set to an instance of an object.
    > >
    > > How can i get it so that the control persists state. I have checked that
    > > everything has viewstate enabled. the control, the page, the panel
    > >
    > > TIA

    >
    >
    >
    steven scaife, Jun 29, 2006
    #4
  5. Steven, It seems to me that you are calling that piece of code you pasted,
    regardless if its a postback scenario or not. Dont do that, instead bind
    your dropdownlist to the datasource only if its not a postback.
    if not (Page.IsPostBack) then
    ' call the method that binds your dropdownlist to the datasource
    End if

    If you keep rebinding your dropdownlist even after a postback scenario then
    you keep resetting the dropdownlist each time and rebuilding the items, this
    will cause it to lose state, especially if you are doing this in a phase
    that fires before the SelectedIndexChanged event of the dropdownlist list
    fires, for eg. in page_load.
    The items for the dropdownlist need to set only once, during postback, the
    dropdownlist is going to get populated from viewstate.
    Regards,
    Alessandro Zifiglio
    "steven scaife" <> ha scritto nel
    messaggio news:...
    > Ok I am a bit confused. Am I right in thinking the code you gave me has
    > got
    > to be placed within the web user control.
    >
    > The control has been dropped on the form in the designer then in the page
    > load i populate the control with the following code.
    >
    > Dim conn As SqlConnection = Nothing
    > Dim dr As SqlDataReader = Nothing
    > Dim cmd As New SqlCommand
    >
    > Try
    > conn = New
    > SqlConnection(ConfigurationSettings.AppSettings("ConnString")) 'set the
    > connection props
    > cmd.Connection = conn
    > cmd.CommandType = CommandType.StoredProcedure
    > cmd.CommandText = ("usr_SP_GetSuppliers")
    >
    > conn.Open()
    > dr = cmd.ExecuteReader
    >
    > Suppliers.DataSource = dr
    > Suppliers.DataTextField = "Supplier_Name"
    > Suppliers.DataValueField = "SupplierID"
    > Suppliers.DataBind()
    > Suppliers.Items.Insert(0, New ListItem("Please Select One",
    > "Please Select One"))
    > Suppliers.Items(0).Selected = True
    >
    > Catch ex As Exception
    > If Not dr Is Nothing Then
    > dr.Close()
    > End If
    > If Not conn Is Nothing Then
    > conn.Close()
    > End If
    >
    > Finally
    > If Not dr Is Nothing Then
    > dr.Close()
    > End If
    > If Not conn Is Nothing Then
    > conn.Close()
    > End If
    > End Try
    >
    > However I am unsure of where i add the code that you suggested. Do i add
    > it
    > to the page load in the web user control, or do i add it to the aspx page
    > load. Or does the code need to go somewhere else.
    >
    > Thanks for the help
    >
    > "John Blair" wrote:
    >
    >> Hi Steven,
    >>
    >> I had similar problem in my custom control.
    >> Essentially you need to know that view state starts tracking AFTER you
    >> add
    >> your childe control to your custom control e.g.
    >>
    >> DropDownList ddl = new DropDownList();
    >>
    >> //Any loading of the ddl list here will not be retained in
    >> viewstate...only
    >> AFTER you add it to the container's child controls.
    >>
    >> //Begin tracking viewstate.
    >>
    >> c.Controls.AddAt(c.Controls.Count,ddl);
    >>
    >> //Populate dropdownlist now for it to be retained in viewstate.
    >>
    >>
    >>
    >> "steven scaife" <> wrote in message
    >> news:...
    >> >I have created a web user control for use in a web site. Basically it
    >> >pulls
    >> > all the suppliers from a table and lists them in a drop down list.
    >> >
    >> > However when the page posts back to itself the drop down list loses the
    >> > item
    >> > that was selected. I tried adding a public property to the control and
    >> > using
    >> > a request.form to get the selected item and set the value that way but
    >> > it
    >> > fails saying Object reference not set to an instance of an object.
    >> >
    >> > How can i get it so that the control persists state. I have checked
    >> > that
    >> > everything has viewstate enabled. the control, the page, the panel
    >> >
    >> > TIA

    >>
    >>
    >>
    Alessandro Zifiglio, Jun 29, 2006
    #5
    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. Dinesh Upare
    Replies:
    0
    Views:
    387
    Dinesh Upare
    Jul 16, 2003
  2. sklett
    Replies:
    4
    Views:
    9,865
    sklett
    Apr 8, 2004
  3. Charlie@CBFC
    Replies:
    0
    Views:
    460
    Charlie@CBFC
    Apr 2, 2005
  4. Steve Murphy
    Replies:
    2
    Views:
    747
    =?Utf-8?B?Sm9l?=
    Jan 15, 2006
  5. Patrick Wilby via .NET 247

    Maintaining viewstate in custom control collection

    Patrick Wilby via .NET 247, Apr 23, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    401
    Teemu Keiski
    Apr 23, 2004
Loading...

Share This Page