Thank you very much for taking the time to read this. I have an issue in which I was setting a default button for an ASP.NET page on page load from code behind, but now that I have multiple validation groups targeting one control, that is no longer working. Now, when I hit enter while in that control (textbox), validation for both groups are triggered minus the validation summary text.
Here is my examplefied code:
ASPX
<table>
<tr>
<td><asp:Textbox runat="server" ID="validateMe"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
<td><asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</tr>
<tr>
<td><asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup"/></td>
<td><asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup"/></td>
</tr>
<table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup"/>
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup"/>
C#
protected void Page_Load(object sender, EventArgs e)
{
this.Form.DefaultButton = firstButton.UniqueID;
}
If I use this and hit 'Enter' while inside of the textbox without typing anything into it, then neither of the validation summaries will appear but I will have two asterisks next to the textbox (one for each group). If the user presses 'Enter' I would expect a full validation using only the first group which is supposed to be assigned to the DefaultButton (here, 'firstButton'). Is there any way to achieve this functionality and initiate the client-side validation that would have happened had the user clicked 'firstButton' instead?
I have also tried wrapping the whole table plus the validation summaries inside of an asp:Panel and setting the DefaultButton there, but I received the same results. Any help or pointers in the right direction would be greatly appreciated!
Set
EnableClientScript="false"
in RequiredFieldValidator control. It will help.
<asp:Panel runat="server" DefaultButton="secondButton">
<table>
<tr>
<td>
<asp:TextBox runat="server" ID="validateMe"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup" /></td>
<td>
<asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup" /></td>
</tr>
</table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup" />
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup" />
</asp:Panel>
Related
Here is my asp.net Repeater From within it there is a button. I want to get the value of lblFaulType in a string in code behind when i click Button1, How can I do that?I have tried many approaches but nothing working. Please give me your suggestions.
<asp:Repeater ID="RepterDetails" runat="server">
<HeaderTemplate>
<table style="border:1px solid #0000FF; width:500px" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<tr>
<td >
<strong><asp:Label ID="Label18" runat="server" Text="Fault Category: " Font-Bold="true"/></strong>
<asp:Label ID="lblFaultType" runat="server" Text='<%#Eval("Fault Type") %>' Font-Bold="true"/>
</td>
<td >
<strong><asp:Label ID="Label19" runat="server" Text="Fault Description: " Font-Bold="true"/></strong>
<asp:Label ID="Label8" runat="server" Text='<%#Eval("Fault Description") %>' Font-Bold="true"/>
</td>
</tr>
<tr>
<td >
<strong><asp:Label ID="Label2" runat="server" Text="Building: " Font-Bold="true"/></strong>
<asp:Label ID="Label9" runat="server" Text='<%#Eval("Name") %>' Font-Bold="true"/>
</td>
<td >
<strong><asp:Label ID="Label3" runat="server" Text="Floor: " Font-Bold="true"/></strong>
<asp:Label ID="Label10" runat="server" Text='<%#Eval("FloorNo") %>' Font-Bold="true"/>
</td>
</tr>
<tr>
<td>
<strong><asp:Label ID="Label6" runat="server" Text="Room Number: " Font-Bold="true"/></strong>
<asp:Label ID="Label11" runat="server" Text='<%#Eval("RoomNumber") %>' Font-Bold="true"/>
</td>
</tr>
<tr>
<td ><strong><asp:Label ID="Label14" runat="server" Text="Created Time: " Font-Bold="true"/></strong> <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("Time") %>'/></td>
<td ><strong><asp:Label ID="Label15" runat="server" Text="Created Date: " Font-Bold="true"/></strong><asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("Date") %>'/></td>
</tr>
<tr>
<td ><strong><asp:Label ID="Label16" runat="server" Text="Start Status: " Font-Bold="true"/></strong> <asp:Label ID="Label12" runat="server" Font-Bold="true" Text='<%#Eval("StartStatus") %>'/></td>
<td ><strong><asp:Label ID="Label17" runat="server" Text="Assign Status: " Font-Bold="true"/></strong> <asp:Label ID="Label13" runat="server" Font-Bold="true" Text='<%#Eval("AssignStatus") %>'/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td >
<asp:TextBox ID="lblComment" runat="server" style="color:#000000; font-weight: bold;" placeholder="Describe the fault here" Text='<%#Eval("Comment") %>' TextMode="MultiLine" Columns="70" Rows="6" Enabled="false"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" class="text-center"><asp:Button ID="Button1" runat="server" Text="Assing Fault" class="btn btn-success" OnClick="Button1_Click" /> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
We may need to see the CodeBehind for this page, and specifically the "Button1_Click" method (click event).
Now though, it appears as though you only have one of the labels with an ID of "lblFaultType" (maybe you misspelled that in your initial description. Each of the other labels all carry sequentially numbered IDs (e.g. ID="Label1", ID="Label2", ID="Label3", etc), and your ClickEvent (the Button1_Click method) will need to iterate through those items to know 'which' label you are attempting to obtain a value from.
Suggestion: It may be better to apply a ClickEvent on each of the particular labels since it will have the ID there with it (a JScript/JQuery solution).
This actually a GREAT question!
(in fact good interview question)
Why?
Because how.net works for a gridview, listview, or a repeater?
They are ALL very similar.
If one has failed to grasp the “most basic” event model in .net, then suggestions to use JavaScript, or all kinds of wild approaches will be suggested (and they would get a failing mark in answering this question).
When using data bound controls that repeat data, then you can set a special command to trigger the Itemcommand event.
The attribute setting is called “CommandName”.
There are built in commands – and the command you use is important. However, "just" setting command name to somthing of your liking is sufficient in most cases.
This will CAUSE the ItemCommand event will fire. And once that occures, then you have "itemIndex" to point to any given row in the repeater (or gridview, or listview, or form view - the list goes on!!!).
So, add this to your button:
<asp:Button ID="Button1" runat="server" Text="Assing Fault"
class="btn btn-success" OnClick="Button1_Click"
CommandName = "Select" CommandArgument = '<%#Eval("Fault Type") %>'/>
Now, the the ItemCommand event, you can do this:
Protected Sub RepterDetails_ItemCommand(source As Object, e As RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
Debug.Print("item command")
Dim MyRow As RepeaterItem = e.Item
Dim h As Label = e.Item.FindControl("lblFaultType")
Debug.Print(h.Text)
End Sub
Of course, since we set the CommandArguemnt to pass the value we want, then of course you can also do this:
debug.print("Value of FaultType = " & e.CommandArgument)
So, be it a classic "data aware" control in .net? Well be it a listview, gridview, repeater, and MANY MORE, then use the built in "gift horse" features that of course expected this common need and solution.
And you can get the selected index into the repeater with this:
Debug.Print(e.Item.ItemIndex)
Thanks guys but i found the answer at https://youtu.be/Oltxy1sGIds
So i did it like this:
RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem; string type = +(item.FindControl("lblFaultType") as Label).Text;
I have used compare validator to check whether the selected date is valid or it. The issue here is it only fires up when the submit button is clicked, is it possible to check when the user selects the date.
<tr id="trow" runat="server">
<td class="auto-style3">Need Duration</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox1" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgJoin" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox1" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator></td>
<td>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgHide" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="TextBox2" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
It has been a while, but I think you need to enable client side validation scripts by adding:
EnableClientScript="True"
Example
<asp:CompareValidator ID="CompareValidator1" EnableClientScript="True" runat="server"
Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
It's documented at msdn.
Aditionally, I do know that custom validators often lack a correct implementation of the javascript. I am not sure how the CompareValidatorbehaves in that sense.
You might need to create a inherited class, to implement the scripts fully. Before going there, try do research a bit.
For example, here is a solution with a custom validator
I am a beginner in C#, ASP.NET.
What i want is when clicks 'submit' button, need to display a message 'Your registration is succesful'.
But When i clicks here, nothing showing, but it showing message in other pc with same code.
Somebody can help me ? is there any configuration errors in my project ?
code here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
Response.Write("Your registration is succesful"); // Not displaying
//MessageBox.Show("Test Display");
}
}
ASP.NET
<form id="form1" runat="server">
<div>
</div>
<table class="style1">
<tr>
<td class="style3">
Username</td>
<td class="style6">
<asp:TextBox ID="uname" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="uname" ErrorMessage="Username is required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
Email</td>
<td class="style6">
<asp:TextBox ID="email" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="email" ErrorMessage="Email is required" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="email" ErrorMessage="You must enter the valid email id"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style3">
Password</td>
<td class="style6">
<asp:TextBox ID="pass" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="pass" ErrorMessage="Password is required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
Confirm Password</td>
<td class="style6">
<asp:TextBox ID="passr" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="passr" ErrorMessage="Confirm password is required"
ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="pass" ControlToValidate="passr"
ErrorMessage="Both passwords must be same" ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="style3">
Country</td>
<td class="style6">
<asp:DropDownList ID="country" runat="server" Width="180px">
<asp:ListItem>Select Country</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
<asp:ListItem>GERMANY</asp:ListItem>
<asp:ListItem>FRANCE</asp:ListItem>
<asp:ListItem>INDIA</asp:ListItem>
</asp:DropDownList>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="country" ErrorMessage="Select a country name"
ForeColor="Red" InitialValue="Select Country"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td class="style7">
<asp:Button ID="submit" runat="server" Text="Submit" />
</td>
<td class="style5">
<input id="Reset1" type="reset" value="reset" /></td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style6">
</td>
<td>
</td>
</tr>
</table>
</form>
You need to understand the difference between server-side and client-side.
Server-side is everything that happens on the server (e.g. ASP, ASP.NET, PHP) that is used to create the HTML that is sent to the browser. It also deals with the information that is returned by the browser when the user submits stuff back (the post-back).
Client-side is everything that happens on the browser once the HTML has been received, or when user does something on the page like clicking an element.
What you are doing is trying to run a Windows Application style MessageBox.Show on the server... that's not going to work.
If you want the browser to display an "alert" window (a bit like a MessageBox) then you need to send client-side script to the browser. Try this...
protected void submit_Click(object sender, EventArgs e)
{
Response.Write("Your registration is succesful");
var script = "window.alert('Test Display');";
ClientScript.RegisterStartupScript(this.GetType(), "message", script, true);
}
As per the comment by the OP...
My problem is Response.Write("Your registration is succesful"); is not working
Instead of using Response.Write use a <asp:Literal> control (which gives you the advantage of positioning the control exactly where you need it) and set the .Text property of it (remember that this will remain on post-back, so you might need to clear it).
You could also use a <asp:Label> which would not only allow you to position it, but you can also including .CssClass or .Style attributes for better formatting.
If you want to show confirmation message box try to use jquery on your .aspx page. You can add function like this
function confirmationAccept() {
return confirm("Accept?");
}
And than add properties onclientclick like this:
<asp:Button ID="addButton" runat="server" Text="Add" OnClientClick="return confirmationAccept()">
You can show javascript alert message by using ClientScript.RegisterStartupScript.
I have a captcha control, when write code and check it it always be wrong if I write it correctly , then form the second time it works fine.
what may be the problem in that
that is the captcha
<td id="Td6" runat="server" align="center" class="style4">
<MSCapatchaControl:CaptchaControl ID="Capatcha"
runat="server"
BackColor="#CC3300"
CaptchaBackgroundNoise="Extreme"
CaptchaChars="ACDEFGHJKLNPQRTUVXYZ2346789"
CaptchaHeight="40"
CaptchaMaxTimeout="240"
CaptchaMinTimeout="5"
CaptchaWidth="130"
FontColor="White"
ForeColor="White"
LineColor="Black"
NoiseColor="Black" />
</td>
<td id="Td7" runat="server" colspan="3">
<asp:TextBox ID="registerCaptchText"
runat="server" MaxLength="6" Width="135px">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="registerCaptchText"
Display="None"
ErrorMessage="Enter the capatcha">
</asp:RequiredFieldValidator>
<font style="font-size:12px; font-family:trebuchet; color: rgb(51, 51, 51);">
<div class="important">*</div>
Please enter the text you see in the image.
</font>
</td>
and that is the function test it
Capatcha.ValidateCaptcha(registerCaptchText.Text);
bool capatchaValid = Capatcha.UserValidated;
Hi I have solved this problem by taken captcha field in update panel.
Thanks Every one for your value able time.
Can we submit a form in ASP.NET with AjaxToolkit ?
e.g:
below in our form:
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div id="mainDiv">
<table style="width: 40%;" cellspacing="3" cellpadding="3">
<tr>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorUsername" runat="server" ErrorMessage="*"
SetFocusOnError="True" Text="*" ControlToValidate="TextBoxUsername"
ValidationGroup="login"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxUsername" runat="server"></asp:TextBox>
</td>
<td align="left">
: UserName
</td>
</tr>
<tr>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
SetFocusOnError="True" Text="*" ControlToValidate="TextBoxPass"
ValidationGroup="login"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxPass" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td align="left">
: Pass
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="ButtonSubmit" runat="server" Text="Login"
ValidationGroup="login"/>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="LabelResult" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
I wanna log-in a user with Ajax so I have to send username and pass to a Webservice and get the result and show the result in LabelResult.
Can we do it with AjaxToolkit in ASP.Net?
I'm using ajax for a cascading dropdown menu set and i don't know how to submit the contents of the form to a controller. I think this is a related question becausewht I did was generate a hidden ajax control that triggered upon the change in value of the final ajax control in the form. The webservice that the control triggered then saved al of the form information to a database at which point the submitbuton was more of a comforting decoration for the user. Once the user submitted the form the values were available in a meta data field in the database and the submit handler accessed those values rather than reading them from the postback.
I didthis because it ended up being faster than learning what I needed to in order to read them form the postback using java script or some other method of retrieval form the postback so I wouldn't claim thatthisis the best approach but it worked for me. Hope this helpsyou somehow.
D.A.P.
for working of AJAX script manager must appear before the form which i think not possible and also
Cos Callis says very well
"If you want to submit the form, that is what a post back is for"
So, If we wanna send a form to server with Ajax, we have to use something like JQuery, and we can't do it with AjaxControlToolkit.