Maintain a scrollable gridview position on postback inside update panel - c#

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; }
}

Related

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

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>

Nested Gridview Footer Textbox Required Field Validation Firing for Each Row on Nested Gridview Footer Button Click

I am trying to get the required field validation to fire only for the specific row when the add button is clicked. But right now, when I click any add button, it fires the required field validation for all nested grid rows. The code for the nested gridview and the image of the gridviews is the following. I also tried to use validation groups, attempting to get unique validation groups for each nested gridview row, but couldn't get it to work.
<asp:GridView ID="GrdXML" runat="server" AllowSorting="false"
AutoGenerateColumns="False" CellPadding="0"
ForeColor="#333333" GridLines="Horizontal" onrowdeleting="GrdXML_RowDeleting" OnRowDataBound="OnRowDataBound"
ShowFooter="false" CssClass="xmlgrid" OnSorting="GrdXML_Sorting" DataKeyNames="ContractRecordID">
<Columns>
<asp:TemplateField HeaderText="Contract Start Date" SortExpression="ContractStartDate">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ContractStartDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Contract End Date" SortExpression="ContractEndDate">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ContractEndDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete" runat="server"
CommandArgument='<%# Eval("ContractRecordID") %>' CommandName="Delete"
onclick="BtnDelete_Click" Text="Delete" Width="60px" />
<ajaxToolkit:ConfirmButtonExtender ID="BtnDelete_ConfirmButtonExtender"
runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True"
TargetControlID="BtnDelete">
</ajaxToolkit:ConfirmButtonExtender>
</ItemTemplate>
<ItemStyle Width="40px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal runat="server" ID="lit1" Text="<tr id='trGrid'><td colspan='100%'>" />
<asp:GridView ID="projectCodeGridView" runat="server" AllowSorting="false" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" AutoGenerateColumns="false" ShowFooter="true" OnRowCommand="ProjectCodeGridView_RowCommand" OnRowCreated="ProjectCodeGridView_RowCreated" OnRowDataBound="ProjectCodeGridView_RowDataBound" OnRowDeleting="ProjectCodeGridView_RowDeleting" DataKeyNames="ContractProjectCodeID" BorderStyle="None" BorderWidth="0">
<Columns>
<asp:TemplateField HeaderText="Project Codes" SortExpression="ContractProjectCode" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ContractProjectCode") %>' CssClass="projectcodelabel"></asp:Label>
</ItemTemplate>
<ItemStyle Width="100%" />
<FooterTemplate>
<asp:TextBox ID="projectCodeTextBox" runat="server" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="projectCodeRequiredFieldValidator" runat="server"
ControlToValidate="projectCodeTextBox" ErrorMessage="Project Code Required" ForeColor="Red" Display="Static" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>' Enabled="false"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="100%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnDelete1" runat="server"
CommandArgument='<%# Eval("ContractProjectCodeID") %>' CommandName="Delete"
onclick="BtnDelete1_Click" Text="Delete" Width="60px" />
<ajaxToolkit:ConfirmButtonExtender ID="BtnDelete1_ConfirmButtonExtender"
runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True"
TargetControlID="BtnDelete1">
</ajaxToolkit:ConfirmButtonExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnAdd" runat="server" onclick="BtnAdd_Click" Text="Add" Width="60px" ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>' CommandName="Add" />
</FooterTemplate>
<FooterStyle Width="100%" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#F7F6F3" Font-Bold="True" />
</asp:GridView>
<asp:Literal runat="server" ID="lit2" Text="</td></tr>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="Black" CssClass="padding" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" CssClass="padding" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
For the validation control of each row you are adding the ValidationGroup='<%# "PC" + Eval("ContractProjectCodeID") %>'. As you are saying, that the validation triggers for the wrong row, it could be that ContractProjectCodeID may not be unique for each row. I think what you can do is add row index to the validation group
ValidationGroup='<%# "PC" + Container.DataItemIndex + Eval("ContractProjectCodeID")

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?

Gridview WebUserControl access selected row

i have a grid view in the web custom control and want to select any row and so that i can edit that row. the edit button is on the master page and default.aspx is inherited from that master and know i want the row id on my defult page so that i can edit that row easily.
my web user control is
<asp:GridView runat="server" ID="grvBranches" GridLines="None"
AutoGenerateColumns="False" CellPadding="5"
OnRowDataBound="grvBranches_RowDataBound"
OnRowCancelingEdit="grvBranches_RowCancelingEdit"
OnRowEditing="grvBranches_RowEditing"
OnRowUpdating="grvBranches_RowUpdating">
<SelectedRowStyle BackColor="#d8d8d8" />
<HeaderStyle BackColor="#d8d8d8" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label Text="BranchName" ID="lblHeaderBranchName" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("ID") %>' ID="lblID" Visible="false" runat="server" />
<asp:Label Text='<%# Eval("Branch_Name") %>' ID="lblBranchName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label Text="OrganizationName" ID="lblHeaderOrganizationName" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("Organization_Name") %>' ID="lblOrganizationName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label Text="Address" ID="lblHeaderAddress" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("Address_1") %>' ID="lblAddress" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label Text="City" ID="lblHeaderCityName" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("City_Name") %>' ID="lblCityName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
and i want this rowid in on my Default page
On your master page .cs, on the EditButton click event
GridView gView = ContentPlaceHolder1.FindControl("GridView1") as GridView;
// use gView.SelectedIndex to manipulate the row, edit it, etc
Assuming your main ContentPlaceHolder's ID is 'ContentPlaceHolder1'

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