Custom Validation on a RadComboBox is not working - vb.net

I am currently working on a project in vb.net and I am having trouble getting my custom validator to work on a radcombobox. When I run my application and test the validation it is not being invoked, causing my application to break. I am not looking into client side validation, just server side. Below is the code I have been using.
Aspx code
<td>
<telerik:RadComboBox ID="radcomboboxListTimeZone" runat="server" BorderColor="#bbe0ef"
BorderWidth="2px" Width="400px" MaxHeight="200px"
DataSourceID="ObjectDataSourceListTimeZones"
DataTextField="DisplayName" DataValueField="StandardName" SelectedValue='<%#
Bind("TimeZoneStandardName")%>'>
</telerik:RadComboBox>
<asp:CustomValidator ID="CustomValidatorTimeZone" runat="server" ErrorMessage="Please select a
valid Time Zone Standard Name" ControlToValidate="radcomboboxListTimeZone"
OnServerValidate="CustomValidatorListTimeZone_ServerValidate">•</asp:CustomValidator>
</td>
Server side code
Protected Sub CustomValidatorListTimeZone_ServerValidate(ByVal source As Object, ByVal args As
ServerValidateEventArgs)
Dim radcomboboxListTimeZone As RadComboBox =
CType(FormViewTimeZones.FindControl("radcomboboxListTimeZone"), RadComboBox)
Dim selectedValue As String = radcomboboxListTimeZone.SelectedValue
If selectedValue = "Coordinated Universal Time" Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub
Any help would be much appreciated.

I managed to get the server side custom validation working. To do this I created a validation group
<asp:ValidationSummary ID="ValidationSummaryEditItem" runat="server" ValidationGroup="TimeZone" />
and set
CausesValidation="true"
on the edit button and added
ValidationGroup="TimeZone"
to both the edit button and the custom validator

Related

How do I change an object's value in EntityFramework before it gets displayed on the web page?

