473,761 Members | 4,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

datagrid dynamic web controls viewstate problem

Sue
I'm missing something very simple here, and I need an answer fairly quick if
possible. I'm behind on this project now because of this...

Code examples below question...

I have a datagrid (DG) that I've pared down to the bare essentials. The two
controls involved (MyButton and MyValue) are created in the DG_ItemCreated
sub and added to the two placeholders in the datagrid.

If the button is clicked, the DG_ItemCommand grabs the button's
CommandArgument and sticks it into ViewState("Sele cted Value").

In the DG_ItemDataBoun d sub, if this is a postback trip, the current
e.item.button's CommandArgument value is checked
against the ViewState("Sele cted Value") entry, and if they don't match, a
sub is called using existing postback
property values. If they do match, the property values for the Button and
Label control are "flip-flopped" programatically , then the sub is called.

All the postback values for other controls (not shown) are retained
correctly. The postback value for the MyValue Label does not retain the new
property settings from DG_ItemDataBoun d, and therein lies the problem. I'm
missing creating or changing the controls in the wrong sub or ???. Any ideas?

tia,
Sue
<asp:datagrid id="DG" Runat="server" EnableViewState ="True">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:PlaceHol er ID="ButtonHolde r" Runat="server"/>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:templateco lumn>
<ItemTemplate >
<asp:PlaceHolde r ID="ValueHolder " Runat="server"/>
</ItemTemplate>
</asp:templatecol umn>
</Columns>
</asp:datagrid>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
If Not Page.IsPostBack Then Call loaddata()
End Sub

Private Sub loaddata()
DG.DataSource = some dataset
DG.DataBind
End Sub

Private Sub DG_ItemCreated( ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles DG.ItemCreated

Select Case e.Item.ItemType
Case ListItemType.It em, ListItemType.Al ternatingItem,
ListItemType.Se lectedItem

Dim ButtonHolder As PlaceHolder
ButtonHolder = CType(e.Item.Fi ndControl("Butt onHolder"),
PlaceHolder)
If Not ButtonHolder Is Nothing Then
Dim MyButton As New ImageButton
With MyButton
.ID = "MyButton"
.EnableViewStat e = True
.ImageAlign = ImageAlign.AbsM iddle
.ImageUrl = "Flip.jpg"
.CommandName = "UpdateControls "
.ToolTip = "Flip message."
End With
ButtonHolder.Co ntrols.Add(MyBu tton)
End If

Dim ValueHolder As PlaceHolder
ValueHolder = CType(e.Item.Fi ndControl("Valu eHolder"),
PlaceHolder)
If Not ValueHolder Is Nothing Then
Dim MyValue As New Label
With MyValue
.ID = "MyValue"
.Visible = False
.EnableViewStat e = True
.Text = "Flip"
End With
ValueHolder.Con trols.Add(MyVal ue)
End If
End Select
End Sub

Private Sub DG_ItemDataBoun d(ByVal sender As System.Object, ByVal e
As System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles DG.ItemDataBoun d

Select Case e.Item.ItemType
Case ListItemType.It em, ListItemType.Al ternatingItem,
ListItemType.Se lectedItem

Dim MyButton As ImageButton
MyButton = CType(e.item.Fi ndControl("MyBu tton"),
ImageButton)
If MyButton Is Nothing Then Exit Sub

If Not IsDBNull(e.Item .DataItem("ID") ) Then
MyButton.Comman dArgument = e.Item.DataItem ("ID")

Dim MyValue As Label
MyValue = CType(ValueHold er.FindControl( "MyValue"),
Label)
If MyValue Is Nothing Then Exit Sub

' viewstate created and value assigned in DG_ItemCommand sub below;
' selected value <> button's commandargument value, then
' call function without changing any control properties
If Not viewstate("Sele ctedValue") =
MyButton.Comman dArgument Then
call SomeSub(MyButto n.CommandArgume nt,
Trim(MyValue.Te xt))
Else
' else flip the control properties to opposite values, then call the
function
If Trim(MyValue.Te xt) = "Flip" Then
MyValue.Text = "Flop"
MyButton.ImageU rl = "Flop.jpg"
MyButton.ToolTi p = "Show flop message"
Else
MyValue.Text = "Flip"
MyButton.ImageU rl = "flip.jpg"
MyButton.ToolTi p = "Show flip message"
End If
call SomeSub(MyButto n.CommandArgume nt,
Trim(MyValue.Te xt))
End If
End If
End Select
End Sub

Private Sub DG_ItemCommand( ByVal source As System.Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles DG.ItemCommand
If Not e.CommandName = "Page" Then
If e.CommandName = "UpdateControls " Then
viewstate("Sele ctedValue") = e.CommandArgume nt
Call loaddata()
End If
End If
End Sub

Dec 2 '05 #1
0 1332

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

Similar topics

8
7932
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the header of the datagrid in the "ItemDataBound" method of the grid. However, once, they are added in the grid, i seem to lose the event handler for the same control. Is there anyone who has tried this before and knows why this is hapenning ??
6
4477
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are clicked. I have read several posts which say to put your column generating section in the Page_Init section and it will solve the problem....however, it hasn't solved mine. Can somebody please take a look at this and provide any insight if possible?
3
4884
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
2
4000
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for ListItemType.Pager. The problem I'm having is subscribing to the selectedindex changed event. The datagrid doesn't even seem fire an itemcommand, but that's ok, just the selectedindex changed event would do... Any advice is greatly appreciated!
4
2079
by: Luis Esteban Valencia | last post by:
I have a asp.net page (C#), with a datagrid. I use template for all columns, and have <asp:requiredfieldvalidator> in with one of the textboxes, to make sure it's filled in. However, this validation is not firing, even when I leave the field empty. Below please find the code: <%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %>
5
2791
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go to the database to get the next page. Is there a way to use the dataset to allow us to read back and forth in it instead of going back to the database to get it? Thanks,
1
1918
by: pbb | last post by:
I'm creating a set of dynamic controls on a webpage by calling my BuildControls sub in the Page_Init sub. I recreate the controls by calling the BuildControls sub in the LoadViewState override procedure. The set of controls will not always be the same (based on the selection the user makes in a static ddl). The controls are persisted perfectly. The problem I'm experiencing is that when one of the dynamic ddl's (let's say ddl1) is...
9
2727
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the code: <script runat="server"> Dim sqlConn As New SqlConnection(".....") Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then FillDataGrid()
0
2230
by: Scott Roberts | last post by:
I always thought that the viewstate "keys" included the control ID. As long as the control IDs were unique, there shouldn't be any conflicts. Well, it appears that that may not be the case with dynamic controls. Our application was having a problem very similar to the one described here: http://weblogs.asp.net/alessandro/archive/2008/01/04/failed-to-load-viewstate-typical-problem-with-an-obvious-solution.aspx Unfortunately, the "fix"...
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9957
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...
0
9775
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...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
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
6609
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
5229
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...
1
3881
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

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.