Asp.net OnClick event and OnClientClick event used together - javascript

suppose i have a create form and with button save data in database and i want to show that save successfully in div tag using JavaScript but when i click button then at a time on event work but not 2 event onclick and onClientClick ..
<here>
<asp:Button runat="server" CssClass="myButton" Text="Registration" ID="btnRegistration" OnClick="btnRegistration_Click " OnClientClick="return YourJavaScriptFunction();" Height="65px" Width="184px" ClientIDMode="Predictable"></asp:Button>
and
<script type="text/javascript">
function YourJavaScriptFunction() {
$("#message").slideDown("slow");
// this will prevent the postback, equivalent to: event.preventDefault();
return false;
}
</script>
I want that first data saved then work onClientClick event in one asp:button

You need to tell your Javascript code to display the message on page load after the button click postback event happens.
There are many ways to accomplish this.
One way is through a session
aspx code with js:
<% if(Session["ShowMessage"] != null){ %>
$("#message").slideDown("slow");
<% } %>
Code behind on Page_Load:
Session["ShowMessage"] = null; //This will make sure that only the button will set the session state
on Button Click event
Session["ShowMessage"] = "Not null";
Another way is to Add Client Script Dynamically to ASP.NET Web Page

as #boruchsiper suggested, I will go for the second option by calling registerclientscriptblock after the postback has successfully completed.

Related

OnClientClick and OnClick relation not working on second hit

good evening!
I've been trying to use the OnClientClick and OnClick for a validation process. It means, I use the OnClientClick for a confirmation, and proceed with the OnClick if confirmed. The "no" is always ok, we are focusing on the "yes". It all goes well on the first hit, but on the second hit, the PostBack can be seen (it means the "click" was effective), but no action is triggered on code behind.
Here is the code:
<script type="text/javascript" >
function testExample(button) {
var confirmed = confirm("Are you sure?");
return confirmed;
}
</script>
<asp:Button ID="btnButton" runat="server" OnClientClick="if(!testExample(this)) return false;" OnClick="btnClicked" Text="Button Test"/>
On code behind there is nothing done, just a check if it was triggered:
protected void btnClicked(object sender, EventArgs e)
{
return;
}
Adding or removingUseSubmitBehavior="false" does nothing, as also setting ClientIDMode="Static" or AutoID.
Changing to OnClientClick="return testExample(this);" also has no impact. If I do on other buttons individually it happens the same, they all run the first time, the second fails. I am trying to avoid the CssClass and pageLoad() relation.
Can you help me figuring out what I am doing wrong or missing?
EDIT: This seems to happen every time there is a postback event, even after removing OnClientClick. This Button is not inside any place holder.
Change your OnClientClick event to
OnClientClick="return confirm('Are you sure?');"

Login control, set onauthenticate="LoginAuthenticateEvent" by keypress

I have asp.net Login control, example
<asp:Login ID="Login" onauthenticate="LoginAuthenticateEvent" runat="server"/>
have *.cs function like
protected void LoginAuthenticateEvent(object sender, AuthenticateEventArgs e)
{
//..
}
But it launch Authenticate only if pressing login Button, not by 'Enter' in textboxes.
So I decided to set this event to javascript 'onkeypress' event, and want it to check if keycode is 13(Enter) - and launch LoginAuthenticateEvent.
But there is problem - to call C# function from JS function should be static, but in my case it is 'protected void'
In JS(jQuery) code I get necessary element like this:
var tboxUserName = $("input[name*='UserName']");
I understand that there should be somethin like
tboxUserName.attr("onkeypress","....");
But how to hang there key check and make it such as onauthenticate="LoginAuthenticateEvent"?
upd.: Tried find control on Server side, and add TextChanged event there
TextBox tb = (TextBox)Login.FindControl("UserName");
tb.TextChanged += tbox_TextChanged;
But 'TextChanged' event fires only on focus change(when click other textbox or button) - but I need to check every keypress. So question is still opened.
Yes, solution from #Dave Becker is working. In my case my Login control is branded and there was not Button, but ImageButton. In this the code looks like this:
<asp:Panel ID="panelLogin" runat="server" DefaultButton="Login$LoginImageButton">
<asp:Login ID="Login" onauthenticate="LoginAuthenticateEvent" runat="server">
</asp:Login>
</asp:Panel>

How to trigger server side button's click event after javascript confirm dialog returns true

