I looked through and through and found no clean solution for this. A colleague of mine did find a pretty dirty solution but I don't see why the solutions in the links reference below does not work when I place it in a master page. I have a master page that basically has a textbox and a linkbutton, when I hit the enter key, the default button for the child page gets called instead.
http://weblogs.asp.net/jeff/archive/2005/07/26/420618.aspx
set linkbutton as default button for asp:panel in asp.net (Ahmad's Solution)
any insight on this please? thanks :)
I will post the dirty solution when I get to work tomorrow.
EDIT: here's the dirty (i think) solution:
string id = ctlToClick.UniqueID;
string someJavascript = //see below
EmailTextBox.Attributes.Add("OnKeyPress", someJavascript);
The javascript (placed it here so StackOverflow can format it better):
javascript:if (window.event){
if(parseInt(window.event.keyCode) == 13){
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('" + id + "', '', true, 'Login', '', false, true));
return false;
}
}
Is this really the only solution for this?
Did you try setting the defaultButton property?
<form id="Form1"
defaultbutton="lnkNextPage"
runat="server">
Where lnkNextPage is the LinkButton control's id.
I think you need a simple javascript to control your tab/enter keys. I have run into this before an there was no way to make the default button solution work.
The idea is simple - check each key that is entered while in the text box. If the key presed is enter or tab, find the button either by jquery or document.GetElementById('id') and invoke the click event on the button.
the form's default button doesn't work in asp.net since a): there is only 1 form, and b): for different text boxes you want to be able to control which button is actually clicked (on tab/enter).
A simple javascript file solves this.
<bleepzter/>
Related
I have a web page that prompts for user input via DropDownLists in some table cells. When a selection is made the selection replaces the DropDownList so that the action can only be performed once. In the case that a change needs to be made I want to be able to click a button that reloads the page from scratch. I have Googled and Googled but I have not managed to find a way to do this.
Any advice is appreciated.
Regards.
Put a link on the page with the text "Reload" and the url the url of the page. It's perfectly valid to have a page with a link to itself.
If you don't like the link idea, use a standard Button and in the click event, use Response.Redirect to redirect to the current page.
You can set an OnClick for your button that resets each DropDownList's SelectedIndex to 0 instead of reloading the page from scratch. Alternatively, you can set a Response.Redirect([the page's url]) into the OnClick as is suggested here.
I have a RadioButtonList with 2 items. I want a postback when any of the items is selected. I have added a confirm box for one item of RadioButtonList. But, when I click OK in the confirmbox, there is no postback and SelectedIndexChanged event is not getting fired.
AutoPostback property of RadioButtonList is set to true.
This is a code fragment from my Page_Load method:
RadioButtonOpenRestricted.Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:return confirm('Are you sure?');");
Earlier, I had added confirm box for entire RadioButtonList and postback was working as expected. But I want the confirm box to be displayed only when user clicks on "Open Access" item.
Please help!
I tried a few things. The new code lines look like:
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:showConfirmBox(0,'" + RadioButtonOpenRestricted.ClientID + "')");
RadioButtonOpenRestricted.Items.FindByValue("Restricted Access").Attributes.Add("OnClick", "javascript:showConfirmBox(1,'" + RadioButtonOpenRestricted.ClientID + "')");
The javascript method is:
function showConfirmBox(i,id)
{
if(i==0)
{
if (confirm("Are you sure you want to provide Open Access? All existing individual permissions will be removed!")==true)
{
var ctrl1 = document.getElementById(id + "_0");
ctrl1.selected=true;
}
else
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
if(i==1)
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
The problem with this code is that it is treating both OK and Cancel as same. The confirm box is getting displayed but if-else part of the javascript method is not getting called. I tried using OnClientClick also...this doesnt even display the Confirmbox.
Help!!!
This is because your on click script does not play well with auto-post back script generated by the ASP.NET. A quick hack solution can be
RadioButtonOpenRestricted.AutoPostBack = true;
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "if (!confirm('Are you sure?')) return false;");
Although, this will still give you an issue when you cancel on your confirmation box (in such case, you have to add script to select the previous radio button again).
As such I am not a great fan of radio button list - you may consider alternate mechanism such as repeater with radio button in item template and having your own java-script for confirmation.
I think you want to use OnClientClick to show a "confirm" window.
I don't think you can have a javascript confirm window perform a postback, at least not the way your code is set up.
You should set the OnClientClick to show a confirm modal or window with an <asp:Button/> and have that button perform the postback your looking for.
Khushboo, it used to happen with me many times. The reason behind that was I was missing some closing tag somewhere in my aspx page. I used to copy my whole aspx page to some other text editor and paste all the elements one by one to my aspx page. It always solved my this problem. I am sure you must be missing some closing tag, please cross check all elements.
I want to develop an application in asp.net with C#.
In that application there are one text box and one label.
I want the following functionality:
When I press any key in textbox then I should get this value on .cs page i.e; code behind. And from .cs page I want to add this value to the label.
The problem is that there is no keypress event for an asp textbox and if I take a html text box then I don't get its value on .cs page
How can I come out with this problem?
Because a keypress on a textbox is a client side event but you want to perform server-side processing you will need to use AJAX requests.
You may find the following useful:
AJAX Toolit
Using Jquery to call asp.net page methods
In asp.net the TextBox will have TextChanged event but you will need to enable post back for the button and the event will fire when you tab out of the TextBox.
For the task you want either use javascript or add a button and when this button is do what you want.
I don't think this is a good aproach in web app., In this way you will end with a lot of post-backs.
However, if you still want this functionality. Texbox has TextChanges event, and if you also change the textboxs's AutoPostBack property to true you will get something close, but you will still have to move a currsor.
But it is still a terible solution. Why don't you simply use a button that fires click event instead?
Alternative solution is to use Ajax or javaScript,..
You can simply create a JavaScript-Method for this.
Your Textbox:
<asp:TextBox ID="textBox" runat="server" onkeydown="onFilterTextChanged()">
</asp:TextBox>
Your JavaScript, do a TimeOut to not do this every 0,0001 secs.
function onFilterTextChanged() {
if (timeoutID)
window.clearTimeout(timeoutID);
timeoutID = window.setTimeout(updateFilterText, 600);
}
Send the Values to the CodeBehind, textis your TextBox-Text.
function updateFilterText() {
var text = document.getElementById("<%=textBox.ClientID %>").value;
__doPostBack("<%=textBox.ClientID%>", "CommandArg" + text);
}
You won't need to do as many PostBacks as with the native TextChanged-Event and you can simply use this e.g. for Auto-Extender-Plugins. Pack the TextBox into an UpdatePanel and you're good to go!
Unless of course you do not NEED to go back to the server, in which case just set the labeltext in updateFilterText.
I've got an ASP WebForm with 2 Textboxes and 3 Buttons and I want a specific button_click event to be fired whenever I press return whilst in a TextBox - how would I achieve this?
I tried using the TabIndex-Property, unfortunately that actually only seems to work for tabbing, not for pressing return in a TextBox.
Thanks!
Hey everyone, thanks for your answers - I just found the solution, for an aspx-page within a MasterPage I need to set
Form.DefaultButton = myButton.UniqueID;
instead of
Form.DefaultButton = myButton.ID;
which throws an error.
Thanks everyone!
Set the form's AcceptButton to the button you want pressed:
form1.AcceptButton = btnOK;
You can set which button is to be used as the default directly in mark-up of controls, by settings the defaultbutton property value to the ID of the desired button control, try this:
<form id="form1" runat="server" defaultbutton="DefaultButtonId">
<!-- content -->
</form>
I'm trying to get a specific asp:button onclick event to fire when I press the enter key in a specific asp:textbox control.
The other factor to be taken into account is that the button is within a asp:Login control template.
I've no idea how to do this, suggestions on a postcard please.
You could look at the DefaultButton property of the panel control.
You could set the DefaultButton property on the form. Either as an attribute of the form tag in your markup DefaultButton = "btnSubmit" or using something like this in your code-behind:
Page.Form.DefaultButton = "btnSubmit"
Its HtmlForm.DefaultButton
You need to do it with javascript. It's really easy with jQuery.
You can do something like (off the top of my head, not tested):
$('#myTextBox').keypress(function(e){
if(e.which == 13)
$('#myBtn').click();
});
Edit: Be aware that although jQuery works exceptionally cross browser, there are some quirks with keypress described here.
Whoops i didnt see you said the "enter key" i thought you said "any key", yeah in that case use DefaultButton on asp:panel