datagrid dynamic web controls viewstate problem

G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top