Run asp.net required field validation only if at least one of the fields in the group is completed - vb.net

I have a form on an asp.net (VB) page which has 2 sections.
All of section 1 is always required, but section 2 is optional. However, if section 2 is started, then all fields are required.
I'm stuck as to the logic I need to create this validation.
Here's my code:
<form runat="server">
<div>
<asp:TextBox runat="server" ID="field1a" Text="Name" />
<asp:TextBox runat="server" ID="field1b" Text="City" />
<asp:RequiredFieldValidator runat="server" ID="field1aVal" Text="Name Required" ControlToValidate="field1a" InitialValue="Name" />
<asp:RequiredFieldValidator runat="server" ID="field1bVal" Text="City Required" ControlToValidate="field1b" InitialValue="City" />
</div>
<div>
<asp:TextBox runat="server" ID="field2a" Text="Name" />
<asp:TextBox runat="server" ID="field2b" Text="City" />
<asp:RequiredFieldValidator runat="server" ID="field2aVal" Text="Name Required" ControlToValidate="field2a" InitialValue="Name" />
<asp:RequiredFieldValidator runat="server" ID="field2bVal" Text="City Required" ControlToValidate="field2b" InitialValue="City" />
</div>
<asp:button runat="server" ID="btnSubmit" Text="Submit" OnClick="submitForm" />
</form>

Id say use required fields on section 1, but for section two use a custom validator. Something like this.
<asp:CustomValidator ID="CustomValidator2" runat="server"
Display="Dynamic" EnableClientScript="False"
ErrorMessage="You must select one checkbox or click all offices. " OnServerValidate="OfficeVaildator">
</asp:CustomValidator>
Then in code behind do your logic
Something like this
Sub OfficeVaildator(ByVal source As Object, ByVal args As ServerValidateEventArgs)
If OfficeCheckBoxs.Checked Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
Obviously that is really simple. You would want to check if the user altered section 2 in anyway, and if he did make him complete all fields to return your server validation arguments true. If he didnt alter section 2 or give it any data right a method that checks that and set your validation to true to allow him to proceed.

Related

I want my Checkboxlist to be automatically filled on page loading

I have created a hobbies checkboxlist and want it to be automatically filled on page loading after particular user logs in. The checkboxes should be checked according to the hobbies stored in database records for a user. I have no idea how to do it.
I doing code in vb.net, here is my code for User_Form.aspx
<asp:Label ID="lblSelectHobbies" runat="server" Text="Select hobbies"></asp:Label>
<br />
<asp:CheckBox ID="chbSelectAllHobbies" runat="server" Text="Select all" OnCheckedChanged ="chbSelectAllHobbies_CheckedChanged" AutoPostBack="true"/>
<br />
<asp:CheckBoxList ID="cblHobbies" runat="server" DataSourceID="SqlDataSource_Hobby" DataTextField="Hobbies" DataValueField="Hobbies">
</asp:CheckBoxList>
</td>

.aspx page does not load after adding Validators in VB.Net

I have an aspx page with a few FormViews and an updateable GridView that I would like to add validation for. The fields that I am trying to validate are also using the CalendarExtender from the AJAX Control Toolkit by the way. I am trying to use the RequiredFieldValidator and RangeValidator for this. The problem is I don't see any errors from within VisualStudio 2015 while adding the validators. But when I run debug mode and click the button to open a FormView that contains validation I get a 500 error without any useful information to find/fix my problem.
I first tried adding both of the validators at once but after having problems, I am just trying to get the RequiredFiledValidator working. But I still need to know how to get the RangeValidator to work properly. For the RangeValidator, I want to validate a start date of no less than today and an end date that is greater than the start date. I tried using all of the examples that I found on these pages:
[1] https://www.c-sharpcorner.com/UploadFile/17e8f6/requiredfieldvalidator-control-in-Asp-Net/
[2] https://asp.net-tutorials.com/validation/required-field-validator/
[3] http://www.java2s.com/Tutorial/ASP.NET/0160__Validation/UseaspRangeValidatortocheckthevaluerangeinanasptextbox.htm
[4] http://www.informit.com/articles/article.aspx?p=101137&seqNum=5
[5] https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.rangevalidator.minimumvalue?view=netframework-4.8
[6] https://www.tutorialspoint.com/asp.net/asp.net_validators.htm
Here is an example of one of the FormViews that I am trying to validate. In the RangeValidator I just have hard coded Max and Min values but I eventually want to have the Min value to be the same value that is in the begin date text box plus day:
<asp:FormView ID="fvNotification" runat="server" Visible="False" DefaultMode="Insert" GridLines="Both" DataSourceID="SqlUserDetails">
<InsertItemTemplate>
<asp:Table ID="Table1" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>Notification</asp:TableHeaderCell>
<asp:TableHeaderCell>Filter 1</asp:TableHeaderCell>
<asp:TableHeaderCell>Filter 2</asp:TableHeaderCell>
<asp:TableHeaderCell>Begin Date</asp:TableHeaderCell>
<asp:TableHeaderCell>End Date</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
<asp:DropDownList ID="ddNotification" runat="server" DataSourceID="SqlNotifications" DataTextField="Name" DataValueField="Name" ></asp:DropDownList></asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="ddFilterInsrt" runat="server" DataSourceID="SqlFilters1" DataTextField="Filter" DataValueField="Filter" ></asp:DropDownList></asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="ddFilterInsrt2 runat="server" >
<asp:ListItem>*</asp:ListItem>
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="TextBoxDateBgnInsrt" runat="server" autocomplete="Disabled" ></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxDateBgnInsrt_CalendarExtender" TargetControlID="TextBoxDateBgnInsrt" ID="TextBoxDateBgnInsrt_CalendarExtender"></ajaxToolkit:CalendarExtender>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="TextBoxDateEndInsrt" runat="server" autocomplete="Disabled" ></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxDateEndInsrt_CalendarExtender" TargetControlID="TextBoxDateEndInsrt" ID="TextBoxDateEndInsrt_CalendarExtender"></ajaxToolkit:CalendarExtender>
<asp:RangeValidator runat="server" id="rngDate" controltovalidate="TextBoxDateEndInsrt" type="Date" MaximumValue='09/20/2011' MinimumValue="09/01/2011" errormessage="Please enter a valid date within 2006!" display="Dynamic"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" CausesValidation="True" />
<asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" OnClick="InsertCancelButton_Click1" />
<asp:Button ID="InsertClearEndDateButton" runat="server" Text="Clear End Date" OnClick="InsertClearEndDateButton_Click"/>
</InsertItemTemplate>
</asp:FormView>
I am expecting the FormView to be visible and validate after clicking the button that make the FormView visible but instead I am getting a 500 error.

