I have a text box in grid view control,i used required field validator to validate the textbox like
<asp:TemplateField HeaderText="Relationship" ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtRelationShip" runat="server" Text='<%# Eval("RelationShip") %>' Visible="false" autocomplete="off"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvRelationShip" runat="server" ControlToValidate="txtRelationShip" ForeColor="Red" ClientIDMode="AutoID"
ErrorMessage="Please enter relationship" ValidationGroup="vgUpdateRelationShip" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:LinkButton ID="btnRelationShipUpdate" runat="server" ValidationGroup="vgUpdateRelationShip"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I have many rows in my grid view , when i edit and update the text box value if single text box is empty it shows error in all text box.
How to resolve this problem, i want if single text box is empty shows error only in single text box
You have to make sure that the ValidationGroup is unique per row, like ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1" ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'></asp:RequiredFieldValidator>
<asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup='<%# "ValidationGroup_" + Container.DataItemIndex %>'>Update</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Related
I am displaying data(wrapped) in grid but when i am clicking on edit button is is not showing complete data because textbox is very small(data not wrapped). any help please...
Sample Code Line:
<asp:TemplateField HeaderText="Reason(NotDone)" SortExpression="tReason">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("tReason") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label17" runat="server" Text='<%# Bind("tReason") %>' ></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="SeaGreen" />
</asp:TemplateField>
........
On page load i am using the following code:
GridView1.Attributes.Add("style", "word-break:break-all; word-wrap:break-word");
Make the textbox a multiline.
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("tReason") %>'
TextMode="MultiLine"></asp:TextBox>
Thanks Akash & Michael for the quick response......i have done like this and it is working in grid .....please suggest if it is correct way ........asp:TemplateField HeaderText="Reason (If not Done)" SortExpression="tReason">
' TextMode="Multiline" Rows="10">
' >
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.
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 have a page that has a listview that is used for inserting and editing records.
Assigning a RequiredFieldValidator and ValidatorCallOutExtender to the InsertItemTemplate works well.
When I try to do the same on the EditItemTemplate the ValidatorCallOut appears but with no text in the box.
Is there something that I'm doing wrong?
My code for the InsertItemTemplate:
<asp:TextBox ID="date_timeTextBox" runat="server" Text='<%# Bind("date_time") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="insert_into">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1"
runat="server"
TargetControlID="RequiredFieldValidator1">
</cc1:ValidatorCalloutExtender>
And for the EditItemTemplate:
<asp:TextBox
ID="date_timeTextBox"
runat="server"
Text='<%# Bind("date_time","{0:yyyy-MM-dd}") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="reqDTT"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="edit_validate">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender
ID="val_reqDTT"
runat="server"
TargetControlID="reqDTT">
</cc1:ValidatorCalloutExtender>
Make sure your ID's are unique across your Templates, so the ControlToValidate="date_timeTextBox" is different.
InsertTemplate
<asp:TextBox ID="date_timeTextBoxInsert" runat="server" Text='<%# Bind("date_time") %>' />
EditTemplate
<asp:TextBox ID="date_timeTextBoxEdit" runat="server" Text='<%# Bind("date_time") %>' />