.aspx page does not load after adding Validators in VB.Net - 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.

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>

Format date in GridView

Please see the database DDL below:
create table datetest (datetime1 datetime)
insert into datetest values(getdate())
Please see the presentation code below:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# bind("datetime1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The output on the page is: 11/04/2013 19:51:40, but the database value is: 2013-04-11 19:51:40.017 (notice the milliseconds are chopped off the first value).
I have looked into this and have found a number of posts on here, which suggest using EVAL and String.Format, but I have been unsuccessful. How can I display milliseconds on the webpage?
I have tried the following code:
<asp:Label ID="Label1" runat="server" Text='<%
Eval("datetime1").ToString("dd/MM/yyyy hh:mm:ss.fff") %>'></asp:Label>
However, I am prompted with an exception: "Conversion from string "dd/MM/yyyy" to type 'Integer' is not valid."
<asp:Label ID="Label1" runat="server" Text='<%# Eval("datetime1").ToString("dd/MM/yyyy hh:mm:ss.fff") %>'></asp:Label>
Something like that should do it. If you need help with custom time formats, have a look here: http://msdn.microsoft.com/en-gb/library/8kb3ddd4.aspx
You do not need to do a template to change the format though, you can add {0:dd/MM/yyyy hh:mm:ss.fff} in the custom format property
You should use Eval with additional format parameter instead of Eval().ToString() call:
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("datetime1", "{0:dd/MM/yyyy hh:mm:ss.fff}") %>'></asp:Label>

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

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.

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.

How to remove one item from string in a textbox

I have a gridview with 2 columns.
First field contains checkbox named chkSelect and second column is a label which is binded with EmailId.
<asp:GridView ID="gvwNewsLetter" runat="server" AutoGenerateColumns="false" DataKeyNames="UserID">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelectMail" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmailID">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("EmailID")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
When I check each checkbox I have to display mailid in corresponding row in a textarea which is outside gridview like" abc#gmail.com,sdf#gmail.com". If I uncheck inbetween I have to remove that particular id from textbox. Can anybody help give the code to remove mailid on unchecking the checkbox.
Can you not just rebuild the entire string each time a checkbox is checked/unchecked? Might be faster than trying to parse through and modify the existing string each time.