OnUploadedComplete function of AsyncFileUpload is never hit in detailsview

I have an AsyncFileUpload control in a detailsview. I can browse to select a file to upload. When I start the upload the OnClientUploadComplete function uploadRoomThumbComplete method is executed, but the function in OnUploadedComplete is never hit.
Why?
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DetailsView AutoGenerateRows="False" DefaultMode="Edit" Width="100%" DataKeyNames="id" DataSourceID="odsDetails" ID="dvOfferDetails" runat="server">
<Fields>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Bottom" HeaderText="<%$Resources:Glossary,image%>">
<EditItemTemplate>
<asp:literal ID="ltImage" runat="server" />
<p id="RoomContainer">
</p>
<asp:literal ID="ltDeleteRoomThumb" runat="server" />
<cc1:AsyncFileUpload CssClass="imageUploaderField" ID="afuRoomThumb" Width="150" runat="server" ClientIDMode="AutoID" OnUploadedComplete="afuRoomThumb_UploadedComplete" OnClientUploadComplete="uploadRoomThumbComplete" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</ContentTemplate>
</asp:UpdatePanel>
Protected Sub afuRoomThumb_UploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs)
'---> this codeblock is NEVER hit....why?
End Sub
Ok, I found the answer here: http://forums.asp.net/t/1477331.aspx?AsyncFileUpload+inside+a+GridView+inside+an+UpdatePanel
In short: place another AsyncFileUpload control on the page and set the style's display:none.
It seems that the AsyncFileUpload is designed to be used only once on a page, and when you put it on your master page, any afu inside an updatepanel will work.

update rad ajax manager from codebehind

Hi can someone help me how to call (or refresh) below control from code behind?
Currently it is working when I click my btnUpdate button. Along with I want to refresh the same usercontrol when I press another button also (ex: btnrefresh).
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="btnUpdate">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="Panel1" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
<ClientEvents OnRequestStart="RequestStart" OnResponseEnd="ResponseEnd" />
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default" />
<asp:Panel ID="Panel1" runat="server" Width="100%">
<uc:EmpEnrollment runat="server" ID="EmpEnrollment" />
</asp:Panel>
Just add another AjaxSetting for your btnrefresh in your AjaxSettings like below:
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="btnUpdate">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="Panel1" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="btnrefresh">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="Panel1" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>

Sending email to all the email adresses in gridview which are checked

I have a grid view control in my application.
Please see the below code.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CPUserID" DataSourceID="GetSelectDelegatesDataSource">
<Columns>
<asp:BoundField HeaderStyle-CssClass="gridview_header" HeaderStyle-ForeColor="White" Visible="false" DataField="EmailAddress" HeaderText="Email Address" SortExpression="EmailAddress">
<HeaderStyle CssClass="gridview_header" ForeColor="White"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<input id="masterCheck" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkSelect" />
</ItemTemplate>
<HeaderStyle CssClass="gridview_header" ForeColor="White"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
And I have a image button
<asp:ImageButton ID="ibtnSendInvites" Visible="false" runat="server" ImageUrl="~/images1/send_button.png" />
If you see above code you can find that there is check box functionality in my gridview. I have email address boundfield in my gridview. I want to send an email to the email addresses which are been checked in gridview.
Please provide the vb.net code for the same
Very quickly thrown together, you can get the jist.. I didn't test it or anything.
Dim emailList As New List(Of String)
For Each curRow As GridViewRow In GridView1.Rows
Dim chkSelect As CheckBox = CType(curRow.Cells(1).FindControl("chkSelect"), CheckBox)
If chkSelect.Checked Then
emailList.Add(curRow.Cells(0).Text)
End If
Next
' you now have a generic list of email addresses..
System.Web.Mail provide the functionality to send mail from .Net framework. Download the sample project Send Mail- codeproject and see how this is implemented.
Check this link on BulkEditGridView. It's just like a GridView, but you can edit (or perform actions on) multiple rows of a gridview with one click. I use it and love it.