maintaining viewstate in web user control

S

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
 
J

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.
 
S

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
 
S

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
 
A

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top