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>
Related
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.
I have an ASP.net textbox that the user needs to enter emails. I need to detect when the user types a semi-colon(;) then unhide a textbox and set the focus to that textbox.
Disclaimer: This might seem a little "hacky" and I'm sure there are better ways of doing this.
You can use the onkeypress of the textbox, set that to a JavaScript function. In the JavaScript function, programmatically "unhide" and give focus to your other textbox on the client-side. No post-back required.
JavaScript:
function checkForSemicolons(event) {
var txtEmail = document.getElementById("<%= txtEmail.ClientID %>");
if (event.keyCode === 59) {
// unhide other textbox and give focus to it
}
}
ASPX:
<asp:TextBox ID="txtEmail" onkeypress="checkForSemicolons(event)" runat="server" />
OnKeypress event is fired from the client side, so you need do catch that from the client script.
You can use javascript to cause a postback and than send the parameters to the server-side, but usually you can handle all from the client.
On jquery you would have somethig like this:
$("TextboxID").keypress(function(e){
//do something
//If you want to cause a postback use " __doPostBack('EventTaarget','Parameters');"
//you can capture the key that was pressed on e.keycode (for keycodes list, check: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes)
})
On the code behind you can add some code to capture this event on page_load.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Me.Request("__EVENTTARGET") Is Nothing Then
'do something - you can use the Parameter with: Me.Request("__EVENTARGUMENT")
end if
end sub
I am in the process of adding a filter to asp grid. I have succeeded in showing a text box for filtering in header.
I need to fire server code for filtering whenever user presses enter key in textbox.
So I started with adding event like
txtFilter.Attributes.Add("onkeyup", keyUpScript);
Then I added client script as
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "registerkeyUPScript", registerkeyUPScript, true);
where
string keyUpScript = "keyUPScript(event);";
string registerkeyUPScript = "function keyUPScript(e){\n"
+ "var unicode=e.keyCode? e.keyCode : e.charCode;\n"
+ "if(unicode == '13')\n"
+"{\n"
+" //PostBack code"
+"}"
+"}";
Now how could I postback when ever user enters a string in text box and presses enter key. I also need to rebind the filtered data back to grid
Any help will be appreciated.
You can try using this line of code to attach an onkeyup to your TextBox to only fire a postback event when Enter is pressed. Also, just in case you want to attach an OnTextChanged event for that particular TextBox, you can do so and that will be called as well when the page postbacks.
Use this in your Page_Load event :
TextBoxID.Attributes.Add("onkeyup", "return (event.keyCode==13);");
And this is the OnTextChanged event just in case if you want to attach :
protected void T1_TextChanged(object sender, EventArgs e)
{
//your logic goes here
}
Hope this helps.
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?');"
I want to call javascript function and click method on single asp button click event.
I was trying with following code.
<asp:LinkButton ID="lbtnSave" runat="server"
OnClientClick="myFunction();"OnClick="lbtnSave_Click">Save</asp:LinkButton>--%>
//javascript
function myFunction() {}
//code behind method
protected void lbtnSave_Click(object sender, EventArgs e) {}
Thanks in advance.
#Aparna, as you are using validation control than i believe those validation controls get triggered when button is clicked.
To overcome this issue there are two alternates either define validationgroup to your validation control or set the CausesValidation = false of your link button.
Example
<asp:LinkButton ID="lbtnSave" runat="server" CausesValidaion="false"
OnClientClick="myFunction();"OnClick="lbtnSave_Click">Save</asp:LinkButton>
Hope this will help !!
If you want call the js function on client click use this:
<asp:LinkButton ID="lbtnSave" runat="server" OnClientClick="myFunction()" OnClick="lbtnSave_Click">Save</asp:LinkButton>
to call it in a method(codebehind) u can use this:
ScriptManager.RegisterStartupScript(this,GetType(), "myFunction", "myFunction()", true);