I have an UpdatePanel and a GridView within it. I have a problem with paging: when I press Next button, grdUsers_PageIndexChanging() is not called, so the page stays 1, and if previously I changed selection in ddlPageSize, now it goes back to the initial selection.
Also, if I am on the first page, the Previous button is not disabled, and pressing it causes Out Of Range error.
What am I doing wrong?
Thanks.
<asp:UpdatePanel ID="upnlUsers" runat="server" ChildrenAsTriggers="true" UpdateMode="Always" >
<ContentTemplate>
<div style="height:400px; width:1500px; overflow:auto;">
<asp:GridView ID="grdUsers" runat="server" AllowPaging="True" ShowHeader="false" ShowFooter="true"
AutoGenerateColumns="false" CssClass="largegridview largegridview_td"
Width="1480px" Height="100%" PageSize="100" DataKeyNames="ID" EnableSortingAndPagingCallbacks="false"
onpageindexchanging="grdUsers_PageIndexChanging"
onrowdatabound="grdUsers_RowDataBound">
<AlternatingRowStyle CssClass="alternatingrowstyle" />
<Columns>
<asp:TemplateField HeaderText="User Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="Email">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pagerstyle" />
<PagerTemplate>
<asp:Label ID="Label1" runat="server" Text="Show rows:" />
<asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged">
<asp:ListItem Value="20" />
<asp:ListItem Value="50" />
<asp:ListItem Value="100" />
</asp:DropDownList>
Page
<asp:TextBox ID="txtGoToPage" runat="server" AutoPostBack="true"
OnTextChanged="GoToPage_TextChanged" CssClass="gotopage" />
of
<asp:Label ID="lblTotalNumberOfPages" runat="server" />
<asp:Button ID="btnPrev" runat="server" CommandName="Page"
ToolTip="Previous Page" CommandArgument="Prev" CssClass="previous" />
<asp:Button ID="btnNext" runat="server" CommandName="Page" ToolTip="Next Page"
CommandArgument="Next" CssClass="next" />
</PagerTemplate>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Agreed, I tested the code, and grdUsers_PageIndexChanging() is called on mine.
Dear Friend
their is in build paging in the gridview if you want your own paging style then you have to implement your own paging through PageDataSource Class.
Click to view code
this link provide you how to implement the paging in the Datalist and Repeater through same way you will implement the paging in grid view .
Then no pageindex change event will fired in the gridview.
I don't think you are doing anything wrong; it may not actually fire the PageIndexChanged event, it probably fires the RowCommand event. Attach to the ItemCommand event and that will receive the event.
HTH.
Related
I have a gridView that I wish to add a textbox to. I want the textbox to be located in the footer, but I have no idea how to do that.
This is my gridview:
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false"
AllowSorting="True"
ShowFooter="True"
ShowHeaderWhenEmpty="true"
OnRowDataBound="gvTest_RowDataBound"
Width="550px">
<EmptyDataTemplate>
No data.
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="Name" headerText="Name"/>
<asp:TemplateField>
<HeaderTemplate>
Actions
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/Search.png" Width="16" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I've seen people talking about adding something like this:
<FooterTemplate>
<asp:TextBox ID="tbName" runat="server"/>
</FooterTemplate>
But I dont know where or how to add it. If I just put it in between the <asp:TemplateField>tags it messes up (probably because my boundfields).
Additional information:
I bind the gridview using DataTable. The real table has more columns, but this will suffice for an example.
Switch from:
<asp:TemplateField>
<HeaderTemplate>
Actions
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/Search.png" Width="16" />
</ItemTemplate>
</asp:TemplateField>
to:
<asp:TemplateField>
<HeaderTemplate>
Actions
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/Search.png" Width="16" />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
It's easy if you understand, that you can devide your TemplateField into three sections:
Header
Item(or main content)
Footer
Greetz
You will need to put it in the template field.. like this..
<asp:TemplateField HeaderText="BankName" SortExpression="BankName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BankName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("BankName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddBankName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
this is an example from my working code.
I have multiple gridviews on one page. For each gridview, there are two ways to insert a new record. One is through FooterTemplate of a TemplateField when the gridview has data and the other through EmptyDataTemplate when the gridview is empty.
By itself, the inserts work well. However, if another gridview is empty, then by default, the insert portion in its EmptyDataTemplate is displayed. When this is displayed, I can't get the FooterTemplate of another gridview to display. That portion is inaccessible until I get rid of the EmptyDataTemplate by forcing an insert.
<asp:Button ID="btnAddNewDomSoilType" runat="server" Text="Add new record" CssClass="btnNewRecord" OnClick="addNewDomSoilType" />
<asp:GridView ID="gvDomSoilType" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="dsDomSoilType" CssClass="data"
AllowSorting="True" onrowcommand="gvDomSoilType_RowCommand" DataKeyNames="HabitatObsSubstrateID, HabitatObservationID, SubstrateID" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure?');"
AlternateText="Delete" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertSubmit" runat="server" Text="Submit" CommandName="SubmitInsert" CssClass="btnInsertSEID" />
<asp:Button ID="btnInsertCancel" runat="server" Text="Cancel" CommandName="CancelInsert" CssClass="btnInsertSEID" CausesValidation="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Substrate" SortExpression="Substrate">
<FooterTemplate>
<asp:DropDownList ID="ddlSoilType" runat="server"
DataSourceID="dsDomSoilType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Substrate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label4" runat="server" Text="Substrate: " CssClass="insertLabel"></asp:Label>
<asp:DropDownList ID="ddlSoilType2" runat="server"
DataSourceID="dsDomSoilType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnInsertEmpty" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
<asp:Button ID="btnAddNewVegCover" runat="server" Text="Add new record" CssClass="btnNewRecord" OnClick="addNewVegCover" />
<asp:GridView ID="gvVegCover" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="dsVegCover" CssClass="data"
AllowSorting="True" onrowcommand="gvVegCover_RowCommand" DataKeyNames="HabitatObsVegID">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure?');"
AlternateText="Delete" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertSubmit" runat="server" Text="Submit" CommandName="SubmitInsert" CssClass="btnInsertSEID" />
<asp:Button ID="btnInsertCancel" runat="server" Text="Cancel" CommandName="CancelInsert" CssClass="btnInsertSEID" CausesValidation="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HabitatVegetationType" SortExpression="HabitatVegetationType">
<FooterTemplate>
<asp:DropDownList ID="ddlVegType" runat="server"
DataSourceID="dsVegType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblVegType" runat="server" Text='<%# Bind("HabitatVegetationType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VegetationTypePercent" SortExpression="VegetationTypePercent">
<FooterTemplate>
<asp:TextBox ID="txtVegTypePercent" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblVegTypePercentage" runat="server" Text='<%# Bind("VegetationTypePercent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label2" runat="server" Text="Vegetation Type:" CssClass="insertLabel"></asp:Label>
<asp:DropDownList ID="ddlVegType2" runat="server"
DataSourceID="dsVegType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label5" runat="server" Text="Percenttage:" CssClass="insertLabel"></asp:Label>
<asp:TextBox ID="txtVegTypePercent2" runat="server"></asp:TextBox>
<asp:Button ID="btnInsertEmpty" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
Code behind:
protected void addNewDomSoilType(object sender, EventArgs e)
{
gvDomSoilType.ShowFooter = true;
}
protected void addNewVegCover(object sender, EventArgs e)
{
gvVegCover.ShowFooter = true;
}
I know my explanation is a bit confusing. Let me know what doesn't make sense and I'll try to clarify. Thanks for looking.
These two gridviews shouldn't impact each other. I think it might be the PostBack that is messing up the state of the webpage. On the PageLoad do Page.IsPostBack false and then populate the data. Make sure the events are hooked up to the right GridView.
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'
Can anyone please help me to validate Textbox within Footer of the GridView Control upon Clicking the Button
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
DataKeyNames="Id" ShowFooter="true" onrowdatabound="GridView1_RowDataBound" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Units
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txt_Units" runat="server" Text='<%# Eval("Units") %>' ToolTip="Enter Units"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ReadOnly="true" Text="999999" Enabled="false" ID="txt_MaxUnits" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Cost
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txt_Cost" runat="server" Text='<%# Eval("Cost") %>' ></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_MaxCost" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter Integer Between 0 to 999998" ControlToValidate="txt_Units" ValidationExpression="^(0{0,5}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9]| [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9])$" ValidationGroup="vld_Rows"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Enter Integer Between 0 to 999998" ControlToValidate="txt_Cost" ValidationExpression="^(0{0,5}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9]| [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9])$" Display="Dynamic" ValidationGroup="vld_Rows"></asp:RegularExpressionValidator>
</ItemTemplate>
<FooterTemplate>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Enter Integer Between 0 to 999998" ControlToValidate="txt_MaxCost" ValidationExpression="^(0{0,5}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9]| [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9])$" ValidationGroup="vld_Rows1" ></asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<%--<asp:TemplateField>
<HeaderTemplate>
Is Max
</HeaderTemplate>
<ItemTemplate>--%>
<%-- <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("IsMax") %>'></asp:TextBox>--%>
<%-- <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("IsMax") %>' OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" ValidationGroup="vld_Rows" />
<asp:Button ID="btn_Save" runat="server" Text="Save" onclick="btn_Save_Click" ValidationGroup="vld_Rows1" />
</ContentTemplate>
<%--<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" runat="server" />
<asp:AsyncPostBackTrigger ControlID="CheckBox1" EventName="CheckedChanged" runat="server" />
</Triggers>--%>
</asp:UpdatePanel>
Above is the Sample Code Which I have used. Upon Clicking Button(btn_Save) I need to validate the ValidateGroup with id as "vld_Rows1"
Please Suggest a Solution.
Thanks in Advance!
Put the validation control in the same template where you put your control. In your scenario, your controls are in the footer template, so you should put the validation controls in footer template, and try to use validation group as well. e.g.
<asp:TemplateField>
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ValidationGroup="ft" ControlToValidate="TextBox3"
ErrorMessage="*"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
Checkout the following MSDN article: RegularExpressionValidator Control
From the article:
If the input control is empty, no validation functions are called and
validation succeeds. Use a RequiredFieldValidator control to prevent
the user from skipping an input control.
As it states you will need to make use of a RequiredFieldValidator to ensure that data has been entered.
So I have an ASP.NET grid view:
<asp:GridView ID="gvReferences" runat="server" AutoGenerateColumns="False" ShowHeader="False"
OnRowEditing="gvReferences_RowEditing"
OnRowDeleting="gvReferences_RowDeleting" onrowcreated="gvReferences_RowCreated">
<Columns>
<asp:TemplateField ItemStyle-Width="400px">
<ItemTemplate>
<asp:Label ID="lblId" Visible="false" runat="server" Text='<%# Eval("Id") %>' />
<asp:Label ID="lblAssociatedSpecies" runat="server" Text='<%# Eval("Text") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblKind" runat="server" Text='<%# Eval("Kind") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" DeleteText="delete" ShowDeleteButton="True"
ShowCancelButton="False" EditText="edit" ShowEditButton="True">
<ControlStyle Width="60px" />
</asp:CommandField>
</Columns>
</asp:GridView>
I'd like to attach some JavaScript to the Delete command button to ask for confirmation before a row is deleted.
Any ideas?
You could always use a TemplateField rather than the CommandField.
<asp:TemplateField>
<ItemTemplate>
<asp:Button name="btnDelete" commandName="Delete" OnClientClick="return confirm('Delete
this Item');" Text="Delete" runat="server" />
<asp:Button name="btnEdit" commandName="Edit" Text="Edit" runat="server" />
</ItemTemplate>
</asp:TemplateField>
When I've done this I've used a Template Field with the ConfirmButtonExtender from the Ajax Control Toolkit.
<asp:TemplateField>
<ItemTemplate>
<asp:Button name="DeleteButton" commandName="Delete" Text="Delete" runat="server" />
<ajaxToolkit:ConfirmButtonExtender TargetControlId="DeleteButton" ConfirmText="Delete this entry?" />
</ItemTemplate>
</asp:TemplateField>
This is a javascript for delete confirmation.
function not_check1()
{
var where_to1= confirm("Do you really want to delete this record??");
if (where_to1 == true)
{
return true;
}
else
{
return false;
}
}
This is a gridview field from where you call the javascript.
<asp:TemplateColumn ItemStyle-Width="20" >
<ItemTemplate>
<asp:ImageButton ID="ib_delete" runat="server" ImageUrl="~/image/images.jpg" commandName="Delete" OnClientClick="return not_check1();" ImageAlign="Middle"/></ItemTemplate>
</asp:TemplateColumn>
In RowDataBound add -
LinkButton objDelete = e.Row.Cells[0].Controls[0] as LinkButton;
objDelete.Attributes.Add("onclick", "javascript:return confirm('Do you want to delete this item?');");