473,382 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Viewstate erased on databind?

Hello all,

I have a user control (ascx) that has a DropDownList on it.
The User Control exposes the selectedvalue/text of the ddl.
The User Control Has a Click event.

I place that User control on the page dynamically in the OnInit.
In the Click event for the control I am trying to acces the properties
like this:
string s
s= ((NewUserSection)ph.FindControl("uc")).SelectedUse rTypeText

If i populate the drop down in the page load of the control like this
(my belief of what is the standard way):

this.ddl.DataSource=sqlReader;
this.ddl.DataTextField="ENTITY_NAME";
this.ddl.DataValueField="ROLE_ID";
this.ddl.DataBind();

I only ever get the first item in the list.
I assume I am losing view state.

If I populate the drop down in the page load of the control like this:

while (reader.Read())
{
ddl.Items.Add(new ListItem(reader["ENTITY_NAME"].ToString(),
reader["ROLE_ID"].ToString());
}

When the onclick is fired I get the selected text as requested.

WHY???

what is happening in the databind that is not when I do it by hand?

I have run the code both ways side by side and one works and one does
not.

just curious at this point.

Tal

Nov 17 '05 #1
1 1708
You should be testing IsPostBack before doing your databind:

if (!IsPostBack)
{
this.ddl.DataSource=sqlReader;
this.ddl.DataTextField="ENTITY_NAME";
this.ddl.DataValueField="ROLE_ID";
this.ddl.DataBind();
}

Also, in your second example, unless you're doing something that you're not
showing, you're just adding the contents of your sqlReader to the end of the
dropdownlist, leaving the original contents there as well. So your code can
retrieve the selection but the contents will all be there twice or more, as
you load and reload the page.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"tal_mcmahon" <ta*********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello all,

I have a user control (ascx) that has a DropDownList on it.
The User Control exposes the selectedvalue/text of the ddl.
The User Control Has a Click event.

I place that User control on the page dynamically in the OnInit.
In the Click event for the control I am trying to acces the properties
like this:
string s
s= ((NewUserSection)ph.FindControl("uc")).SelectedUse rTypeText

If i populate the drop down in the page load of the control like this
(my belief of what is the standard way):

this.ddl.DataSource=sqlReader;
this.ddl.DataTextField="ENTITY_NAME";
this.ddl.DataValueField="ROLE_ID";
this.ddl.DataBind();

I only ever get the first item in the list.
I assume I am losing view state.

If I populate the drop down in the page load of the control like this:

while (reader.Read())
{
ddl.Items.Add(new ListItem(reader["ENTITY_NAME"].ToString(),
reader["ROLE_ID"].ToString());
}

When the onclick is fired I get the selected text as requested.

WHY???

what is happening in the databind that is not when I do it by hand?

I have run the code both ways side by side and one works and one does
not.

just curious at this point.

Tal

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Corey | last post by:
I'm trying to finish off do an ASP.NET project where a DropDownList box is used to access a Table. Once you Make a selection on an item in the DropDownListBox, it updates a DataView, and text...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
2
by: Bill Yeager | last post by:
I have a grid that I databind with a strongly typed dataset which contains thousands of records retrieved from a database. After the dataset is retrieved from the database, I place it in...
6
by: sonic | last post by:
Hi, I am experimenting with different viewstate management ideas for large datagrids, and found a microsoft suggestion to turn it off, and only store relevant information by manually accessing...
5
by: VK | last post by:
Hello, I have created a datalist control which I derived from the datalist control. Now I also implemented sorting for this datalist. In my webform when the user clicks the header, then I...
1
by: Graham | last post by:
I have created a server control that reads data from an xml file and renders either a datagrid of entries from a database or group of form controls to allow editing of an individual entry from the...
1
by: Mark Olbert | last post by:
I have a "master" composite control which, in turn, holds an instance of a "detail" composite control (the "master" control will ultimately contain multiple instances of the "detail" control, but...
2
by: dotComPaJi | last post by:
Hello There, I have a simple question. In one of my program if I use ViewState then program does not work as expected(nothing is displayed in GridView but If I use Session State then it works...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.