By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

datagrid dynamic web controls viewstate problem

P: n/a
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?

<asp:datagrid id="DG" Runat="server" EnableViewState="True">
<asp:PlaceHoler ID="ButtonHolder" Runat="server"/>
<asp:PlaceHolder ID="ValueHolder" Runat="server"/>
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
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,

Dim ButtonHolder As PlaceHolder
ButtonHolder = CType(e.Item.FindControl("ButtonHolder"),
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
End If

Dim ValueHolder As PlaceHolder
ValueHolder = CType(e.Item.FindControl("ValueHolder"),
If Not ValueHolder Is Nothing Then
Dim MyValue As New Label
With MyValue
.ID = "MyValue"
.Visible = False
.EnableViewState = True
.Text = "Flip"
End With
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,

Dim MyButton As ImageButton
MyButton = CType(e.item.FindControl("MyButton"),
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"),
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,
' else flip the control properties to opposite values, then call the
If Trim(MyValue.Text) = "Flip" Then
MyValue.Text = "Flop"
MyButton.ImageUrl = "Flop.jpg"
MyButton.ToolTip = "Show flop message"
MyValue.Text = "Flip"
MyButton.ImageUrl = "flip.jpg"
MyButton.ToolTip = "Show flip message"
End If
call SomeSub(MyButton.CommandArgument,
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.