Hi All, I have a gridview control that gets its data from an objectdatasource control. Inside this gridview, I have a checkbox where the user can select it. I also have a button that is outside of the gridview and when I click on it, I want it to retrieve the value of the checkbox. Here are some code:
<asp:GridView ID="Grid" runat="server" GridLines="None" CellSpacing="2" AllowPaging="true" AllowSorting="true"CellPadding="0" Width="100%" AutoGenerateColumns="False" OnSorting="OnSorting" OnRowCommand="OnRowCommand" DataSourceID="ObjectDataSource"> <Columns> <asp:TemplateField> <ItemTemplate> <input type="checkbox" runat="server" id="SelectRecord" VALUE='<%# Eval("ObjectID") + "+" + Eval("RecordType")%>'/> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title"/> </Columns> <EmptyDataTemplate> <asp:Label runat="server" ID="noResultsFoundLabel" Text="No results found." ForeColor="red"></asp:Label> </EmptyDataTemplate></asp:GridView><asp:ObjectDataSource ID="ObjectDataSource" runat="server" SelectMethod="Get" TypeName="GetWork" OnSelected="OnSelected"> <SelectParameters> <asp:Parameter Direction="Input" Name="recordType" Type="string" DefaultValue=""/> <asp:Parameter Direction="Output" Name="totalRecords" Type="int32"/> </SelectParameters></asp:ObjectDataSource><asp:button runat="server" id="btnClick" text="Click!" OnClick="FindCheckBox" />
For my code behind, I have the following:
public string FindCheckBox() {if (Page.IsPostBack) {// look for checked boxes HtmlInputCheckBox recordCheckBox;foreach (GridViewRow Itemin Grid.Rows) { recordCheckBox = (HtmlInputCheckBox)Item.Cells[0].FindControl("SelectRecord");if (recordCheckBox !=null && recordCheckBox.Checked)return recordCheckBox.Value; } }return""; } The problem occurs when I click on the paging on the gridview. For example, If I have 20 records, and I set each page to display 10 records. So then I would have 2 pages to select from. So in the case
I select page 2, and click on the checkbox of the first record in page 2, and then press the button. I want it to retrieve the value of the first checkbox in the 2nd page. However, the foreach loop is looping
through the GridViewRows in page 1 and not page 2 which keeps giving me the wrong result. Anyone got idea on how I can fix this problem? Thanks.
Hi,
Based on my understanding, you use ObjectDataSource to retrieve data and display them with GridView, which has a CheckBox column and paging enabled. When the page postbacks, the application always loop the first paging of GridView. If I have misunderstood you, please feel free to let me know.
To better understand your issue, could you please confirm the following information:
Please make sure that we don't bind the GridView before looping this GridView.
Hi Thomas, I believe I have found the problem (although I don't understand why...) and a solution to it.
Basically. I have a master page and a content page. The content page contains two asp panel, one call toolbar and one call grid. the toolbar panel is pass back to the master page and have it render it.
Something like Master.Toolbar = this.toolBar. And on the Master page, I have a Toolbar property that will add it to a placeholder.
public Control Toolbar
{
set { this.placeHolder.add(value); }
}
Everything seems to work properly, except for when a page contains a gridview. It would somehow when i try to do a foreach loop on the grid.rows, it would quit instantly with a rows.count = 0. I play around with it and if I don't pass the toolbar control back to the Master page, then it works fine. However, because the design of passing a control was from an older .NET 1.1 code, many of the pages already use this design so it wouldn't be feasible for me to make the change. Therefore I came up with using a 2nd Content Place Holder and inside that have the orginal PlaceHolder control that will be use by the old pages. And since I remember that if I don't specify using a content inside a content page, the default "content" from the master page will load, I took advantage of it. So all the new pages that uses gridview will use the 2nd content place holder to display the Toolbar, while the older page can be the same without being affected.
Unfortunately, I still don't understand why passing a panel back to a master page for display, while having the event handler "OnClick" code inside my content page will kill the gridview... Hopefully you might be able to enlighten me :)
Thanks.
0 comments:
Post a Comment