Prevent GridView OnRowCommand to execute for specific gridview column Asp.net - c#

I have a GridView defined like this:
<asp:GridView ID="GridView_Messages" runat="server" AutoGenerateColumns="False" PageSize="5" CellPadding="4" AllowPaging="true" Width=100% AllowSorting="true" OnSorting="GridView_Messages_Sorting"
ForeColor="#333333" GridLines="None" OnRowDataBound="GridView_Messages_OnRowDataBound" OnPageIndexChanging="GridView_Messages_PageIndexChanged" OnRowCommand="GridView_Messages_OnRowCommand" EmptyDataText="<%$ Resources:Localization, NoMsg %>">
<%--CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"--%>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<HeaderTemplate>
<asp:CheckBox ID="allchk" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="check" Text="" TextAlign="Right" AutoPostBack="false" Checked="false" runat="server" OnCheckedChanged="GridView_Messages_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MessageID" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblMessageID" runat="server" Text='<%# Eval("MessageID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, Title2 %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.Title">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblTitle" runat="server" Text='<%# Eval("Title") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblDescription" runat="server" Text='<%# Eval("Description")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FromUserID" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblFromUserID" runat="server" Text='<%# Eval("FromUserID")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, From %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.UserName">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblFromUserName" runat="server" Text='<%# Eval("UserName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblNotes" runat="server" Text='<%# Eval("Notes") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, IsRead %>" Visible="false" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.IsRead">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblIsRead" runat="server" Text='<%# Eval("IsRead") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, TimeSent %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.SentDateTime">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblDateTime" runat="server" Text='<%# Eval("SentDateTime") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Msg_lnkBtnClk" runat="server" CommandName="SingleClick" Text="click"
Visible="true" CssClass="hidden"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Msg_lnkBtnDblClk" runat="server" CommandName="DoubleClick" Text="dblClick"
Visible="true" CssClass="hidden"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#C7B88D" />
</asp:GridView>
I don't want my OnRowCommand to execute for the first column of my GridView. Actually I don't want my event to fire when some of my checkboxes is clicked. How to determine if I have clicked on a checkbox or I have clicked somewhere else in a gridview? In WPF I could use e.OriginalSource property but here this is not possible.

Using JQuery:
If you haven't already added JQuery to your page, add this tag to your page's head section:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
And this is the actual code. Add this after the link to JQuery, in the .aspx page's head section:
<script type="text/javascript">
$(function(){
$('#<%= GridView_Messages.ClientID %> input[type="checkbox"]')
.click(function(event){
event.stopPropagation();
event.preventDefault();
})
});
</script>

Related

How can I add static text as Footer at the bottom of the GridView?

I am trying to add a Foot Note at the bottom of the Grid. but from the below code snippet, I am seeing footnote at the bottom but it is coming in the empty columns. I mean to say if I have 4 columns in the Grid, the Foot Note is displaying after the 4th column. this should not happen. Ideally, the footnote will starts from the first column until the end of the grid. (as a row)
Any suggestions, please?
<asp:Table ID="Table1" BorderWidth="0" CellPadding="0" CellSpacing="0" Width="100%" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:GridView ID="_gridView1" runat="server"
EnableViewState="false" ShowFooter="true" AllowPaging="false" EnableFixedHeader="true" Width="100%"
AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="12%" ItemStyle-Width="12%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate >
<asp:Label ID="_LineName" runat="server" Text='<%# Eval("LineName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity0" HeaderText="Entity0" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemTemplate>
<asp:Label ID="_entityZero" runat="server" Text='<%# Eval("EntityName0") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity1" HeaderText="Entity1" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityOne" runat="server" Text='<%# Eval("EntityName1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity2" HeaderText="Entity2" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityTwo" runat="server" Text='<%# Eval("EntityName2") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity3" HeaderText="Entity3" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityThree" runat="server" Text='<%# Eval("EntityName3") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity4" HeaderText="Entity4" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityFour" runat="server" Text='<%# Eval("EntityName4") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity5" HeaderText="Entity5" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityFive" runat="server" Text='<%# Eval("EntityName5") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity6" HeaderText="Entity6" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entitySix" runat="server" Text='<%# Eval("EntityName6") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity7" HeaderText="Entity7" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entitySeven" runat="server" Text='<%# Eval("EntityName7") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity8" HeaderText="Entity8" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityEight" runat="server" Text='<%# Eval("EntityName8") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity9" HeaderText="Entity9" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityNine" runat="server" Text='<%# Eval("EntityName9") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity10" HeaderText="Entity10" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityTen" runat="server" Text='<%# Eval("EntityName10") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="footer" HeaderStyle-HorizontalAlign="Left">
<FooterTemplate>
<asp:Label id="lblFooter" runat="server">'Last Review Date' indicates the date on which an Issuer Rating was last formally reviewed within a twelve-month period or when a Credit Rating Action was last published. <br /></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:TableCell>
</asp:TableRow>
</asp:Table>