I have an ASP.NET webforms webpage that displays a grid of data extracted from a database using EntityFramework and Model Binding.
There are occasions however when I wish to override the displayed information depending of the value of one of the fields in the data.
So for example if the field 'hideme' is true then I want to replace the person's name with "Withheld" instead of displaying "Joe Bloggs", which is the value stored in the database.
Note: I do not want to change the value held in database, I just want to intecept it on the way to the web page and alter it's value.
The Webpage grid TemplateField looks like this:
<asp:TemplateField HeaderText="Company" SortExpression="">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Item.VehicleKeeper.Name %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The code-behind looks something like this (sorry, it's VB!):
Public Function VehicleGrid_GetData(sender As Object, e As EventArgs) As IQueryable(Of VehicleKeeperCollection) Dim mycontext = New RecallContainer()
Dim myreg As String = "AB01CDE"
Dim Q1 As IQueryable(Of VehicleKeeperCollection) = From vk In mycontext.VehicleKeeperCollections
Select vk
Q1 = Q1.Where(Function(x) x.Vehicle.Reg = myreg)
Return Q1
End Function
This returns a set of data containing, amongst other things, VehicleKeeperCollection.VehicleKeeper.Name and VehicleKeeperCollection.VehicleKeeper.HideMe
So what I want to do is change the value of VehicleKeeperCollection.VehicleKeeper.Name to "Withheld" if the value of VehicleKeeperCollection.VehicleKeeper.HideMe is true.
The ModelBinding to the grid requires the object I return to be of class "VehicleKeeperCollection" & I thought I might be able to execute the resultant query into evaluated objects, but I don't even seem to be able to do that and maintain the class type.
The code below fails to be able to cast the new object to class VehicleKeeperCollection
Dim mynewquery1 As IQueryable(Of VehicleKeeperCollection) = From q In Q1 Select New With
{.id = q.id, .VehicleKeeperId = q.VehicleKeeperId, .VehicleId = q.VehicleId, .Vehicle = q.Vehicle, .VehicleKeeper = q.VehicleKeeper}
Any pointers as to how I achieve what I want gratefully received, this isn't my area of expertise & I'm struggling!
Thanks

onClick vb.net function but prevent postback for JavaScript

I have seen various examples that use a onClientClick to avoid postback.
However, I don't want this. I want to contain the linkbutton's links within vb.net code and prevent the page from performing a postback when clicked. The reason for this is when my page loads it sets the default show() and hide() div displays again which loses the user's current spot on the page when the new window opens. So how can I do this without causing the main page from resetting?
I have tried OnClientClick="javascript:return false;" but prevented user from clicking linkbutton.
I have also tried AutoPostBack = "false" and it didn't work
Attempted other ways as well. ... no luck... including trying HyperLink but those gave me issues too.
<asp:linkbutton ID="aCapShip" runat="server" OnClick="aCapShip_Click" style="text-decoration:none;" >
<i class="fa glyphicon glyphicon-plane fa-lg"></i> Cap Shipping <!--<span class="arrow"></span>-->
</asp:linkbutton>
JavaScript
$(document).ready(function () {
$("#shipping").show();
$("#Ul2List").hide();
$("#manufacture").hide();
$("#Ul1List").hide();
$("#reList").hide();
.........
VB.net
Protected Sub aCapShip_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim url As String = "http://dummyinfo/dummyinfo/dummyinfo.aspx"
Process.Start(url)
End Sub
So my work around is the following:
VB.net
If Not Page.IsPostBack Then
startUp()
End If
Public Sub startUp()
Response.Write("<SCRIPT LANGUAGE=""JavaScript"">startSort()<" & "/" & "SCRIPT>")
End Sub
JavaScript
$(function startSort() {
$("#shipping").show();
$("#Ul2List").hide();
$("#manufacture").hide();
$("#Ul1List").hide();
$("#reworkList").hide();
});

Find Radiobutton Control from ID String in vb.net

I have 3 radiobuttons in my aspx:
<asp:RadioButton ID="rdoMasculin"
runat="server" AutoPostBack="true"
Checked="true" GroupName="gender"
TextAlign="Right" />
<asp:RadioButton ID="rdoFeminin"
runat="server" AutoPostBack="true"
GroupName="gender" />
<asp:RadioButton ID="rdoAnonymous"
runat="server" AutoPostBack="true"
GroupName="gender" />
When sending the form I'm storing the ID of the checked radiobutton in a Session variable:
for example: Session("currentGender") = rdoZukuTermine.ID
When I'm getting back on the page in a later stage I'm using the following code in codebehind:
Dim currentRadio = CType(FindControl(Session("currentGender")), RadioButton)
currentRadio.Checked = True
Session.Remove("currentGender")
currentRadio ends up as a null reference. But when I'm checking the Session it contains the right ID as a string.
Can someone help me?
Is the RadioButton control directly in your .aspx or does it have any parent controls? if yes call Findcontrol() on it direct parent like this:
Dim currentRadio = CType(ParentControl.FindControl(Session("currentGender")), RadioButton)

Disable ASP Button if Variable is null

I'm sure this is simple, I need to disable a button on my html page if a variable doesn't have a value from inside the code.
<asp:Linkbutton ID="eastMinute" runat="server" CssClass="btn btn-default" Text="Open Once" Width="95%" />
I've found several options for disabling the button if there is no text for form validation but I haven't found a way to actually connect the VB code with my ASPX page...
My thought was:
'Logic for button enable
If (username.Length < 0) Then eastOpen_Click.Enabled = False
Else eastOpen_Click.Enabled = True
But with that I get an argument not specified error.
Based on this from your sample:
<asp:Linkbutton ID="eastMinute" runat="server" CssClass="btn btn-default" Text="Open Once" Width="95%" />
Your button name is eastMinute, so your code should be:
'Logic for button enable
If (username.Length < 0) Then eastMinute.Enabled = False
Else eastMinute.Enabled = True
The eastOpen_Click sounds like an event handler; that is a function and therefore does not have an Enabled property.
You can also write it in one line like:
eastMinute.Enabled = (username.Length < 0)
What I ended up having to do for this to work properly was do the logic for if the value was passed or not and then add an attribute manually to my button using Attributes.Add().
'Logic for button enable
If (username Is Nothing) Then eastOpen.Attributes.Add("disabled", "disabled") Else

RegisterStartupScript doesn't work when firing from UpdatePanel under IIS 6.0 on Framework 2.0

I've looked everywhere but haven't found a solution. Hope you could help:
I'm maintaining a web site written in VB.NET and runs under Framework 2.0.
In one of my pages I have a drop down control inside an UpdatePanel:
<asp:UpdatePanel ID="pnlDropDown" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DropDownList ID="ddlTest" runat="server" Width="400"
OnSelectedIndexChanged="ddlTest_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="--- Please Select ---" Selected="True"></asp:ListItem>
<asp:ListItem Text="111"></asp:ListItem>
<asp:ListItem Text="222"></asp:ListItem>
<asp:ListItem Text="333"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
Inside the handler of this control, I have a call to ScriptManager.RegisterStartupScript:
Protected Sub ddlTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
ScriptManager.RegisterStartupScript(Me, GetType(Page), "Test", "alert('Test'); ", True)
End Sub
Now here is my problem:
When I choose a value from the drop down, the "RegisterStartupScript" method WON'T WORK, but it happens only when the project runs under IIS 6 and Framework 2.0.
- If I change it to run under IIS 7, it works perfectly.
- If I change it to run under Framework 4.0, it works perfectly.
- If I remove the UpdatePanel part, it works.
But if I don't change anything, it doesn't do anything.
Any solution before I change my whole code to work with jquery and ajax? :)
Thanks,
Orit.
Here is the workaround that I found in order to avoid the use of RegisterStartupScript. It's simple and works perfectly:
I added a hidden field inside the UpdatePanel named "hdnTest":
<asp:UpdatePanel ID="pnlDropDown" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:HiddenField ID="hdnTest" runat="server" />
<asp:DropDownList ID="ddlTest" runat="server" Width="400"
OnSelectedIndexChanged="ddlTest_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="--- Please Select ---" Selected="True"></asp:ListItem>
<asp:ListItem Text="111"></asp:ListItem>
<asp:ListItem Text="222"></asp:ListItem>
<asp:ListItem Text="333"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
On server side, I inserted a value to the hidden field inside the handler of the control:
Protected Sub ddlTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If x Then
hdnTest.Value = "true"
Else
hdnTest.Value = "false"
End If
pnlDropDown.Update()
End Sub
On client side, I added a pageLoad() function, that gets the value of the hidden field and acts according to it:
function pageLoad(){
if ($("#hdnTest").val() == "true")
alert('true');
else
alert('false');
}
Works just great.
Orit.