jQuery after onclick - asp.net

I've a form with different field, a validation summary and a button Sign up!
When I click on Sign up, if a field is empty, it active validation summary and until here there's no problem.
So the problem is that I would want active a jQuery script after it was clicked Sign up button and it appeared validation summary.
I try with
$(this).load(function()
but in effect it occur when the page is load and not after the button is clicked.
I try also use button's onclick ,but I have to use script after onclick and not while.
I used Validation group for all field of form, also button and for this reason it seems doesn't postback!

Um your question is a bit unclear to me but wouldn't this work ?
$("#sign_btn").click(function(){
alert("I was clicked");
});
sign_btn is the ID of the button.

Related

how to make asp:LinkButton work in safari when enter pressed

I have asp:LinkButton (submit button) and it works properly in every browser. However, in safari it works only if user clicks the submit button and doesn't work if enter is pressed. How can I solve this? I just want this to work similarly in every browsers.
Thanks in advance :)
A LinkButton control turns into an <a> tag in html with a javascript onclick event which will trigger a postback to your webform. What I would do is view the source of the page and find this onclick javascript code, then follow the instructions in this stackoverflow post to add an enter key handler to your linkbutton:
JS enter key press handler
In the event handler for the enter key, put in the onclick code you found from the html source.

.Net textbox is null when a "submit" link is clicked

Good day,
I have a problem in a .NET page where I am using an asp:textbox in combination with an OnClick action on a link button.
What happens is that after text has been entered into the textbox, if you directly click on the link button, more often than not the textbox is considered to be null.
If you click off the text box first then click the link, all is well and the save function performed by the link button proceeds as expected.
My assumption is that there is a lifecycle event that is being missed, or not applied which is not binding the text to the textbox for use in the codebehind when the link button is clicked.
The question is, what can i do to enforce that binding short of doing something like adding an onkeypress event to the textbox to force a postback.
There must be a more elegant solution.
Thank you in advance for your help.
Do you have initializations on your textbox inside Page_Load event? If so, use IsPostBack=false and put the initialization inside.
If IsPostBack =False Then
TextBox1.text=""
End If

How do you clear a CustomValidator Error on a Button click event?

