I would like to validate columns in an asp:Gridview but am unsure how to do so as all the tutorials I have found show a previous version on Microsoft Visual Studio (I am using 2010).
How do I validate each column of the Gridview?
Thanks
This is the code of my Gridview generated by visual studio.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="registrationNo" DataSourceID="SqlDataSource3"
onselectedindexchanged="GridView2_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="fullName" HeaderText="fullName"
SortExpression="fullName" />
<asp:BoundField DataField="address" HeaderText="address"
SortExpression="address" />
<asp:BoundField DataField="work" HeaderText="work"
SortExpression="work" />
<asp:BoundField DataField="home" HeaderText="home" SortExpression="home" />
<asp:BoundField DataField="mobile" HeaderText="mobile"
SortExpression="mobile" />
<asp:BoundField DataField="registrationNo" HeaderText="registrationNo"
InsertVisible="False" ReadOnly="True" SortExpression="registrationNo" />
<asp:ButtonField ButtonType="Button" CommandName="Edit" HeaderText="Edit"
ShowHeader="True" Text="Edit" />
<asp:ButtonField ButtonType="Button" CommandName="Update" HeaderText="Update"
ShowHeader="True" Text="Update" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete"
ShowHeader="True" Text="Delete" />
</Columns>
<HeaderStyle BorderColor="#33CC33" />
<RowStyle BorderStyle="Double" Font-Names="Monotype Corsiva" />
</asp:GridView>
//native asp.net clientside validation (ATLAS Ajax)
Instead of using BoundFields you should use TemplateField. This will allow you to add Validation controls in the field i.e.
Tutorial from 2007 which is still pertinent to this question
//serverside validation
In order to validate the values on the server side you have a couple of options:
a) add a CustomValidator pointing to codebehind server validation method i.e. msdn doc
b) on the gridviews RowUpdating method you could validate manually (this would also be for batch updates)
Related
I am using Visual Studio 2015 and Entity Framework 6. I have a gridview displaying orders from a database. However, I need a user to be able to click a row and be taken to another page for editing that row after a dialog box confirmation.
This is what I have:
<asp:GridView ID="gridOrders" runat="server" Height="184px" Width="1359px" AutoGenerateColumns="false"
AllowSorting="true" >
<HeaderStyle Font-Bold="true" Font-Size="16pt" BackColor="#cc0000" ForeColor="Black" />
<RowStyle Font-Size="12pt" BackColor="#afadad" ForeColor="White"/>
<AlternatingRowStyle BackColor="#afadad" ForeColor="White" />
<Columns>
<asp:CommandField HeaderText="" SelectText="CANCEL ORDER" ShowSelectButton="true" ControlStyle-ForeColor="White" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
How do I make the row selection to another page happen with a dialog that asks user if they are sure?
Change your aspx page with the below code
<asp:GridView ID="gridOrders" runat="server" Height="184px" Width="1359px" AutoGenerateColumns="False"
AllowSorting="True">
<HeaderStyle Font-Bold="true" Font-Size="16pt" BackColor="#cc0000" ForeColor="Black" />
<RowStyle Font-Size="12pt" BackColor="#afadad" ForeColor="White" />
<AlternatingRowStyle BackColor="#afadad" ForeColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkCancelOrder" runat="server" OnClientClick="return confirm('Are you sure to redirect?')" OnClick="lnkCancelOrder_Click" CausesValidation="False" CommandArgument='<%#Eval("OrderID") %>' CommandName="Select" Text="CANCEL ORDER"></asp:LinkButton>
</ItemTemplate>
<ControlStyle ForeColor="White" />
</asp:TemplateField>
<asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
</Columns>
</asp:GridView>
Write the c# code as follows
You can redirect to another page and pass the orderID as QueryString, and retrieve the complete order information by orderID and show that in an edit mode form
protected void lnkCancelOrder_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
string orderID = lnk.CommandArgument;
Response.Redirect("AnotherPage.aspx?orderId="+orderID);
}
Write under the <asp:TemplateField> of Gridview as
<asp:LinkButton ID="anchrTag" runat="server" PostBackUrl="Your edit page url" OnClientClick="return confirm('Are u sure to leave this page and want to go for edit?');">Edit</asp:LinkButton>
I have a grid view which display records by reading the data from a local database. I want to display a button called view with each record displayed. Simply meaning in each row there should be an asp.net button called view Details.
Here is my grid view code:
<asp:GridView ID="ItemView" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CssClass="list" DataKeyNames="ItemID"
DataSourceID="ADDataSource" GridLines="Both" Width="215px">
<Columns>
<asp:BoundField DataField="itemID" HeaderText="Item ID" />
<asp:BoundField DataField="Item Name" HeaderText="Item Name" />
<asp:BoundField DataField="Item Brand" HeaderText="Item Brand" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
I have added a button to delete records and edit records through common field property, but how do i add my own button called view to display in each row as the other 2 buttons.
Thank you.
I managed do it myself.
<asp:BoundField DataField="topic" HeaderText="Topic Name" />
<asp:BoundField DataField="Brand" HeaderText="Vehicle Brand" />
<asp:BoundField DataField="Model" HeaderText="Vehicle Model" />
<asp:BoundField DataField="Year" HeaderText="Vehicle Year" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="viewDetails" runat="server" Text="View" />
</ItemTemplate>
</asp:TemplateField>
I am facing a situation where the selectcountmethod isn't called.
The getdatamethod is called every time, but the count method is not. I also tried to remove the selectcountmethod and set enablepaging="false" and no data is displayed either, even if the dataset is not empty.
<asp:GridView ID="gvGradeDocent" runat="server" AutoGenerateColumns="False" AllowPaging="True"
DataSourceID="odsGradeDocent" DataKeyNames="Id" PagerStyle-HorizontalAlign="Right"
PagerSettings-Position="Top" CssClass="table">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" Visible="false" />
<asp:BoundField DataField="DecisionNo" HeaderText="DecisionNo" SortExpression="DecisionNo" />
<asp:BoundField DataField="DecisionDate" HeaderText="DecisionDate" SortExpression="DecisionDate"
DataFormatString="{0:dd/MM/yyyy}" />
<asp:CheckBoxField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:CommandField ShowSelectButton="True" ButtonType="Image" SelectImageUrl="~/Images/edit.png" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Image" DeleteImageUrl="~/Images/delete.png" />
</Columns>
</asp:GridView>
</div>
<asp:ObjectDataSource ID="odsGradeDocent" runat="server" SelectMethod="GetDocentGrades"
TypeName="mash.BusinessLogic.DocentGrade" EnablePaging="True" DeleteMethod="Delete"
SelectCountMethod="GetCountDocentGrades"></asp:ObjectDataSource>
Try working with the code sample here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.enablepaging.aspx
Check your query first to ensure that data is coming back. Also set the MaximumRowsParameterName and StartRowIndexParameterName parameters to the corresponding params in your select query.
I am trying to upgrade an ASP.NET application to .NET 4, but one page on my site contains an exception:
Argument Exception: an entry with the same key already exists".
What is different about ASP.NET 4 that might cause this problem?
One Solution
Not sure why but setting clientIDMode="Predictable" rather than Static seems to have avoided this exception message.
I had the same issue and fixed it.
I went through my entire ASPX page and found ASP.NET control that had the same ID as another.
I also tested this fix, and found that any control that conflicts with another control on the page will cause this error.
<asp:Label ID="FailureText" runat="server" EnableViewState="False" ClientIDMode="Static" />
<asp:Label ID="FailureText" runat="server" EnableViewState="False" ClientIDMode="Static" />
It happens when you Copy/Paste elements on the same page.
This in your web.config may also cause the error by allowing duplicate IDs
<pages clientIDMode="Static">
I have this error too and not resolve with this
<pages clientIDMode="Static">
my datagrid work fine this :
I have gridview :
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" CancelText="لغو" DeleteText="حذف" EditText="ويرايش" UpdateText="بروزرساني">
<ControlStyle CssClass="btn btn-xs btn-default" />
<ItemStyle Width="143px" />
</asp:CommandField>
<asp:BoundField DataField="ID" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="عنوان" SortExpression="Title" />
<asp:BoundField DataField="ParentID" HeaderText="پدر" SortExpression="ParentID" />
<asp:BoundField DataField="Url" HeaderText="آدرس" SortExpression="Url">
<ItemStyle CssClass="ltr"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Icon" HeaderText="آيکون" SortExpression="Icon" />
<asp:BoundField DataField="Order" HeaderText="اولويت" SortExpression="Order" />
</Columns>
</asp:GridView>
but after add image field i see this error
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" CancelText="لغو" DeleteText="حذف" EditText="ويرايش" UpdateText="بروزرساني">
<ControlStyle CssClass="btn btn-xs btn-default" />
<ItemStyle Width="143px" />
</asp:CommandField>
<asp:ImageField ReadOnly="true" SortExpression="Icon" DataImageUrlField="icon">
</asp:ImageField>
<asp:BoundField DataField="ID" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="عنوان" SortExpression="Title" />
<asp:BoundField DataField="ParentID" HeaderText="پدر" SortExpression="ParentID" />
<asp:BoundField DataField="Url" HeaderText="آدرس" SortExpression="Url">
<ItemStyle CssClass="ltr"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Icon" HeaderText="آيکون" SortExpression="Icon" />
<asp:BoundField DataField="Order" HeaderText="اولويت" SortExpression="Order" />
</Columns>
</asp:GridView>
I have gridview that I am using paging on. I want to pass along the current page in a asp:Hyperlink so I can send them back to the current page when they are done viewing the details of a record. Is this possible? If it is how can I do this?
<asp:GridView ID="grdObitList" runat="server" allowpaging="true"
PageSize="10" AutoGenerateColumns="false" CssClass="grdClass"
BorderStyle="None" GridLines="None" CellSpacing="2" >
<PagerStyle HorizontalAlign="Center" />
<PagerSettings Position="Bottom" FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" />
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="obit_fullname" DataNavigateUrlFields="obit_id" DataNavigateUrlFormatString="obitDisplay.aspx?oid={0}" />
<asp:BoundField ReadOnly="true" HeaderText="Date" DataField="obit_dod" DataFormatString="{0:d/M/yyyy}" />
<asp:BoundField ReadOnly="true" HeaderText="Resident Of" DataField="obit_resident" />
<asp:BoundField ReadOnly="true" HeaderText="Funeral Home" DataField="obit_funeralhome" />
</Columns>
One way to do it is converting it to a template column, that way you can use normal databind syntaxt to get to it (<%#)