This is very strange. I have a gridview with item template checkboxes and buttons in each row. I am toggling the text value of the buttons (using JQuery) representing the user adding rows to a cart of items for purchase. I have a button on the form seperate from the gridview which when clicked raises an event in the code behind to spin through the rows and process the selected (toggled button text) rows. But what is happening is upon pressing the process button the grid rows are reverting back to their original states (original button text and also modified line attributes for the previously selected rows). I am not rebinding the grid. This is also not an update grid. Now the checkboxes have always worked just fine but the button text is obviously very different. Can anybody tell me why this is happening and how I may correct it? One idea I have is to create a column of hidden checkboxes and when I toggle my button text, I can toggle the checkboxes as well. But I sure wish I could understand why this is happening. Also, on the postback from the process button having been checked, the screen is rendered in it’s original state as well.
Here is my grid with the checkboxes and buttons:
<div class="OneHundredPercentWide" style="padding-top:7px">
<asp:Panel ID="Panel1" runat="server" Width="100%" >
<asp:GridView id="grvSessionOrderDownloads" runat="server" BorderWidth="2px" BorderStyle="Solid" BorderColor="#C0C0FF"
Font-Names="Arial" Font-Bold="True" Font-Size="8pt" GridLines="Horizontal" AutoGenerateColumns="False"
HorizontalAlign="Left" ForeColor="Blue" CellPadding="2" AllowSorting="True" Width="100%"
OnRowDataBound="grvSessionOrderDownloads_OnRowDataBound" DataKeyNames="SORD_ID">
<HeaderStyle Font-Names="Arial" HorizontalAlign="Center" ForeColor="White" BackColor="#152EE5"
CssClass="sessionOrderDownloadHeaders" />
<RowStyle BackColor="White" ForeColor="#333333" />
<AlternatingRowStyle BackColor="#A5B0FF" />
<Columns>
<asp:BoundField Visible="False" DataField="SORD_ID" HeaderStyle-Width="0px" >
<HeaderStyle Width="0px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Select <br /> Files" HeaderStyle-ForeColor="White" >
<ItemTemplate >
<asp:CheckBox ID="chkSelectVideo" runat="server" OnClick="checkboxClicked(this)" ToolTip="Select file for download"
Enabled='<%# Eval("SORD_EnableSelectionCheckBox") %>' Checked='<%# Eval("SORD_SelectedForDownloadFlag") %>' />
</ItemTemplate>
<HeaderStyle Width="6%" />
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField><asp:TemplateField HeaderText="Prior <br /> Downld" HeaderStyle-ForeColor="White" >
<ItemTemplate >
<asp:Button id="btnBuy" runat="server" OnClientClick="btnBuyToggle(this); return false;"
Text="BUY This" CssClass="buyButton" Visible='<%# Eval("SORD_ShowBuyButton") %>' />
</ItemTemplate>
<HeaderStyle Width="7%" />
<ItemStyle CssClass="sessionOrderDownloadItems" VerticalAlign="Middle" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is part of my code behing to check the button text:
Dim gvr As GridViewRow
For Each gvr In grvSessionOrderDownloads.Rows
Dim selBtn As Button = CType(gvr.FindControl("btnBuy"), Button)
If Not selBtn Is Nothing Then
If selBtn.Enabled And selBtn.Visible Then
Dim strSORD_ID As String = grvSessionOrderDownloads.DataKeys(gvr.RowIndex)("SORD_ID").ToString
If selBtn.Text = "BUY Video" Then
// Note: I will process here
End If
End If
End If
Next
Thank you,
Jim
The checkbox changes are posted back to the server once they are changed in the browser. But when you change the text of the button (or other fields) in the grid using JS, the changed button text (or other fields) is not sent to the server. Hence when the button is rendered in the next post back, it uses the original default value which is “BUY This”.
In order to retain the button text, you would have to create a hidden field along with the button. When you change the text of the button (in OnClientClick JS event), change the value of the hidden field simultaneously. The hidden field changes are sent to the server which helps you to read them on the process button click event.