How to retain multiple dropdown list's selected value even when page index is changing?

I'm a newbie in asp.net development. Any help would be much appreciated. :)
What I need to accomplish is to retain the selected value of my drop down lists in my gridview even when the user navigate to the next page index. I am thinking to put the values into session while page are changing and put it back when the page shows again. I have tried to do this in check box only and I don't know how to implement using drop down lists. I have 4 drop down lists by the way. Please help. Thanks a lot. Below is my code
<asp:GridView ID="gvwAssociation" runat="server" AutoGenerateColumns="False"
AllowSorting="True" HorizontalAlign="Left" AllowPaging="true" Height="75%" Width="100%" SkinID="TitleReviewGridViewSkin"
OnRowDataBound="gvwAssociation_RowDataBound" PageSize="20" OnPageIndexChanging="gvwAssociation_PageIndexChanging" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" Visible="true"/>
<asp:BoundField DataField="_fileName" HeaderText="File Name"/>
<asp:BoundField DataField="_uploadDate" HeaderText="Upload Date" DataFormatString="{0:MM-dd-yyyy}" />
<asp:TemplateField HeaderText="Pool">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<%--<asp:dropdownlist ID="ddlPool" runat="server" ReadOnly="false" Width="75px" ForeColor="Black" MaxLength="6" EnableViewState="true"></asp:dropdownlist> --%>
<asp:DropDownList ID="ddlpool" width="75px" runat="server" AutoPostBack="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<%--<asp:dropdownlist ID="ddlyear" runat="server" ReadOnly="false" Width="75px" ForeColor="Black" MaxLength="6" EnableViewState="true"></asp:dropdownlist>--%>
<asp:DropDownList ID="ddlyear" width="75px" runat="server" AutoPostBack="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plant">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<%-- <asp:dropdownlist ID="ddlplant" runat="server" DataTextField='<%# Bind("_plant") %>' DataValueField='<%# Bind("_plant") %>' ReadOnly="false" Width="150px" ForeColor="Black" MaxLength="6" EnableViewState="true" AppendDataBoundItems="true" ></asp:dropdownlist> --%>
<asp:DropDownList ID="ddlplant" width="135px" runat="server" AutoPostBack="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Event">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<%-- <asp:dropdownlist ID="ddlevent" runat="server" DataTextField='<%# Bind("_event") %>' DataValueField='<%# Bind("_plant") %>' ReadOnly="false" Width="150px" ForeColor="Black" MaxLength="6" EnableViewState="true" AppendDataBoundItems="true" ></asp:dropdownlist> --%>
<asp:DropDownList ID="ddlevent" width="135px" runat="server" AutoPostBack="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns> </asp:GridView>
Hope you binding a DataTable to GridView.
Save the Data Table which you are binding to the GridView and Update the Same DataTable something like beow.
gvwAssociation_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
DataTable dt = (DataTable)Session["SavedDataTable"];
foreach (GridViewRow gvRow in gvwAssociation.Rows)
{
DropDownList ddlpool = (DropDownList)gvRow.FindControl("ddlpool");
DropDownList ddlyear = (DropDownList)gvRow.FindControl("ddlyear");
if (dt.Select("ID=" + gvRow.Cells[1].Text).Length > 0)
{
dt.Select("ID=" + gvRow.Cells[1].Text)[0]["Pool"] = ddlpool.SelectedValue;
dt.Select("ID=" + gvRow.Cells[1].Text)[0]["Year"] = ddlyear.SelectedValue;
}
}
Session["SavedDataTable"] = dt;
gvwAssociation.PageIndex = e.NewPageIndex;
gvwAssociation.DataSource = dt;
gvwAssociation.DataBind();
}

