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
Related
I've dropdown to language based on language selection change master page.
Where as I've gridview , this will bind with data based on language .
When i change language textbox value in gridview is not changing other than that remaining control values are changing .
Here is my Code of gridview
<asp:TemplateField HeaderText="<%$ Resources:AFRResources, Status %>" ItemStyle-CssClass="gridLanguage-status"
SortExpression="STATUS" ItemStyle-HorizontalAlign="Center" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblActive" runat="server" Text='<%# Bind("ACTIVE") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="gridLanguage-status" ForeColor="White" />
<ItemStyle CssClass="gridLanguage-status" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:AFRResources, SeqNo %>" HeaderStyle-CssClass="gridViolation-status"
ItemStyle-CssClass="gridViolation-status" ItemStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="White" SortExpression="STATUS">
<ItemTemplate>
<asp:Label ID="lblSeqno" runat="server" Text='<%# Bind("SEQUENCENO") %>'></asp:Label>
<asp:TextBox ID="txtSeqNo" runat="server" Width="40%" Text='<%# Bind("SEQUENCENO") %>'
onchange="SetValue()" MaxLength="4" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
but you have to refresh your gridview i mean you could have a method that receives as parameter the selection from your dropdownlist you could maybe use the event SelectedIndexChanged
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.
Trying to access a RequiredFieldValidator control that's inside a GridView in the RowCommand event and having trouble.
Here's the partial GridView code:
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password" Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
As you can see, there's a RFV for the EditTemplate and FooterTemplate. My issue is this; when the page loads, it has all the records in it, including an emtpy row at the bottom (Footer). If I click Edit on a populated row, the data is populated correctly, then when I hit UPDATE, I get all the error messages from the FOOTER RFV's firing off, which isn't correct. So, in the RowCommand event, I'd like to attempt this: If the user clicks the EDIT button, then disable all the RFV's in the footer row (the add new row), if they click anything else, enable them.
Ideas?
Sorry, meant to put this in the first time. In the RowCommand event, I am able to find the control but when I set the properties to something bogus, it seems to get overridden later, by the RowDataBound event:
RequiredFieldValidator rfv = (RequiredFieldValidator)gvUsers.FooterRow.FindControl("rfvAddWebLogin");
rfv.ControlToValidate = string.Empty;
rfv.ErrorMessage = "sdfgsdfgsdgsdfgsdfgsdfgsdfg";
rfv.Enabled = false;
You should use different ValidationGroups in your EditItemTemplate and FooterItemplate:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="BtnEdit" CausesValidation="False" Text="Edit" CommandName="Edit" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" ValidationGroup="UpdateUser" Text="Update" CommandName="Update" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnInsert" ValidationGroup="InsertUser" Text="Add" CommandName="Insert" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password"
Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" ValidationGroup="UpdateUser" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" ValidationGroup="UpdateUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" ValidationGroup="InsertUser" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" ValidationGroup="InsertUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic7
Note: If you're using ValidationSummaries, you need to add the appropriate ValidationGroup to every ValidationSummary. If you leave this property blank, only controls without a specified ValidationGroup will be listed.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.validationgroup.aspx
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" />
I have the following gridview that is inside an updatepanel:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="ButtonAdd" runat="server" OnClick="ButtonAdd_Click" Text="Novo Artigo" />
<asp:GridView ID="Dados" runat="server" AutoGenerateColumns="False" CssClass="Grid">
<Columns>
<asp:TemplateField HeaderText="Artigo">
<ItemTemplate>
<asp:TextBox ID="Artigo" runat="server"></asp:TextBox>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="Artigo"
PopupControlID="PanelArtigos"
>
</asp:ModalPopupExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descrição">
<ItemTemplate>
<asp:TextBox ID="Descricao" runat="server" Width="300px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IVA">
<ItemTemplate>
<asp:TextBox ID="IVA" runat="server" Width="40px" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pr. Unit.">
<ItemTemplate>
<asp:TextBox ID="PU" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="% Desc.">
<ItemTemplate>
<asp:TextBox ID="Desconto" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UN">
<ItemTemplate>
<asp:TextBox ID="UN" runat="server" Width="50px" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quant.">
<ItemTemplate>
<asp:TextBox ID="Quantidade" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Liquido">
<ItemTemplate>
<asp:TextBox ID="TotalLiquido" runat="server" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="RemoveArtigo" runat="server" OnClick="RemoveArtigo_Click">Remover
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Font-Size="Small" />
<RowStyle Font-Size="Small" CssClass="grid" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
As you can see, i have some textbox's.
When i change a value in a textbox, for example, the Desconto value, and leave that textbox, i want that the row where i am to be updated, using the new value present in the textbox. It's like a TextChanged or onBlur, but inside the update panel it's not working.
what is the best way to do it?
I am using VB.NET.
Thank you.
EDIT:
When adding the OnTextChanged="Desconto_TextChanged" i receive this compilation error:
Compilation Error Description: An
error occurred during the compilation
of a resource required to service this
request. Please review the following
specific error details and modify your
source code appropriately.
Compiler Error Message: BC30456:
'Desconto_TextChanged' is not a member
of 'ASP.index_aspx'.
Source Error:
Line 204:
Line 205:
Line 206:
Line 207:
Line 208:
Source File:
C:\inetpub\wwwroot\Facturas\Facturas\index.aspx
Line: 206
TextBox controls will not initiate a postback by default. you can set AutoPostBack to true for each textbox control.
I believe the event you need is TextChanged. Like lincolnk said, you need AutoPostBack set to True for the textbox to fire the postback. Then, inside the event handler, call Dados.DataBind() to reload the GridView's data.