ASP.NET Enter key triggering unwanted button - asp.net

I have this on the top of a page (in MasterPage.master)
<asp:Panel ID="panSearch" runat="server">
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:ImageButton ID="btnSearch" runat="server" ImageUrl="~/images/iconSearch.gif" onclick="btnSearch_Click" />
</asp:Panel>
The button keeps getting fired when I press the enter key on another TextBox down the page (in an aspx page)
<input type="text" id="txtTagName">
<input type="button" value="Tag" id="btnAddTagOk">
I couldn't find any JavaScript that does that.
Anyone has any idea why it's happening?

Hitting enter will submit the default form. If you have several controls which you want control over the enter button you need to hook in javascript on the control to intercept the enter key press, and then do your own logic.
Something like this
<input type="text" id="txtTagName" onkeydown="if (event.keyCode == 13) document.getElementById('btnAddTagOk').click()"/>

you might have some other button with its type as type="submit". The browser by default submits the form when enter is pressed on textboxes.
you might want to try following on your asp:panel
<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch">
</asp:Panel>

Related

Call hidden ASP.NET FileUpload control from a Button control

I have a webpage containing one ASP.NET file upload control and a button to upload the file to server. The existing code looks like below.
<div runat="server" style="width: 110%">
<asp:FileUpload ID="fileUpload" runat="server" />
<asp:Button ID="BtnFileUpload" runat="server" OnClick="BtnFileUpload_Click" Text="Upload" />
</div>
But our client don't want to see the default look and feel of a standard file upload control. He wants us to add another button and wrap the file upload control with the button, so that whenever user clicks on the button, file upload dialog window opens.
Thanks
you cna do that using jquery you can set fileupload visiblity as none and can open fileuploader from button click like
<div runat="server" style="width: 110%">
<asp:FileUpload style="display:none" ID="fileUpload" runat="server" />
<asp:Button ID="BtnFileUpload" runat="server" onclick="$('#fileUpload').trigger('click'); return false;" OnClick="BtnFileUpload_Click" Text="Upload" />
</div>
you need to reference of jquery for this.
In addition to #Kevin Shah's solution, I got it working this way:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="BtnFileUpload" runat="server" OnClientClick="$('#ContentPlaceHolder1_FileUpload1').trigger('click'); return false;" Text="Upload" />
My Webform is inside a MasterPage, so I had to look at "View source" while debugging in Chrome to get the correct element ID ContentPlaceHolder1_FileUpload1 instead of just FileUpload1

Why is DefaultButton not working?

I have the following code:
<form id="frmLogOn" runat="server" DefaultButton="testbutton">
<asp:Panel runat="server" ID="testpanel" DefaultButton="testbutton">
<asp:TextBox runat="server"></asp:TextBox>
<asp:Button runat="server" ID="testbutton" OnClick="dotest" UseSubmitBehavior="True"/>
</asp:Panel>
</form>
If I click the button, the page does a postback and calls dotest(), as expected.
If I press the enter key while focused on the textbox nothing happens.
I have investigated, and found that WebForm_FireDefaultButton(event, target) is being called when I type a different key (for example a letter or a number) but it is NOT being called when I press the enter key.
(Note I found it is called twice, and this is probably because I set the same DefaultButton for both the form and the panel).
Why is it not being called?
After some searching I determined this was caused by the following in my master page:
<body onkeydown = "return (event.keyCode!=13)" >
Works fine without it.

ASP.NET pressing Enter key causes log out

I have created Website. After I login When I hit enter key for add Product, my Website just kick me out.? I dont have problem with adding my cart with mouse click. Does Any One have same Issue or Any suggestion ..
You need to add your controls inside an ASP:Panel and make your AddProduct Button as Default Button:
<asp:Panel ID="panel1" runat="server" DefaultButton="btnAddProduct"">
//Your Other Stuff
<asp:Button ID="btnAddProduct" runat="server" onclick="btnAddProduct_Click"/>
</asp:Panel>
This will Fire AddProduct Button when you hit enter.
Regards
Here is another approach I found here - if you don't want to add a panel
<asp:Button ID="btnDisableEnter" runat="server" Text=""
OnClientClick="return false;" style="display: none;" />
<form id="form1" runat="server" DefaultButton="btnDisableEnter">
If you are using Master Page in asp.net C# and if you are using a button for logout, please type in Button:
<asp:Button ID="btn" runat="server" Text="logout"
UseSubmitBehavior="false" />
Use UseSubmitBehavior="false" in your master page. That's how my problem was solved.

