asp.net CompareValidator not running as expected on submit - asp.net

I'm trying to get a CompareValidator to work but having a few problems. If the submit button does not run a function it works fine, but the validator does not work if there is a function attached to the button.
Here's the code.
Sub myGo(sender As Object, e As EventArgs)
response.redirect("http://www.google.co.uk")
End Sub
and
<form runat="server">
<asp:TextBox id="txt1" runat="server" /> = <asp:TextBox id="txt2" runat="server" /> <asp:Button OnClick="myGo" Text="not working with onclick" runat="server" /> <asp:Button Text="working button" runat="server" />
<br />
<asp:CompareValidator EnableClientScript="false" id="compval" Display="dynamic" ControlToValidate="txt1" ControlToCompare="txt2" Type="String" Text="Validation Failed!" runat="server" />
</form>
any ideas?

The validator should work fine, but you probably ought to perform the redirect only if the page is valid:
Sub myGo(sender As Object, e As EventArgs)
If Page.IsValid Then
Response.Redirect("http://www.google.co.uk")
End If
End Sub

Use ValidationGroup="...."

Related

Textbox validation to make sure has integer values only

Example textbox
Hi All, how can i add validate expression to only accept numeric and space in my textbox? Example as image above. I try this way ValidationExpression="[0-100' ']{100,}" but not correct..
<asp:TextBox ID="txtCopied" runat="server" TextMode="MultiLine"
Height="200px" Width="617px" />
<asp:RegularExpressionValidator ID="rvDigits" runat="server"
ControlToValidate="txtCopied" ErrorMessage="Enter numbers only till 100
digit" ValidationGroup="valGroup" ForeColor="Red" ValidationExpression="
[0-100' ']{100,}" />
<asp:RegularExpressionValidator ID="rvDigits" runat="server"
ControlToValidate="txtCopied"
ErrorMessage="Enter numbers only till 100
digit" ValidationGroup="valGroup" ForeColor="Red"
ValidationExpression="^[\d ]*$" />
I tried using ServerValidate instead of RegularExpressionValidator and tried to mess around with it.
<form id="form1" runat="server">
<asp:TextBox ID="txtCopied" runat="server" TextMode="MultiLine" Height="200px" Width="617px" MaxLength="2000" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCopied" OnServerValidate="TextValidate"
ValidateEmptyText="True" SetFocusOnError="True" ErrorMessage="Enter numbers only till 100 digit" Display="Dynamic" />
</form>
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub TextValidate(source As Object, args As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
Dim strRegex As String = "\b(0{0,2}[1-9]|0?[1-9][0-9]|1[1-9][0-9]|100)\b"
Dim re As New Regex(strRegex, RegexOptions.Multiline)
Dim ss As String() = args.Value.Split(" ")
For i = 0 To ss.Length - 1
args.IsValid = re.IsMatch(ss(i))
Next
End Sub
End Class

Required field validator not responding or firing

I have several text boxes on my website, each with a required field validator, and a submit button on the bottom. The submit button works whether or not I have entered values into the text boxes. Below is the code:
<asp:TextBox ID="TextBoxName" ValidationGroup="textgroup" runat="server"
ToolTip="Insert the Pokemon's name" CssClass="upper-case" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ValidationGroup="textgroup" Text="Names only please" ControlToValidate="TextBoxName"
ValidationExpression="^[a-zA-Z]*$" CssClass="required-field" />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="textgroup"
runat="server" Text="Required Field" ControlToValidate="TextBoxName"
CssClass="required-field" />
<br />
Here is the submit button:
<asp:Button ID="Button1" runat="server" Text="Insert Card" ValidationGroup="textgroup" />
And the code-behind for the submit button:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Page.Validate()
CardCollectionDataSource.Insert()

Ajax Extension (Timer) stopped working when I retrieve value from DataTable

Currently I have an Ajax Timer which executes a function every few second. It is working perfectly fine until I tried to retrieve data from a DataTable. I have no idea why. I have tried debugging. I even placed label on the page to check.
For example the Ajax Timer:
Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Label1.Text = DateTime.Now.ToLongTimeString()
End Sub
The datalist (the one giving the problem):
Protected Sub dlOrgProfile_ItemCreated(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlOrgProfile.ItemCreated
Dim bizLayerMgmt As BlOrganizations
Dim dt As DataTable
bizLayerMgmt = New BlOrganizations()
dt = bizLayerMgmt.getOrgDetails(userId).Tables(0)
ddl = CType(e.Item.FindControl("ddlCoType"), DropDownList)
Dim value As Integer = Convert.ToInt32(dt.Rows(0)(3)) 'I have narrowed the problem to this line, if I comment this line.. everything works perfectly
ddl.SelectedValue = value
End Sub
The problem lies in the dt.Rows(0)(3). I have no idea why. I need to use it to retrieve some data from the database.
Just in case if its the front-end side.. here's the markup for the site.
<div class="content">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000">
</asp:Timer>
<asp:DataList ID="dlOrgProfile" runat="server" DataSourceID="odsOrgDetails"
EnableTheming="True" RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">
<ItemTemplate>
<h3>
<asp:TextBox ID="txBxCoName" runat="server" Text='<%# Eval("OrgName") %>'></asp:TextBox>
<div class="ddlSelect">
<asp:DropDownList ID="ddlCoType" runat="server" DataSource='<%# listOrgType() %>' DataTextField="OrganizationType" DataValueField="OrgTypeID" >
</asp:DropDownList>
</div>
<h3>
</h3>
<a id="linkCoImg" href="upload_co_logo.aspx">
<asp:Image ID="CoImg" runat="server" ImageUrl="~/logo/org/default.png" />
<span>Change</span> </a>
<br />
<div id="description">
<textarea id="taCoDesc" rows="2" cols="1"><%# Eval("Description") %></textarea>
</div>
<br />
<asp:Label ID="lblContacts" runat="server" Text="Contacts:"></asp:Label>
<br />
<div id="contacts">
<asp:TextBox ID="tbContactOffice" runat="server" CssClass="tbContacts"></asp:TextBox>
<asp:TextBox ID="tbContactFax" runat="server" CssClass="tbContacts"></asp:TextBox>
<asp:TextBox ID="tbContactMail" runat="server" CssClass="tbContacts2"></asp:TextBox>
</div>
<br />
<asp:Button ID="btnSave" runat="server" CommandArgument='<%# Eval("OrgID") %>'
CommandName="save" Text="Save" />
</h3>
</ItemTemplate>
</asp:DataList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Okay I have resolved my own problem.. Still don't really get it why but this is what I did.. Instead of using ItemCreated, I used ItemDataBound.
Protected Sub dlOrgProfile_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlOrgProfile.ItemDataBound
Dim bizLayerMgmt As BlOrganizations
Dim dt As DataTable
bizLayerMgmt = New BlOrganizations()
dt = bizLayerMgmt.getOrgDetails(userId).Tables(0)
ddl = CType(e.Item.FindControl("ddlCoType"), DropDownList)
Dim value As Integer = Convert.ToInt32(dt.Rows(0)(3))
ddl.SelectedValue = value
End Sub

asp.net update pane will not fire event

I am trying to implement an update panel on my web page. when I add this, everything works fine:
<script runat="server">
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label2.Text = Date.Now.ToString
End Sub
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<fieldset>
<asp:Label ID="label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="TextBox1" runat="server" Height="289px" TextMode="MultiLine"
Width="663px" ReadOnly="True"></asp:TextBox>
The problem comes when I try to do some stuff on the on_load event of the application. in the code behind, i try to do this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
If Not IsPostBack Then
....a function that produces a very long xml string
Me.TextBox1.Text ="<f"
End If
End Sub
This will cause the event to not occur. If i change the "<" from the string, all is well. why cant I have "<" in my strings? This is important because i wont allow me to put an xml string in the text box.
any help would be great!
Try using &lt< rather than <

AJAX ModalPopup update panel contents based on change in panel controls

I have an AJAX Modal Popup panel that contains a RadioButtonList, 2 labels and 2 DropDowns. I want to update the Labels and DropDowns when a radio button is selected. My attempt at this posts back which causes the ajax popup to disappear.
aspx called on image click:
<asp:Panel ID="pnlModalContainer" runat="server">
<asp:RadioButtonList ID="rblTest" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblTest_SelectedIndexChanged">
<asp:ListItem Text="Test 1" Value="1" Selected="True" />
<asp:ListItem Text="Test 2" Value="2" />
</asp:RadioButtonList>
<br />
<asp:Label ID="lblFoo" Text="Foo" />
<asp:Label ID="lblBar" Text="Bar" />
<asp:DropDownList ID="ddlDogs" runat="server" DataSourceID="odsDogs" DataTextField="Dog" DataValueField="DogID" />
<asp:DropDownList ID="ddlCats" runat="server" DataSourceID="odsCats" DataTextField="Cat" DataValueField="CatID" />
</asp:Panel>
Code Behind (vb.net):
Protected Sub rblTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rblTest.SelectedIndexChanged
' ???
' Make it change lblFoo.Text and lblBar.Text as well as the DataSource for the DDLs
End Sub
You need to add an UpdatePanel within pnlModalContainer.

Resources