I am editing a field in gridview and would like that the edited value not be allowed to be greater than the old value?
is there a front end validation for this? so as to not use a javascript popup
Thanks
<asp:TemplateField HeaderText="FC Amount">
<ItemTemplate>
<asp:Label ID="FCLabel" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="FCTextBox1" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Use the CompareValidator control:
Add hidden with old value, and compare its value with new one. Or set ValueToCompare property:
<asp:TemplateField HeaderText="FC Amount">
<ItemTemplate>
<asp:Label ID="FCLabel" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="FCTextBox1" runat="server" Text='<%# Eval("FC AMOUNT") %>'></asp:TextBox>
<asp:CompareValidator
ID="cmpAmount"
runat="server"
ValueToCompare='<%# Eval("FC AMOUNT") %>'
ControlToValidate="FCTextBox1"
Type="Double"
Operator="LessThanEqual" />
</EditItemTemplate>
</asp:TemplateField>
How to: Validate Against a Specific Value for ASP.NET Server Controls
CompareValidator.Operator Property
Try this,
<asp:CompareValidator
ID="cval1"
runat="server"
ValueToCompare='<%#Eval("OldValue") %>'
ControlToValidate="FCTextBox1"
Type="Integer"
Operator="GreaterThanEqual" />
Related
I have a checkbox on my griview and its able to be clicked before the edit button is clicked, this doesn't make sense to me and its not something I have came across before. In theory the checkbox should be greyed out until a user clicks the edit button.
Nothing can be updated but it just doesn't make any sense as to why this functionality would be available. I have set up Gridviews before using checkboxes and never came across this. Below is my code:
<asp:GridView ID="gvLeagues" runat="server"
AutoGenerateColumns="False"
onpageindexchanging="gvLeagues_PageIndexChanging"
onrowcancelingedit="gvLeagues_RowCancelingEdit"
onrowdatabound="gvLeagues_RowDataBound"
onrowediting="gvLeagues_RowEditing"
onrowupdating="gvLeagues_RowUpdating"
onsorting="gvLeagues_Sorting" EnableModelValidation="True"
CssClass="footable"
EditRowStyle-CssClass="table table-bordered" >
No Data Found.
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label1" runat="server" Text='<%# Bind("Name")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created Date" SortExpression="CreatedDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly="true" Text='<%# Bind("CreatedDate")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label2" runat="server" Text='<%# Bind("CreatedDate")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Day" SortExpression="Day">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Day")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label3" runat="server" Text='<%# Bind("Day")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Season" SortExpression="Season">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Season")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label4" runat="server" Text='<%# Bind("Season")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enabled" SortExpression="Enabled">
<EditItemTemplate>
<asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' />
</EditItemTemplate>
<ItemTemplate><asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' /></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
When using the ItemTemplate, you are the one specifying what and how you want to see it. Just set that checkbox as disabled.
<asp:TemplateField HeaderText="Enabled" SortExpression="Enabled">
<EditItemTemplate>
<asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' />
</EditItemTemplate>
<ItemTemplate><asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' enabled="False"/></ItemTemplate>
</asp:TemplateField>
This is my code.I am using a gridview in asp.net.I am trying to add a requiredfieldvalidator to the
textbox_id
in gridview edit mode but nothing happens even if I dont put anything in the textbox and click update.
<Columns>
<asp:BoundField DataField="StudentId" HeaderText="Student_ID" />
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox_id" runat="server" Wrap="False" CausesValidation="true" ValidationGroup="a"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="a" runat="server" ControlToValidate="TextBox_id" ErrorMessage="RequiredField" ForeColor="Red"></asp:RequiredFieldValidator>
</EditItemTemplate>
<%--<ItemTemplate>
<asp:Label ID="Label3" runat="server"></asp:Label>
</ItemTemplate>--%>
</asp:TemplateField>
The button that triggers the validation should have
ValidationGroup attribute set to "a" as well.
Try this:
<asp:TemplateField HeaderText="Popolazione residente"
SortExpression="InhabitantsNum">
<EditItemTemplate>
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem,"InhabitantsNum") %>
</itemtemplate>
<asp:TextBox ID="InsertPopolazioneResidente" runat="server"
Text='<%# Bind("InhabitantsNum") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Inserire un numero positivo" ValidationExpression="^[0-9]+$" ForeColor="Red" ControlToValidate="InsertPopolazioneResidente"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="InsertPopolazioneResidente" runat="server"
Text='<%# Bind("InhabitantsNum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
To summarise, to get validation to work in an Edit Template you need:
A ValidationGroup in the control (Eg TextBox)
The same ValidationGroup in the Validator
The same ValidationGroup in the button etc that triggers the action
CausesValidation="true" in the button etc in step 3
I have a GridView with some TemplateField items containing TextBox controls. I would like to add a required field validator on it. This is my code:
<asp:TemplateField HeaderText="vid">
<EditItemTemplate>
<asp:TextBox ID="txtvid" runat="server" Width="150px"
Text='<%# Bind("vid") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label
ID="lblvid" runat="server"
Text='<%# Bind("vid") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
How do I place a required field validator on txtvid?
In the Edit template, add a RequiredFieldValidator like this:
<EditItemTemplate>
<asp:TextBox ID="txtvid"
runat="server" Width="150px"
Text='<%# Bind("vid") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator
ControlToValidate="txtvid"
runat="server"
ErrorMessage="Please enter a 'vid' number"
Text="*"/>
</EditItemTemplate>
Here is the reference for the RequiredFieldValidator on MSDN.
UPDATE:
If you wanted a regular expression validator, its pretty much the same, but with the RegularExpressionValidator control:
<asp:RegularExpressionValidator
ControlToValidate="txtvid"
ValidationExpression="\d{10}"
runat="server"
ErrorMessage="Please enter a 'vid' of 10 digits"
Text="*"/>
Here is a complete list of the functionality for the RegularExpressionValidator on MSDN.
Within gridview i assign textbox,requiredfieldvalidator and button,This validator validate all textboxes in gridview when button click without filling textbox. How can i solve this..
<asp:TemplateField HeaderText="vid">
<ItemTemplate>
<asp:TextBox ID="txtvid" runat="server" Width="150px" ValidationGroup ="subgrp">
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvQuantity" ControlToValidate="txtvid" runat="server"
ErrorMessage="Required" ForeColor="Red"
ValidationGroup = "subgrp"></asp:RequiredFieldValidator>
<asp:Label
ID="lblvid" runat="server"
Text='<%# Bind("vid") %>'>
</asp:Label>
<asp:Button ID="btnSelect" runat="server" Text="Select" ValidationGroup ="subgrp"/>
</ItemTemplate>
</asp:TemplateField>
This will validate all textboxs in gridview,When i click button in particular row without filling the textbox in itemtemplate.
I would like to format a label using a format string returned when the grid view is bound to a stored procedure. I want to do something like this, but not this as it doesn't work:
<asp:Label ID="lbl" runat="server" Text='<%# Eval("ValueColumn"), Eval("NumberFormatColumn") %>'></asp:Label>
Thanks. Dan.
You can use string.Format with Eval.
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lbl" runat="server"
Text='<%# string.Format("{0}, {1}", Eval("ValueColumn"),
Eval("NumberFormatColumn")) %>'/>
</ItemTemplate>
</asp:TemplateField>
Or
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lbl" runat="server"
Text='<%# Eval("ValueColumn") + ", " + Eval("NumberFormatColumn") %>'/>
</ItemTemplate>
</asp:TemplateField>
Updated:
To format a string based on given format string
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lbl" runat="server"
Text='<%# string.Format(Eval("NumberFormatColumn").ToString(),
Eval("ValueColumn")) %>'/>
</ItemTemplate>
</asp:TemplateField>
Simple question? I have a gridview with an insert row in the footer and i have validation controls for that row and the edittemplate how do get it to only validate for that row because when i do an edit it validates the insert row aswell. I just wnat to validate the insert row or edit row.
<asp:GridView ID="PageSettings" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
DataKeyNames="Id"
DataSourceID="ObjectDataSourcePages"
OnLoad="PageSettings_Load"
OnRowDataBound="PageSettings_DataBound"
OnRowCommand="PageSettings_RowCommand"
OnRowCreated="PageSettings_RowCreated"
OnRowEditing="PageSettings_RowEditing"
OnRowCancelingEdit="PageSettings_RowCancelingEdit"
OnRowUpdating="PageSettings_RowUpdating"
OnPageIndexChanging="PageSettings_PageIndexChanging"
OnSorting="PageSettings_Sorting"
OnSorted="PageSetting_Sorted"
PageSize="2"
ShowFooter="True"
ShowHeaderWhenEmpty="True">
<Columns>
<asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireName" runat="server"
ControlToValidate="Name"
CssClass="gridview-error"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireInsertName" runat="server"
ControlToValidate="InsertName"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left" SortExpression="Path">
<ItemTemplate>
<%# Eval("Path") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequirePath" runat="server"
ControlToValidate="Path"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertPath" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireInsertPath" runat="server"
ControlToValidate="InsertPath"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left" SortExpression="RouteValue">
<ItemTemplate>
<%# Eval("RouteValue") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertRouteValue" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left" SortExpression="RegExp">
<ItemTemplate>
<%# Eval("RegExp") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertRegExp" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use a
Validation Group
Define a separate group for each operation
SaveRecords
&
EditRecords
. You have to specify each validation group on the Save/Edit buttons & all corresponding validation controls to fire them for that event.
MSDN
After a long Time
but you must add in footer requiredfieldvalidator a validationgroup with a unique name and add this validationgroup to add button