I have a composite User control for entering dates:
The CustomValidator will include server sided validation code. I would like the error message to be cleared via client sided script if the user alters teh date value in any way. To do this, I included the following code to hook up the two drop downs and the year text box to the validation control:
<script type="text/javascript">
ValidatorHookupControlID("<%= ddlMonth.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
ValidatorHookupControlID("<%= ddlDate.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
ValidatorHookupControlID("<%= txtYear.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
</script>
However, I would also like the Validation error to be cleared when the user clicks the clear button. When the user clicks the Clear button, the other 3 controls are reset. To avoid a Post back, the Clear button is a regular HTML button with an OnClick event that resets the 3 controls. Unfortunately, the ValidatorHookupControlID method does not seem to work on HTML controls, so I thought to change the HTML Button to an ASP button and to Hookup to that control instead. However, I cannot seem to eliminate the Postback functionality associated by default with the ASP button control. I tried to set the UseSubmitBehavior to False, but it still submits. I tried to return false in my btnClear_OnClick client code, but the code sent to the browser included a DoPostback call after my call.
btnClear.Attributes.Add("onClick", "btnClear_OnClick();")
Instead of adding OnClick code, I tried overwriting it, but the DoPostBack code was still included in the final code that was sent to the browser.
What do I have to do to get the Clear button to clear the CustomValidator error when clicked and avoid a postback?
btnClear.Attributes.Item("onClick") = "btnClear_OnClick();"
Try adding a return false; at the end of your onClick call. That should prevent the default behavior (in this case submit).
btnClear.Attributes.Add("onClick", "btnClear_OnClick(); return false;")
Are you wanting to clear the error message on the client side if they fix the error without having to click on anything right?
I did something like this if I have your issue right but calling a revalidation function that called some client side javascript code and then hides the span that the error message is in if they fixed the issue.
See my blog article and let me know if this is what you are wanting to solve. The part you want to read is towards the bottom.
http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx

calling __dopPostback from javascript where target control is an ASP.Net custom control

I need to pop up a jquery dialog as confirmation before a submit happens. I don't want the dialog to always pop up, that depends upon the setting of a dropdown.
The submitting control (which I can't change by the way) is a custom control with a save and a cancel button and handlers for the save and cancel buttons.
So when the user has selected a particular item from the drop down I hook up the click event (on the clientside) of the save button (that has been rendered by the custom control) to a js function using jQuery, that calls my showDialog() function and then returns false (to prevent the postback).
I now need, when the user clicks the yes button on the dialog, to call __doPostBack to get the SaveButtons serverside events to fire and to get the serverrside validation to run.
I've got
eval($("#<%= hdnBtnPostback.ClientID %>").val());
in the handler for the Yes button on the dialog.
The hdnPostBack field contains the result of:
Page.ClientScript.GetPostBackEventReference(mySaveCancelCustomControl, String.Empty)
Unsuprisingly this isn't working. The mySaveCancelCustomControl is the custom control mentioned above and it has two buttons that are rendered with two event handlers so the GetPostBackEventReference is doomed to fail.
Any ideas what I should do?
You can make the button a dialog button and put a asp.net button on the page with the ID of HiddenButton and wrap it in a div with a style of display:none; so the button is not visible. (Must not be Visible="false" so the button is rendered)
In the dialog javascript add a button that has the effect of clicking the hidden button
jQuery("#dialog").dialog({
buttons: {
'ButtonText': function() {
__doPostBack('<%# HiddenButton.ClientID %>', '')
jQuery(this).dialog('close');
}
}
});
and add a asp click handler to the hidden button
<div style="display:none;">
<asp:Button ID="HiddenButton" OnClick="HiddenButton_Click" ></Button>
<div>
Click event (HiddenButton_Click) on the serverside will fire when the dialog button is clicked
Hope this helps I use this pattern all the time
ps If ClientID does not work in the __doPostback() call user UniqueID I cant remember which it is.
When you are wiring up your new click on the client side, could you save the current click method into a temporary variable and then call the temporary variable from your Yes button?

Setting default button in asp.net 2.0

I have 3rd party user control (a captcha control), which has a captcha image, a text box within it.
I am using the above user control in my webpage. I have a 3 submit buttons on my webpage (Validate Captcha, Submit Page, Add User). When I click the Validate Captcha submit button using the mouse, I am validating whether captcha is empty and showing a javascript alert.
The problem comes when I enter the valid captcha text in the textbox and hit enter key when the cursor is in the textbox. The page just refreshes. I am unable to add keypress event to textbox and call Validate Captcha button event as I am using the 3rd party user control which I cannot modify.
Also, Page.ClientScript.RegisterHiddenField(...) will not work in my case as I have two other submit button inside the same page.
Only option left is to enclose these in panels and set default button.
Please let me know if anyone has any better options for achieving this.
Greetings! I too use alot of third party controls. The thing to remember about these controls, it that in the end they just emit HTML. This means you can use the DOM to access and attach event handlers such as onKeyPress. The trick is to identify how your control creator named the control you are looking for, in this case a {textbox}. The easiest way to achieve this is to simply run the page and view the page source. It is there that you can find the name as it is rendered and sent to the browser, after that all you have to do us use document.getElementByID to get the object and setup your handler
Example:
<script>
//Place this AFTER your textbox control is declared in the HTML
//Get the textbox
var textbox = document.getElementById('nameOfRenderedControlHere');
//Assign the event handler and function you want it to call
textbox.onclick = function() { validateCaptcha(); };
function validateCaptcha()
{ //Do your Stuff here }
</script>
That should be it..havent tested, let me knwo if you run into questions.
Put the captcha in its own <asp:Panel> and add a DefaultButton property for the panel with the ID of the captcha's submit button.

Resources