Telerik Radgrid required field issue - asp.net

I have a radgrid displayed on a page and am wanting to make a particular field required when in insert and edit mode but don't necessarily know how to go about this.

Using regular validators
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="*" ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>
Like this:
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView AutoGenerateColumns="False">
<Columns>
<telerik:GridTemplateColumn HeaderText="ContactName" UniqueName="TemplateColumn">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ContactName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
http://www.telerik.com/help/aspnet-ajax/grid-validation.html

For common validation like required field, I found RadGrid has it built-in support
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/validation
<telerik:GridBoundColumn DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName">
<ColumnValidationSettings EnableRequiredFieldValidation="true" EnableModelErrorMessageValidation="true">
<RequiredFieldValidator ForeColor="Red" ErrorMessage="This field is required"></RequiredFieldValidator>
<ModelErrorMessage BackColor="Red" />
</ColumnValidationSettings>
where ModelErrorMessage control validation is available only in .NET 4.5 when ModelBinding is used.

Related

Required Field validator in edit item template is not working

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

RequiredFieldValidator only works for one validation

I have a web application written in VS2010.
I have a GridView in which I want to add 2 different validations to a field in edit mode. One validation is that it is a required field. The other validation is a max. length of 80 characters.
Below is my HTML:
<asp:GridView ID="UserInfoGridView" runat="server" AutoGenerateColumns="False"
Caption="User Information" CaptionAlign="Top" CssClass="grid" HorizontalAlign="Left"
PageSize="1" Width="400px" ShowHeaderWhenEmpty="true" Height="60px"
OnRowCancelingEdit="UserInfoGridView_RowCancelingEdit"
OnRowEditing="UserInfoGridView_RowEditing"
OnRowUpdating="UserInfoGridView_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Name" ItemStyle-Wrap="false">
<EditItemTemplate>
<asp:TextBox ID="uigvTxtBoxName" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldEditName" ControlToValidate="uigvTxtBoxName" runat="server"
ErrorMessage="Required field." ValidationGroup="EditUserNameValidation" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="MaxValEditName" ControlToValidate="uigvTxtBoxName" runat="server"
ErrorMessage="Maximum length is 80." ValidationGroup="EditUserNameValidation" Display="Dynamic"
CssClass="message-error" ValidationExpression="^.{1,80}$">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="uigvLblName" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address" ItemStyle-Wrap="false">
<EditItemTemplate>
<asp:TextBox ID="uigvTxtBoxEmail" runat="server" Text='<%# Bind("UserEmail") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldEditEmail" ControlToValidate="uigvTxtBoxEmail" runat="server"
ErrorMessage="Required field." ValidationGroup="EditUserEmailValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="MaxValEditEmail" ControlToValidate="uigvTxtBoxEmail" runat="server"
ErrorMessage="Maximumn length is 80." ValidationGroup="EditUserEmailValidation" Display="Dynamic" CssClass="message-error"
ValidationExpression="^.{1,80}$" >
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="uigvLblEmail" runat="server" Text='<%# Bind("UserEmail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="False" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:Button ID="uigvUpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" CssClass="gridActionbutton"></asp:Button>
<asp:Button ID="uigvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" CssClass="gridActionbutton"></asp:Button>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="uigvEditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" CssClass="gridActionbutton">
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle Wrap="True" />
</asp:GridView>
When in edit mode if the field is left blank, both of the error messages are displayed. If the user enters more than 80 characters, neither message is displayed.
I did this in an application using VS 2012. Can this be done in VS2010? If so, where am I going wrong?
Thanks.
use regularexpression validator or range validator instead of requiredfield validator for validating max length.
So you need to use one required field and the other is regular expression validator.

textbox Control in GridView

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

RegularExpressionValidator for textbox inside GridView not working

I have GridView:
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It work fine, but when I add RegularrExpressionValidator for textbox with id="tbDate" mygrid does not work
This regular expression must accept datetime in format "dd.MM.yyyy"
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data"
ValidationExpression=
"(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="tbDate">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I think you typed "tbData" instead of "tbDate" in the ControlToValidate property
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data" ValidationExpression= "(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>
I changed ClientIDMode to Predictable for TextBox, RegularExpressionValidator and RequiredFieldValidator and began to work
change ControlToValidate="tbData" to ControlToValidate="tbDate" for <asp:RegularExpressionValidator
Have you tried placing the <asp:RegularExpressionValidator ...> inside the
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</ItemTemplate>
also you need to indicate some text to display if it fails the regex validation
actually what Hallie said is more than likely the answer ha

Popup Calendar in edit mode of asp.net gridview

All respected, I have a Master detail project asp.net (sql data) project in which Master.aspx along with code behind page Master.aspx.cs. Following is the code:
<asp:TemplateField HeaderText="Date of Failure" SortExpression="Failure_date" >
<EditItemTemplate>
<asp:TextBox ID="EditFailure_date" runat="server" Text='<%# Bind("Failure_date", "{0:d}") %>' ></asp:TextBox><img src="_images/images.jpg" style="margin-top:3px;width:30px;height:30px;cursor:hand;" onclick="PopupPicker('EditFailure_date')" />
<asp:RequiredFieldValidator ID="Failure_dateRequiredFieldValidator" runat="server" ControlToValidate="EditFailure_date" Display="Dynamic" ErrorMessage="Can not be blank" SetFocusOnError="True"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemTemplate>
<asp:Label ID="Failure_date" runat="server" Text='<%# Bind("Failure_date", "{0:dd/MM/yyyy}") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now I want to add a popup calender for above text box 'EditFailure_date' without codebehind. Please help.
You can use the AjaxControlToolkit's calendar like this:
<asp:TextBox ID="EditFailure_date" runat="server" Text='<%# Bind("Failure_date", "{0:d}") %>' ></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" ID="cal_EditFailure_date" TargetControlID="EditFailure_date" />
Edit: You can also use a jquery solution such as the jQueryUI DatePicker
<asp:TextBox ID="txtDOJ" Text='<%# Bind("DOJ", "{0:dd-MMM-yyyy}") %>' runat="server" class="form-control input-sm m-bot15" BackColor="#cbeddc"></asp:TextBox> <asp:CalendarExtender ID="CalExtender" runat="server" Enabled="true" Format="dd-MMM-yyyy" TargetControlID="txtDOJ"> </asp:CalendarExtender>

Resources