Maintain scroll position of gridview inside a div

I have a Master Page which contain a Script Manager.In a child page i have a scrollable gridview with checkbox and some textboxes
<div id="grdScr" style ="width:130%; overflow:auto" align="center" >
<asp:GridView ID="dgvBill" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Horizontal" PageSize="100"
Width="150%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True"
oncheckedchanged="chkSelect_CheckedChanged" />
<asp:Label ID="lblVendCd" runat="server" Text='<%# Eval("vend_cd") %>'
Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bill date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("bill_dt") %>'></asp:Label>
<asp:Label ID="lbltrid" runat="server" Text='<%# Eval("tr_id") %>'
Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bill no">
<ItemTemplate>
<asp:Label ID="lblBillNO" runat="server" Text='<%# Eval("bill_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("vend_nm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supplier">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("supplier") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Driver">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("driver") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vechicle Type">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("comm_nm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vehicle No">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("veh_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payable Amount">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Eval("payable") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cash">
<ItemTemplate>
<asp:TextBox ID="txtCashGrid" runat="server" AutoPostBack="True"
Enabled="False" ontextchanged="txtCashGrid_TextChanged"
style="text-align: right" Width="75px" onkeypress="return isNumberKey(event)">0.00</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque">
<ItemTemplate>
<asp:TextBox ID="txtChequeGrid" runat="server" AutoPostBack="True"
Enabled="False" ontextchanged="txtChequeGrid_TextChanged"
style="text-align: right" Width="75px" onkeypress="return isNumberKey(event)">0.00</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bank">
<ItemTemplate>
<asp:TextBox ID="txtBankNameGrid" runat="server" Width="174px"
AutoPostBack="True" ontextchanged="txtBankNameGrid_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque No">
<ItemTemplate>
<asp:TextBox ID="txtChqNoGrid" runat="server" Width="75px" AutoPostBack="True"
ontextchanged="txtChqNoGrid_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque Date">
<ItemTemplate>
<asp:TextBox ID="txtChDateGrid" runat="server" AutoPostBack="True"
EnableTheming="True" Width="97px"
ontextchanged="txtChDateGrid_TextChanged"></asp:TextBox>
<cc1:TextBoxWatermarkExtender ID="txtChDateGrid_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtChDateGrid"
WatermarkText="dd-MMM-yyyy">
</cc1:TextBoxWatermarkExtender>
<cc1:CalendarExtender ID="txtChDateGrid_CalendarExtender" runat="server"
Enabled="True" Format="dd-MMM-yyyy" TargetControlID="txtChDateGrid">
</cc1:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#CCCCCC" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
</div>
My gridview is inside an update panel. The textboxes are doing some mathematical calculations in side the gridview in their TextChange Event and autopostback is true for them.
Whenever I am pressing tab for moving the cursor to the next textbox the page gets postbacked and the scroll position is not maintained. How will I solve this?

Maintain a scrollable gridview position on postback inside update panel

