473,396 Members | 1,827 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,396 software developers and data experts.

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("Selected Value").

In the DG_ItemDataBound sub, if this is a postback trip, the current
e.item.button's CommandArgument value is checked
against the ViewState("Selected 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_ItemDataBound, 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:TemplateColumn>
<ItemTemplate>
<asp:PlaceHoler ID="ButtonHolder" Runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn>
<ItemTemplate>
<asp:PlaceHolder ID="ValueHolder" Runat="server"/>
</ItemTemplate>
</asp:templatecolumn>
</Columns>
</asp:datagrid>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.WebControls.DataGridItemEventArgs) Handles DG.ItemCreated

Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.SelectedItem

Dim ButtonHolder As PlaceHolder
ButtonHolder = CType(e.Item.FindControl("ButtonHolder"),
PlaceHolder)
If Not ButtonHolder Is Nothing Then
Dim MyButton As New ImageButton
With MyButton
.ID = "MyButton"
.EnableViewState = True
.ImageAlign = ImageAlign.AbsMiddle
.ImageUrl = "Flip.jpg"
.CommandName = "UpdateControls"
.ToolTip = "Flip message."
End With
ButtonHolder.Controls.Add(MyButton)
End If

Dim ValueHolder As PlaceHolder
ValueHolder = CType(e.Item.FindControl("ValueHolder"),
PlaceHolder)
If Not ValueHolder Is Nothing Then
Dim MyValue As New Label
With MyValue
.ID = "MyValue"
.Visible = False
.EnableViewState = True
.Text = "Flip"
End With
ValueHolder.Controls.Add(MyValue)
End If
End Select
End Sub

Private Sub DG_ItemDataBound(ByVal sender As System.Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG.ItemDataBound

Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.SelectedItem

Dim MyButton As ImageButton
MyButton = CType(e.item.FindControl("MyButton"),
ImageButton)
If MyButton Is Nothing Then Exit Sub

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

Dim MyValue As Label
MyValue = CType(ValueHolder.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("SelectedValue") =
MyButton.CommandArgument Then
call SomeSub(MyButton.CommandArgument,
Trim(MyValue.Text))
Else
' else flip the control properties to opposite values, then call the
function
If Trim(MyValue.Text) = "Flip" Then
MyValue.Text = "Flop"
MyButton.ImageUrl = "Flop.jpg"
MyButton.ToolTip = "Show flop message"
Else
MyValue.Text = "Flip"
MyButton.ImageUrl = "flip.jpg"
MyButton.ToolTip = "Show flip message"
End If
call SomeSub(MyButton.CommandArgument,
Trim(MyValue.Text))
End If
End If
End Select
End Sub

Private Sub DG_ItemCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DG.ItemCommand
If Not e.CommandName = "Page" Then
If e.CommandName = "UpdateControls" Then
viewstate("SelectedValue") = e.CommandArgument
Call loaddata()
End If
End If
End Sub

Dec 2 '05 #1
0 1316

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

Similar topics

8
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...
6
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...
3
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...
2
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...
4
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...
5
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...
1
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...
9
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.