input button causes RequiredFieldValidator to fire

I have an input button on my page that is dynamically created, every time I press it, it fires the RequiredFieldValidator for the blank field in the email address.
is there a way to ovverride it?
input code:
<input class=\"hledat\" id=\"searchbutton\" type=\"image\" src=\"search-button.gif\" value=\"{0}\" onclick=\"search('{1}');\" onkeypress=\"search('{1}');\"/>"
validator code:
<asp:TextBox runat="server" ID="txtEmail" ClientIDMode="Static" Width="98%" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ErrorMessage="email"
Display="dynamic" ValidationGroup="newsletter" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtEmail" ErrorMessage="email"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="dynamic"
ValidationGroup="newsletter" />
I have an input button on my page that is dynamically created, every
time I press it, it fires the RequiredFieldValidator for the blank
field in the email address. is there a way to ovverride it?
HtmlButton.CausesValidation Property
Mark UP
<button causesvalidation="false" /><button ID="StateQueryButton"
CausesValidation="False"
runat="server">
Submit
</button>
yes, i want it to do a postback
HtmlButton.OnServerClick Method
Code Behind
protected void FancyBtn_Click(object sender, EventArgs e)
{
}
Mark Up
<button causesvalidation="false" /><button ID="StateQueryButton"
CausesValidation="False"
OnServerClick=" FancyBtn_Click"
runat="server" >
Submit
</button>
Make it server side button by writing runat="Server" attribute and make a CausesValidation="true|false" on your requirements. If you do false it will not validate. Do let me know if it solves the problem
try to add runat="server" to input button as shown:
*Note* the runat="server". While asp:button probably renders similarly, if what you really want it an HTML button input, you can use that. Yes, ASP.NET will pick up the value on the server side.**
<input type="button" runat="server" id="btnTest" value="change" onclick="doPostBack('btnTest', '');"/>

how to set a default 'enter' on a certain button

There is a textbox on a ContentPage. When the user presses Enter in that textbox I am trying to fire a 'Submit' button on this ContentPage. I'd like to fire off that particular button's event.
Instead, there is a search textbox & button on the top of the page from a MasterPage, and this search button's event fires off.
How do I control to fire off this ContentPage's submit button, instead of the MasterPage's search button?
I am using Ektron CMS for my content management.
The easiest way is to put the fields and button inside of a Panel and set the default button to the button you want to be activated on enter.
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
if you need to do it from code, use
Me.Form.DefaultButton = Me.btn.UniqueID
Where btn is your button control.
You can use the DefaultButton property on either a server-side form control or Panel control. In your case, group the controls together in a Panel that should fire off the same button:
<asp:Panel ID="SearchBox" runat="server" DefaultButton="BtnSearch">
...
<asp:Button ID="BtnSearch" runat="server" Text="Search!" />
</asp:Panel>
....
<asp:Panel ID="UserPanel" runat="server" DefaultButton="BtnUserSubmit">
...
<asp:Button ID="BtnUserSubmit" runat="server" Text="Submit" />
</asp:Panel>
You can now use UseSubmitBehavior property to disable all the buttons you don't want to fire when hitting submit (check out the documentation for more info)
<asp:Button ID="BtnNotToFIre" runat="server" Text="Search" UseSubmitBehavior="false" />
Microsoft say:
<form id="Form1"
defaultbutton="SubmitButton"
defaultfocus="TextBox1"
runat="server">
enter link description here
$(document).ready(function(){
document.getElementById("text_box_id")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("button_id").click();
}
});
});

Resources