I have a gridview with overflow-y:auto. The gridview is inside an update panel which is wrapped in a user control. This usercontrol is displayed using the ajax modal pop up extender. Now, this gridview could have hundreds of items, if a user would have to select an item, then the async postback reloads the popup again and the scroll position is lost. I tried with the solution posted on the link http://weblogs.asp.net/andrewfrederick/archive/2008/03/04/maintain-scroll-position-after-asynchronous-postback.aspx as well but no luck. Not sure if I am misplacing the javascript code.
This is where I have the modal pop up extender.
<asp:Panel ID="pnlShowDirectMailMedium" runat="server" Visible="false" HorizontalAlign="Center">
<uc2:DirectMailMedium ID="ucDirectMailMedium" runat="server" OnCloseEvent="ucDirectMailMedium_CloseEvent" />
<asp:HiddenField ID="hfCloseDMMedium" runat="server" />
</asp:Panel>
<cc1:ModalPopupExtender ID="mpeDirectMailMedium" runat="server" BackgroundCssClass="Modal"
TargetControlID="hfCloseDMMedium" PopupControlID="pnlShowDirectMailMedium" OkControlID="hfCloseDMMedium"
CancelControlID="hfCloseDMMedium" Drag="true" RepositionMode="None" />
This is the actual user control where i have the gridview.
<asp:Panel ID="pnlDirectMail" runat="server">
<asp:UpdatePanel ID="upDirectMail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="ZipCodeGrid" id="divGrid">
<asp:GridView ID="gvZips" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvZips_RowDataBound"
GridLines="None" Width="870px">
<Columns>
<asp:TemplateField HeaderText="Zip">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:HiddenField ID="hfSelectedId" runat="server" Value='<%# Bind("SelectedId") %>' />
<asp:HiddenField ID="hfID" runat="server" Value='<%# Bind("ID") %>' />
<asp:Label ID="lblZip" runat="server" Text='<%# Bind("Zip") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lblDescription" runat="server" Text="City" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lblSegmentDescHeader" runat="server" Text="Segment" Visible="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblSegment" runat="server" Text='<%# Bind("SegmentDesc") %>' Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lbl1Acre50KHeader" runat="server" Text="1 Acre (50K)" Visible="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl1Acre50KQty" runat="server" Text='<%# Bind("One50K") %>' Width="50px"
Visible="false" />
<asp:CheckBox ID="chk1Acre50K" runat="server" Checked='<%# Bind("One50KSelected") %>'
OnCheckedChanged="chk1Acre50K_CheckChanged" AutoPostBack="true" Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lbl1Acre50KPriceHeader" runat="server" Text="Cost" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl1Acre50KCost" runat="server" Text='<%# Bind("One50KCost", "{0:$###,###,##0.00}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lbl1Acre100KHeader" runat="server" Text="1 Acre (100K)" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl1Acre100KQty" runat="server" Text='<%# Bind("One100K") %>' Width="50px"
Visible="false" />
<asp:CheckBox ID="chk1Acre100K" runat="server" Checked='<%# Bind("One100KSelected") %>'
OnCheckedChanged="chk1Acre100K_CheckChanged" AutoPostBack="true" Visible="false" />
<asp:HiddenField ID="hf1Acre100KCost" runat="server" Value='<%# Bind("One100KCost", "{0:$###,###,##0.00}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lbl2Acre50KHeader" runat="server" Text="Prospect 2Acre+" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl2Acre50KQty" runat="server" Text='<%# Bind("Two50K") %>' Width="50px" />
<asp:CheckBox ID="chk2Acre50K" runat="server" Checked='<%# Bind("Two50KSelected") %>'
OnCheckedChanged="chk2Acre50K_CheckChanged" AutoPostBack="true" />
<asp:HiddenField ID="hf2Acre50KPrice" runat="server" Value='<%# Bind("Two50KCost", "{0:$###,###,##0.00}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lbl2Acre100KHeader" runat="server" Text="Prospect 3Acre+" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl2Acre100KQty" runat="server" Text='<%# Bind("Two100K") %>' Width="50px" />
<asp:CheckBox ID="chk2Acre100K" runat="server" Checked='<%# Bind("Two100KSelected") %>'
OnCheckedChanged="chk2Acre100K_CheckChanged" AutoPostBack="true" />
<asp:HiddenField ID="hf2Acre100KPrice" runat="server" Value='<%# Bind("Two100KCost", "{0:$###,###,##0.00}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lbl3Acre50KHeader" runat="server" Text="3 Acre (50K)" Visible="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl3Acre50KQty" runat="server" Text='<%# Bind("Three50K") %>' Visible="false"
Width="50px" />
<asp:CheckBox ID="chk3Acre50K" runat="server" Checked='<%# Bind("Three50KSelected") %>'
OnCheckedChanged="chk3Acre50K_CheckChanged" AutoPostBack="true" Visible="false" />
<asp:HiddenField ID="hf3Acre50KPrice" runat="server" Value='<%# Bind("Three50KCost", "{0:$###,###,##0.00}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lbl3Acre100KHeader" runat="server" Text="3 Acre (100K)" Visible="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl3Acre100KQty" runat="server" Text='<%# Bind("Three100K") %>' Width="50px"
Visible="false" />
<asp:CheckBox ID="chk3Acre100K" runat="server" Checked='<%# Bind("Three100KSelected") %>'
OnCheckedChanged="chk3Acre100K_CheckChanged" AutoPostBack="true" Visible="false" />
<asp:HiddenField ID="hf3Acre100KPrice" runat="server" Value='<%# Bind("Three100KCost", "{0:$###,###,##0.00}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:LinkButton ID="lblOccupiedHousingUnit" runat="server" Text="Housing Unit" Visible="false"
ForeColor="White" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtOccupiedHousingUnit" runat="server" Text='<%# Bind("OccupiedHousingUnit") %>'
Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:LinkButton ID="lblHouseholdIncome" runat="server" Text="Household Income" Visible="false"
ForeColor="White" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtHouseholdIncome" runat="server" Text='<%# Bind("HouseholdIncome", "{0:$###,###,##0.00}") %>'
Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:LinkButton ID="lblDistancetoSite" runat="server" Text="Distance" Visible="false"
ForeColor="White" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtDistancetoSite" runat="server" Text='<%# Bind("DistancetoSite") %>'
Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:LinkButton ID="lblAverageIncome" runat="server" Text="Home Value" Visible="false"
ForeColor="White" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtAverageIncome" runat="server" Text='<%# Bind("AverageIncome", "{0:$###,###,##0.00}") %>'
Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lblRuralPct" runat="server" Text="% Rural" Visible="false" ForeColor="White" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtRuralPct" runat="server" Text='<%# Bind("RuralPct") %>' Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lblLawnGarden" runat="server" Text="Lawn & Garden" Visible="false"
ForeColor="White" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtLawnGarden" runat="server" Text='<%# Bind("LawnGarden") %>' Visible="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<HeaderTemplate>
<asp:Label ID="lblLawnGardenEquipment" runat="server" Text="Lawn & Garden Equipment"
Visible="false" ForeColor="White" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="txtLawnGardenEquipment" runat="server" Text='<%# Bind("LawnGardenEquipment") %>'
Visible="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="HeaderFreez" />
<AlternatingRowStyle BackColor="#E4E4E4" />
<RowStyle BackColor="#F7F7F7" />
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Could someone give me an insight on what to do. FYI - The scriptmanager is on the master page.
You need to do like this ,
full detail : Maintain Scrollbar Position Inside UpdatePanel After Partial PostBack
You need to write down javascript which is part of image for maintaining scroll position
Since your gridview is located inside the user control that would be the appropriate place to add the code for maintaining the scroll position.
One important thing worth mentioning is that the javascript needs to be executed once the DOM is fully loaded, this can be done as follows:
Use #Pranay Rana's method and place the script after the ScriptManager control
Add a reference to jQuery and use $(document).ready()...
Here's a simple example of how this can be done, I'm sure you can modify this accordingly to work for your scenario :-)
ASPX:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs"
Inherits="WebApplication16.UserControl1" %>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
xPos = $get('scrollDiv').scrollLeft;
yPos = $get('scrollDiv').scrollTop;
}
function EndRequestHandler(sender, args) {
$get('scrollDiv').scrollLeft = xPos;
$get('scrollDiv').scrollTop = yPos;
}
});
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="scrollDiv" style="overflow:auto;height: 100px;" />
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:BoundField DataField="Name" />
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
public partial class UserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
gvEmployees.DataSource = new List<Employee>
{
new Employee{Name="Employee 1"},
new Employee{Name="Employee 2"},
new Employee{Name="Employee 3"},
new Employee{Name="Employee 4"},
new Employee{Name="Employee 5"},
new Employee{Name="Employee 6"},
new Employee{Name="Employee 7"},
};
gvEmployees.DataBind();
}
}
}
public class Employee
{
public string Name { get; set; }
}

