Home All Groups Group Topic Archive Search About

maintaining viewstate in web user control

Author
28 Jun 2006 3:29 PM
steven scaife
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

Author
28 Jun 2006 5:24 PM
John Blair
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.



Show quoteHide quote
"steven scaife" <stevensca***@discussions.microsoft.com> wrote in message
news:5DEC8850-937E-4F38-BAD5-83D8E79B6521@microsoft.com...
>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
Author
29 Jun 2006 9:22 AM
steven scaife
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

Show quoteHide quote
"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" <stevensca***@discussions.microsoft.com> wrote in message
> news:5DEC8850-937E-4F38-BAD5-83D8E79B6521@microsoft.com...
> >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
>
>
>
Author
29 Jun 2006 9:46 AM
Alessandro Zifiglio
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
Show quoteHide quote
"steven scaife" <stevensca***@discussions.microsoft.com> ha scritto nel
messaggio news:60292189-F4AF-4086-BD97-ADEA1FBEFBBC@microsoft.com...
> 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" <stevensca***@discussions.microsoft.com> wrote in message
>> news:5DEC8850-937E-4F38-BAD5-83D8E79B6521@microsoft.com...
>> >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
>>
>>
>>
Author
29 Jun 2006 9:38 AM
steven scaife
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

Show quoteHide quote
"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" <stevensca***@discussions.microsoft.com> wrote in message
> news:5DEC8850-937E-4F38-BAD5-83D8E79B6521@microsoft.com...
> >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
>
>
>