I currently have a RadGrid with an EditItemTemplate that contains, among other things, a checkbox, a RadTextBox and a RequiredFieldValidator. If the checkbox is checked, then the RadTextBox is a required field. If it is unchecked, then the RadTextBox can be empty. This works perfectly when you edit a record. When you insert a new record, however, the validator stops the form from submitting, but the message ("Email is a required field') does not appear. Anyone have any suggestions? My code is below:
RadGrid:
Javascript:
The Code-Behind:
Thanks in advance for any helpl!
Marc
RadGrid:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" |
AutoGenerateColumns="False" GridLines="None" AutoGenerateDeleteColumn="False" |
AutoGenerateEditColumn="False" Width="100%" Skin="Office2007"> |
<AlternatingItemStyle BackColor="#EAF2FF" /> |
<MasterTableView CommandItemDisplay="Top" DataKeyNames="BranchID" OverrideDataSourceControlSorting="true"> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</ExpandCollapseColumn> |
<CommandItemSettings AddNewRecordText="Add new Branch" /> |
<Columns> |
<telerik:GridTemplateColumn DataField="NotifyFlag" HeaderText="Notify?" SortExpression="NotifyFlag" |
UniqueName="NotifyFlag" Visible="False"> |
<EditItemTemplate> |
<asp:CheckBox runat="server" ID="cbNotify" Checked='<%#convertBool(DataBinder.Eval(Container.DataItem, "Notifyflag"))%>' /> |
</EditItemTemplate> |
<ItemStyle HorizontalAlign="Left" /> |
</telerik:GridTemplateColumn> |
<telerik:GridTemplateColumn DataField="Email" HeaderText="Email" SortExpression="Email" |
UniqueName="Email" Visible="false"> |
<ItemTemplate> |
<%#DataBinder.Eval(Container.DataItem, "Email")%> |
</ItemTemplate> |
<EditItemTemplate> |
<telerik:RadTextBox runat="server" ID="txtEmail" Width="280px" Text='<%#DataBinder.Eval(Container.DataItem, "Email")%>' /> |
<asp:RequiredFieldValidator runat="server" ID="rfvEmail" ControlToValidate="txtEmail" |
Display="Dynamic" ErrorMessage="Email address required" Enabled="false"></asp:RequiredFieldValidator> |
<asp:RegularExpressionValidator runat="server" ID="revEmail" Display="Dynamic" ControlToValidate="txtEmail" |
ErrorMessage="Valid email required" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" /> |
</EditItemTemplate> |
<ItemStyle HorizontalAlign="Left" /> |
</telerik:GridTemplateColumn> |
<telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Edit" Text="Edit" |
UniqueName="EditColumn"> |
<ItemStyle HorizontalAlign="Center" /> |
</telerik:GridButtonColumn> |
<telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Delete" Text="Delete" |
ConfirmDialogType="RadWindow" ConfirmText="Are you sure you want to delete this Branch?" /> |
</Columns> |
<EditFormSettings ColumnNumber="1" CaptionFormatString="" FormTableItemStyle-HorizontalAlign="Left" |
FormTableAlternatingItemStyle-HorizontalAlign="Left"> |
<EditColumn ButtonType="PushButton" CancelText=" Cancel" UpdateText=" Save" |
InsertText=" Save" UniqueName="EditCommandColumn1"> |
</EditColumn> |
<FormMainTableStyle CellPadding="3" CellSpacing="0" BackColor="White"></FormMainTableStyle> |
<FormCaptionStyle></FormCaptionStyle> |
<FormTableItemStyle Wrap="False"></FormTableItemStyle> |
<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> |
<FormTableButtonRowStyle HorizontalAlign="Right"></FormTableButtonRowStyle> |
</EditFormSettings> |
</MasterTableView> |
</telerik:RadGrid> |
Javascript:
<script type="text/javascript"> |
function emailChanged(obj) { |
if (obj.checked != true) { |
var hiddenBox = document.getElementById("hdnRFV"); |
var theRFV = document.getElementById(hiddenBox.value); |
alert("Here1: " + hiddenBox.value); |
ValidatorEnable(theRFV, false); |
} else { |
var hiddenBox = document.getElementById("hdnRFV"); |
var theRFV = document.getElementById(hiddenBox.value); |
alert("Here1: " + hiddenBox.value); |
ValidatorEnable(theRFV, true); |
} |
} |
</script> |
The Code-Behind:
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound |
' sets the status of the validator based on the value of the NotifyFlag column |
If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then |
Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem) |
Dim myCheckBox As CheckBox = DirectCast(edititem("NotifyFlag").Controls(1), CheckBox) |
myCheckBox.Attributes.Add("onclick", "emailChanged(this);") |
Dim cntrl As HiddenField = DirectCast(FindControl("hdnRFV"), HiddenField) |
cntrl.Value = DirectCast(edititem("Email").Controls(3), RequiredFieldValidator).ClientID |
End If |
End Subhelp |
Thanks in advance for any helpl!
Marc