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.
Related
I have jquery buttons in my website,but some of these buttons doesnt have to clickable by every user so there must be permissions, all buttons make ajax calls so how can i control that buttons, for example if I have 3 buttons like:
button1_click()
some ajaxcalls
button2_click()
some other ajax calls
button3_click()
another calls
so admin can call this events, but simple user only have to call button3's
what kind of approach I need?
-Do i have to control every button using another ajax call like:
button1_click()
an ajax call (permission.aspx) read session,admin or not,return true or false
if(true)
{
someajaxcall()
}
-Or I have to hide or show buttons according to session? but i dont know how can i do that. I m using jqgrid asp.net components so that grid has custombuttons but that buttons doesnt have id so i cant reach it from serverside and also that buttons doesnt have visible or disable properties.
Im using asp.net and jquery
Normally I prefer to not show buttons that people can't use, so since you are using asp.net on the back end, you can hide the button when the page is created after checking the user's permissions, i.e:
btnOne.Visible = UserIsAdministrator; //you need to define thisvariable based on your own rules.
etc
If you want to show them anyway, you could do btnOne.Enabled = UserIsAdministrator instead, which will show the buttons, but disable them as appropriate;
you can use a hiddenfield on that page which will hold the user roles. then inside the DOM Ready() function iterate through those buttons to make them enabled/disabled.
However, you should also check the roles in the server side after an ajax call is made in case someone manually changes the value of the hiddenfield to enable the buttons.
I'm designing a website and I want my login page to catch "Enter" with all components, but i can not assign any Key or Mouse Events to TextBox ... After pressing to "Enter" a function should be run with 2 parameters (user name and password).
How can i do this?
If you wrap all the controls inside a standard HTML form then hitting enter in one of the text boxes will submit the form. You can then process the username and password entered on the server as with a normal form submission. Is that the behaviour you're looking for?
You don't need to trap any events on the client in this case so no need for JavaScript.
How to submit ASP.NET forms with the enter key
Read up on the defaultbutton property for forms and panels. This should get you what you want. From the site above:
ASP.NET 2.0 introduces a wonderful work around for this. By simply specifying the "defaultbutton" property to the ID of the , whose event you want to fire, your job is done.
The defaultbutton property can be specified at the Form level in the form tag as well as at panel level in the definition tag. The form level setting is overridden when specified at the panel level, for those controls that are inside the panel.
http://www.beansoftware.com/asp.net-tutorials/accept-enter-key.aspx
finally if found the answer. Here is ! The only problem is, I can not hide my button,
so that i put that button to web page as "Login" ... Defaultbutton could be hidden or not?
I want to show a modal dialog/pop-up window when the user has not filled in a text field on the page, instead of using an error text field on the page.
If the required text field is filled in, I want the submit button to work as normal (aka call the onClick function), but if it's not filled in, I want a dialogue window/modal pop-up to show up stating that they need to fill in the specific field.
What's the best way of this?
I personally prefer using the jquery ui library over the ajax control toolkit modal popup if possible.
All that you have to do is to do the following.
Create a JS function that does the validation, display the dialog if it errors. Have that function return true/false if validation is ok or fails.
On your button, add an "onClick" method to the attributes. "javascript: return YourFunction();" and you should be set to go.
The return value of your method will prevent postback if returning false!
I have a user control that has several buttons.
On page_load, I want to run a method unless a specific button was pressed.
When I check the sender on page_load inside the user control, I just get the name of the user control and not the button itself.
Is there a way that I can determine what button was pressed on page_load? Otherwise I will have to come up with some hacky method to solve the issue.
I think you can check Request.Form ("__EVENTTARGET") - that should contain the ClientID of your control.
This refers to the value of a hidden field the ASP.NET event handling framework uses to keep track of what the user clicked. When the user triggers a post-back, some JavaScript on the page sets this hidden field to the ClientID of the control you clicked before submitting the form.
Can you create a property in your user control to return the clicked button (or set a flag or whatever), an set it in each button's click event inside the user control?
Are you sure you're handling the page model correctly? The Page Load event happens (to build the server side object model), then your control is going to handle the button click event bound to the control.
Page Load can come for any number of postback reasons besides buttons in your user control being clicked.
What about buttons in other controls on the page?
There's sometimes good reasons to do this, but I also I worry that you're just hacking around the ASP.NET page model.
Here's a simple way to check if a specific button was pressed:
protected bool isButtonClicked(string buttonName)
{
bool isClicked = false;
foreach (string ctl in this.Request.Form)
{
if (ctl.EndsWith(buttonName))
{
isButtonClicked = true;
break;
}
}
return isClicked;
}
I have a master page with a search box and button at the top. This search functionality is taking over the "enter" key for all my web forms that use this master page. That is, if I have a login page that uses this master page and the user enters in their username/password and hits "enter", instead of logging in the user the system performs a search.
What's the best way to set up the default submit buttons. I could wrap everything in asp Panels and set the DefaultButton property, but that seems a bit tedious. Is there a better way?
use defaultbutton property of form or panel
<form defaultbutton=“button1” runat=“server”>
<asp:button id=“button1” text=“Same Page” runat=“server”/>
<asp:panel defaultbutton=“button2” runat=“server”>
<asp:textbox id=“foo” runat=“server”/>
<asp:button id=“button2” runat=“server”/>
</asp:panel>
</form>
As you have discovered default buttons can cause issues when there is more than one button on a page. I would take the Geeks suggestion but simplify it by removing the setfocus client script and extend it by adding the keydown event to both the search textbox and the login textboxes such that the enter key fires the correct button depending on if your user is using the search box or the log in box, or any other textbox you want to add the javascript to.
You can set focus on loading the page (in code behind if you like) to save the user some mouse work or tabbing if there is a sensible control for the user to start at, but otherwise the control the user is interacting with should determine the flow of the page and what the enter key does.
set focus on the text box ,
Page.RegisterStartupScript("SetFocus", "< script >document.getElementById('" + TextBox1.ClientID + "').focus();< /script >");
and then
In the keydown event for the last textbox you can do something like this:
If e.KeyCode = Keys.Enter Then
Me.Button1.Select()
End If
Rather than using javascript to manipulate the page you could put the search box and the submit button into an IFRAME on the master page. If the focus is in the iframe clicking will submit the search form, if the user is on your main form within the page they will submit the normal page.
The <iframe> src attribute points to a little self contained aspx page holding your text box and submit button which redirects to your search results form.
This is what we tried on our project which seemed to work. I changed the Search asp:Button to be an asp:LinkButton. I then added some CSS style to give it a background image to make it look like a button. LinkButtons are apparently not used by the page for determining which Button is the default action when pressing Enter. The sweet part was that LinkButton can still have click actions associated with them so I didn't have to change any of my code-behind stuff.