473,770 Members | 4,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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= ((NewUserSectio n)ph.FindContro l("uc")).Select edUserTypeText

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.DataSo urce=sqlReader;
this.ddl.DataTe xtField="ENTITY _NAME";
this.ddl.DataVa lueField="ROLE_ ID";
this.ddl.DataBi nd();

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(n ew ListItem(reader["ENTITY_NAM E"].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 1726
You should be testing IsPostBack before doing your databind:

if (!IsPostBack)
{
this.ddl.DataSo urce=sqlReader;
this.ddl.DataTe xtField="ENTITY _NAME";
this.ddl.DataVa lueField="ROLE_ ID";
this.ddl.DataBi nd();
}

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_mcmaho n" <ta*********@ho tmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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= ((NewUserSectio n)ph.FindContro l("uc")).Select edUserTypeText

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.DataSo urce=sqlReader;
this.ddl.DataTe xtField="ENTITY _NAME";
this.ddl.DataVa lueField="ROLE_ ID";
this.ddl.DataBi nd();

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(n ew ListItem(reader["ENTITY_NAM E"].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
33772
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 boxes on the form get updated. I'd like to do this without a submit button. Shouldn't the DropDownList box PostBack? Why does SelectedIndexChanged never get hit? Does the answer lie within ViewState?
8
4279
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 which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this...
2
3199
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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp The problem I am having is that the data in the custom datagridcolumn is not saved to viewstate and after postback, the column does not contain data.
2
1129
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 viewstate. I need to do this because of the following: A user is allowed to check "x" amount of rows on the grid and submit those records (which causes a postback) to the database for updating. At this point, I grab the dataset from viewstate and...
6
2555
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 viewstate. as per some helpful suggestins in MSDN "Common DataGrid Mistakes" http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-commondatagridmistakes.asp they suggest disabling datagrid viewstate and adding...
5
317
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 simply store the SortExpression in the viewstate and do the sorting then. Now I would like to have some kind of indication for the sorting, so that the user can see which column is sorted and in which direction. I would like to do that in the...
1
1369
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 same database. The Xml defines how the datagrid and edit form looks and what fields each displays. The edit form display is perfect but I am having some problems with the datagrid list display. The datagrid has sorting and pagination enabled and...
1
1979
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 I'm keeping things simple to try and figure out what's going wrong). The master control supports databinding. Upon postback, the detail control is not retrieving its state from ViewState after it is created and added to the master control during...
2
3165
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 fine. Any idea??? ----------------------------------------------- //Does not work as expected
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.