asp.net using c# gridview sorting

i have a asp.net grid
<asp:GridView ID="GrdUsers" runat="server" AutoGenerateColumns="False"
Width="95%" BorderWidth="1px" BorderColor="Black" PagerStyle-CssClass="pgr" GridLines="Horizontal"
CssClass="DataGridStyle" AllowPaging="True" OnRowCommand="GrdUsers_RowCommand"
OnRowDeleting="GrdUsers_RowDeleting" OnRowEditing="GrdUsers_RowEditing" AllowSorting="true" OnSorting="gridView_Sorting"
OnPageIndexChanging="GrdUsers_PageIndexChanging">
<%--<EmptyDataRowStyle--%>
<EmptyDataRowStyle CssClass="pgr" />
<EmptyDataTemplate>
<asp:Label ID="lblNorecId" CssClass="notification error" runat="server" Text="No records were found using your search criteria."
Font-Bold="true" ForeColor="Brown" Font-Names="Arial" Font-Size="Medium"></asp:Label>
</EmptyDataTemplate>
<Columns>
<%-- <asp:BoundField HeaderText="Name" DataField="User_Fullname" />
<asp:BoundField HeaderText="City" DataField="User_city" />
<asp:BoundField HeaderText="State" DataField="User_state" />
<asp:BoundField HeaderText="Zip" DataField="User_zip" />
<asp:BoundField HeaderText="Joining Date" DataField="User_joiningdate" />--%>
<asp:BoundField DataField="catId" HeaderText="Categery Id" Visible="False" />
<asp:BoundField DataField="MasterCName" HeaderText="Master Categery" />
<asp:BoundField DataField="CatName" HeaderText="Sub Categery" />
<asp:BoundField DataField="GearID" HeaderText="GearID" Visible="False" />
<asp:BoundField DataField="GearName" HeaderText="Gear" />
<asp:TemplateField HeaderText="RentHourly" SortExpression="RentHourly">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text="$"></asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RentHourly") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rentdaily" SortExpression="Rentdaily">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text="$"></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Rentdaily") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RentWeekend" Visible="false">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text="$" Visible="false"></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("RentWeekend") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RentWeekly" Visible="false">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text="$" Visible="false"></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("RentWeekly") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" CommandName="edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "GearID") %>'
ImageUrl="~/img/icons/16/edit_icon.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDel" runat="server" CommandName="delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "GearID") %>'
ImageUrl="~/img/icons/16/cancel.png" />
<cc1:ConfirmButtonExtender ID="btnDel_ConfirmButtonExtender" runat="server" ConfirmText="Do You Want To Delete?"
Enabled="True" TargetControlID="btnDel">
</cc1:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPrevious" NextPageText="Next" PreviousPageText="Last" />
<RowStyle HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#DBDBDB" ForeColor="Black" HorizontalAlign="Center" />
</asp:GridView>
now i want to sort them renthourly and rentdaily in ascending and descending order
Looks like this is an old question, but your best bet is to use an ObjectDataSource to bind your grid.
Once you use the ObjectDataSource, you can have it automatically supply the SortDirection, SortColumn, CurrentPage and PageSize to the SelectMethod you have specified on the ObjectDataSource.
Inside this business method (your SelectMethod) you can cleanly issue the appropriate command to query only the data needed for the current page and apply the appropriate sorting expression.

Categories

Resources