asp.net c# gridview label value disappear when sorting - c#

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.

Related

Set Focus for ItemTemplate textbox in grid view for asp.net c#

I have one query which is related to get focus in itemfield of Textbox in gridview. Based on checkbox selection populating different number of textBoxes and after entered data in last textbox and making webservice call based on response populating gridview with itemField(textboxes).
The scenario is explaining below.
CheckBox(Unchecked): False
Number of text boxes(ID): 1
CheckBox(Checked): True
Number of text boxes(ID, AccNo, Price): 3
After entered values in last textboxes, make webservice call and filling gridview with data but it's not get focus of first textbox in GridView.
<asp:TemplateField>
<HeaderTemplate>
<p>Paid Amount</p>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAmnt" runat="server" TabIndex="1"
onkeypress="return isNumberKey(event,this)" onblur="calAmount(event,this)" Text='<%# Eval("PaidAmount") %>'>
</asp:TextBox>
</ItemTemplate>
I have not any event related gridview making like, rowBound, CommandView. Could you please show me a way related this.
Thank you so much in advance.

How to set the column of Child gridview in Parent gridview column to be hyperlink

I am having the Nested Gridview in my Web form. Based on the Parent childgridview column value, child gridview column to be hyperlink otherwise the child gridview column should be normal text column.
I find the solution for my problem
<asp:TemplateField ItemStyle-Width="38%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div align="center">
<asp:HyperLink ID="hlnkDepartmentName" runat="server" Visible="false"></asp:HyperLink>
<asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") >'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
I placed the Hyperlink and Label field inside the Template Field in the Child Gridview. Based on the Parent Grid value, i turned On and OFF the Visibility of the hyperlink and label field inside the Gridview in the Child Gridview RowDatabound event. Tats it my problem solved.

.NET framework 4.5 loads default edit template on GridView?

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.

LinkButton display at unnecessary time

I have a LinkButton(LinkButton_x) in my aspx page. i'm using this LinkButton to check all boxes in CheckBoxList. when i clicked on this LinkButton(LinkButton_x), code in partial class is executing to select all the boxes in CheckBoxList.
also have several LinkButtons in a TemplateField of a GridView as below.
<asp:TemplateField HeaderText="">
<ItemTemplate>
LinkButton1 here
LinkButton2 here
</ItemTemplate>
<EditItemTemplate>
LinkButton3 here
LinkButton4 here
</EditItemTemplate>
<FooterTemplate>
LinkButton5 here
LinkButton6 here
</FooterTemplate>
</asp:TemplateField>
LinkButtons in TemplateFields FooterTemplate is normally displaying. but When i click on LinkButton_x in aspx page, LinkButtons in TemplateFields ItemTemplate also displaying. i want to avoid it.
how can i solve this ?
If you have Linkbutton in <ItemTemplate> and you don't have written any logic to hide the linkbutton, then it will obviously display it provided that grid contains atleast one row.
I think your problem is more related to binding of grid and its number of rows during postback. It may be the case that when you click on linkbutton then due to postback grid would be getting change.

Retain form data while paging through a GridView - ASP.NET

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.

Categories