I have a GridView control on my ASP.NET page that binds to result set when the user executes a search. I create an additional TemplateField column with a CheckBox control to allow the user to select a sub set of records from the result set. I have implemented paging in the GridView control and when the user checks the checkbox control and pages through the result set it does not retain any of the checked checkboxes.
<asp:GridView ID="MyGridView" runat="server" AllowPaging="true" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="MyCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Whaat is the best way to retain the checked checkboxes while paging through the GridView?
You have to maintain the state yourself. This Thread shows how this can be done in VB. just use this VB to C# converter to get your desired code
you'll need to loop over the rows and save the checked state of the check boxes (along with a datakey) during the gridview paging event. Likewise you will need to read from your saved check state list to re-check the boxes during the gridview rowdatabound event.
Related
I have a gridview that contain label, the value of this label is assigned in code behind,
I enabled sorting in my gridview and when I click the header to sort the value of the label disappear but the other DataFields doesnt.
<asp:TemplateField HeaderText="<%$Resources:mj.resource, req_category%>" SortExpression="category_id">
<ItemTemplate>
<asp:Label ID="category_id" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Have you set EnableViewState="false" on the GridView or at the page level?
Are you using DataSourceControl (like SqlDataSource) to bind data to the GridView?
If so, you'd have to manually sort and bind data again in the Sorting event.
I've created a gridview with edit feature. On clicking the gridview row edit image, it makes the custom edit panel visible and the user can edit and save the gridview row data or s/he can click the cancel button without any updates. It works perfectly fine with .NET framwork 2.0.
But, the problem lies with the .NET framework 4.5 -
when the user clicks the cancel or save button, instead of loading the
normal gridview, it loads the default edit template on the top of
gridview row where the user had clicked before.
So, how can I solve this problem? This happens in all the Gridviews used in our ASP.NET website.
Here's the original Gridview Row: Original GridRow
Here's the custom edit panel with save/cancel: Custom Edit Panel
Here's error: Default Template loaded
I usually achieve what you are trying to do in the row databound event.
Try setting up your grid view like this
<asp:Gridview id="ClientManagementGridView runat="server"/>
<columns>
<asp:TemplateField>
<itemtemplate>
//put edit button here
</itemtemplate>
<edititemtemplate>
// put your update and cancel buttons here
</edititemtemplate>
</asp:Templatefield>
<asp:templatefield>
<itemtemplate>
<aspTextBox Text='<#Eval("ClientId") %>' runat="server" />
</itemtemplate>
<edititemtemplate>
<asp:Panel id="EditPanel">
//your panel contents
</asp:Panel>
</edititemtemplate>
</asp:templateField>
<asp:BoundField DataField="ClientName" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Name" />
</columns>
<asp:GridView>
Now in your row data bound event of your gridview put this magic piece of code
If e.Row.RowState.ToString().Contains("Edit") Then
Dim editGrid As GridView = TryCast(sender, GridView)
Dim colSpan As Integer = editGrid.Columns.Count
e.Row.Cells(0).Visible = True
e.Row.Cells(1).Visible = True
For i As Integer = 2 To colSpan - 1
e.Row.Cells(i).Visible = False
e.Row.Cells(i).Controls.Clear()
Next
e.Row.Cells(1).Attributes("ColSpan") = (colSpan).ToString()
End if
As you can see I am overriding the default edit item template by clearing its default controls and then changing the column span of the cell to fit my custom edit panel.As well as hiding cells I want to be hidden.
I am using a Grid View of asp.net 4.0. I want to use pagination.
I am Handling Pagination with SQL query. I am getting only the number of records, i want to show in my grid. I want to set the total number of records to my grid, so that my grid displays page numbers (eg. 1 2 3 ....8) according to my total number of records. My grid should tell me what is the current page number which is been viewed.
I am unable to set Page numbers according to my total record count.
Please Help me on this.
Thanks in Advance.
Brining back the whole result set to get paging is probably not the way you want to be going. You should really extend the GridView Control and introduce a virtual item count property. You should then pass the virtual item count the Count of all records in your grid. Also you should then ensure that OnPageIndexChanging event should then set the current page index to the next page.
better approach I ever got for this, using store procedure and returning the data based on PageIndex and PageSize.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
//your data
</Columns>
</asp:GridView>
This shows pagination
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text = '<%#Eval("Text") %>' CommandArgument = '<%# Eval("Value") %>' Enabled = '<%# Eval("Enabled") %>' OnClick = "Page_Changed"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
It's all here what you want: Custom Paging in ASP.Net GridView using SQL Server Stored Procedure
This a pretty simple question, I'm just not sure how to do it exactly. I would like to bind a Button or perhaps ImageButton to a GridView in ASP.NET/C#. Currently, the GridView has two columns and is bound to a DataTable with two columns. I want to add a third column to the GridView, which will include the Button.
I know GridView has ButtonField, but I'm not too sure how to go about using it to do what I want. I want to dynamically generate these Buttons and add them to the GridView.
Here is how my GridView looks right now:
<asp:GridView
ID="GridView1"
Runat="server">
<Columns>
<asp:HyperLinkField
HeaderText="Display Name"
DataNavigateUrlFields="DISPNAME"
DataNavigateUrlFormatString="ViewItem.aspx"
DataTextField="DISPNAME">
<ItemStyle Width="70%" />
</asp:HyperLinkField>
<asp:BoundField
DataField="TypeDisp"
HeaderText="Type">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
You can use a template field like the following,
<TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="image url" CommandName="SomeCommand" CommandArgument='<%# Eval("Id") %>'/>
</ItemTemplate>
</TemplateField>
Then you can handle the RowCommand event of the GridView and check the e.CommandName to see what command to be executed and you can get the e.CommandArgument as well which could be the row Id like I used in the code above.
If we are talking a button that's always present, you can use ButtonField, or even use a TemplateField and provide the template with the button, and bind the data to the button (sounds like you may want to bind data to the attributes of the button?)
If you are looking to dynamically generate buttons in the UI, tap into the RowCreated event and add the button the GridView. You'd have to do this on every page load; the GridView won't remember a button created programmatically.
HTH.
I've read multiple sources that say Gridview's do not persist the Gridview.DataSource property on postback. My understanding is that in term's of ASP.NET, a postback is any page load that is not the first pageload (see MSDN).
I've got a situation with 2 very similar gridviews.
GvOne.DataSource is null on postback.
GvTwo.DataSource is NOT null on postback.
The only big difference outside of a few differing columns is GvOne is populated with the Entity Framework and LINQ. GvTwo is populated by a DataTable filled by a SqlDataAdapter.
Further, GvOne and GvTwo have a TemplateField with a TextBox that I use to gather user input. Both use the same code to pull the TextBox.Text on postback:
TextBox tb = (TextBox)GvOne.Rows[i].FindControl("actualTxt");
GvOne properly gathers tb.Text. GvTwo always finds the tb.Text value to be 0.
Basic Gridview code:
<asp:GridView ID="GvOne" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Return">
<ItemTemplate>
<asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
<asp:GridView ID="GvTwo" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Order">
<ItemTemplate>
<asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
Changing GvTwo to use Entity Framework and LINQ is a potential solution, albeit a major undertaking. Does anyone know what is going on here?
UPDATE (See my comment on Joel Etherton's Answer)
Due to popular demand here is the code to populate the gridview within Page_Load event for GvTwo (GvOne is similar):
ordersGV.DataSource = dataSetObject.Tables["activeParts"];
ordersGV.DataBind();
Searching through the code behind I found no other references to ordersGv.Datasource and no other events that are hooked into associated with the page life cycle.
Gridviews do not persist the datasource across postbacks. If you have a gridview that has a non-null datasource then you must be filling that datasource somewhere in your code. It would be instructive to travel through your event cycle to find where exactly the population of the datasource is occuring on postback.
what does your Page_load code look like?
GridView does not keep DataSource property populated over the postbacks for performance issues
Maybe the second gridview is rebinding the datasource on postback?