I have an input as count named in a repeater with hdncount hidden input. And I have a server side button out of repeater. This script runs onblur event of count. I want to trigger server side button's click event if client click OK button on confirmation. What should I do to do that?
<script type="text/javascript">
function changeItemCount(input) {
var hdnCount = $(input).closest("div").find("input[id=hdnCount]").val();
var crrCount = $(input).closest("div").find("input[id=count]").val();
if (hdnCount != crrCount) {
var answer = confirm("Ürün adedi değiştirilecektir. Onaylıyor musunuz?");
return answer;
} else {
return true;
}
}
</script>
How to fire a button click event from JavaScript in ASP.NET
var answer = confirm("Ürün adedi değiştirilecektir. Onaylıyor musunuz?");
if(answer)
__doPostBack('btnSubmit','OnClick'); //use the server-side ID of the control here
Use OnClientClick together with OnClick
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" OnClientClick="return CheckGoJSEvents();" />
EDIT:
Didn't see that you had the function execute on the blur event. In that case you would not use the OnClientClick of the button. The answer posted by mattmanser would be correct.
If you wanted it to function from the button click, then you could use this method. Sorry for the confusion.

Make submit delay before POST

I have button
<UpdatePanel>
<asp:Button CssClass="btn-send" runat="server"
OnClick="SendMessage" Text="Send" />
</UpdatePanel>
Server side method
protected void SendMessage(object sender, EventArgs e){...}
I need to make little delay and show progress bar animation before POST. I would like to use javascript (jquery) for this.
How to make this delayed submit
1) without server delay
2) without second button with client script, which click server button?
You could try something like this...
<asp:Button runat="server" OnClick="SendMessage" Text="Send"
OnClientClick="return sendMessage(this);" />
<script type="text/javascript">
var sendBtn = null;
function sendMessage(btn){
if(sendBtn==null){
sendBtn = btn;
window.setTimeout(function(){ sendBtn.click(); }, 1000);
// DO YOUR VISUAL STUFF HERE
return false;
}else{
return true;
}
}
</script>
The idea is that the clicking of the button first stores the button object, sets a timeout to click the button, and then does the visual things you want to do.
Then after one second, the button is clicked again (this time not doing the timeout section)
(I have not had time to learn jquery yet, but I'm sure there is a simpler way to do this with it)
You can do this without jQuery, just using a regular Javascript setTimeout.
You can capture the onsubmit of your form, and then when you have submit (your button is pressed) you open your wait message of the page. So you add this line on Page_Load()
Page.Form.Attributes["onsubmit"] = "return AllowFormToRun();";
and the javascript:
<script>
function AllowFormToRun()
{
setTimeout(OpenWaitMsg, 10);
return true;
}
function OpenWaitMsg()
{
// open here the wait div
}
</script>
You should use Event.preventDefault() on the submit button click to prevent the page from submiting on click. Then start your progress animation, when its complete, call your form submit from the js.
$("form").submit(function(e){
var form = this;
e.preventDefault();
function animation(total,callback){
//perform your animation
callback();
};
animation(100,function(){
form.submit();
});
});

Can a texbox click event be captured?

Sorry to bother you again on this problem.
The solution I accepted from James Johnson didn't actually work because, it prevents me from entering any data into the textbox which is not what we want.
We would like to diplays the message as a help info only. In other words, we would just like to advise the the user to tick the checkbox if s/he wishes to receive an email.
Below is what I have come up with based on the reference hel from Rob.
This works. The only assistance I would like to get your help on is to get the message to open up in a a new window. THe sample message I am using is Focus Fire.
Can you please help?
THis is the latest code:
<head>
<style type="text/css">span id='1' {display:none;}</style>
<script type="text/javascript" src="jqueryMsg.js"></script>
</head>
<asp:TextBox ID="chck1" runat="server" Width="75px"></asp:TextBox>
<span id='1'>focus fire</span>
<script type="text/javascript">window.onload = (function () {
try{ $("input:text").focus(function () {
$(this).next("span").css('display','inline').fadeOut(1000); });
}catch(e){}});</script>
Add the ClientIDMode-attribute to you text box so that JavaScript can access it via the ID without fiddling with the ctrwhateverstrangeidaspnetproduces:
<asp:TextBox ID="instruct" runat="server" Width="75px" ClientIDMode="static">
And then handle the Click-Event (this example is using jQuery, you can use raw JavaScript as well):
$(document).ready(function() {
$('#instruct').click(function() {
alert('.click()-handler called.');
});
});
You could wrap the textbox in a SPAN, and add a onmouseover event to the span.
<span onmouseover="alert('This is a TextBox');"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></span>
Look into the onFocus() event handler for JavaScript which will execute a provided function when focusing on the desired textbox, as this method will work if the user clicks or tabs to the desired textbox.
Or you can use focus() in jQuery:
$('#instruct').focus(function(){
// Insert desired response here
alert('Your instructions');
});
One way is to add it in your codebehind in the Page Load event such as:
instruct.Attributes.Add("onclick", "yourJavascriptFunction();")
The reason for adding in the Page Load event is added Attributes are lost during postback.
Or you could use the onFocus event instead (in case the user tabs to the textarea instead of clicking on it, I'm assuming you want to display your message then as well):
$('textarea').focus(function(){
alert('hi');
}):

Categories

Resources