Why is DefaultButton not working? - asp.net

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.

Related

Stop Postback in Image Button after validation failed

I have a aspx page containing a text box and an image button for search. I have used compare validator (to check for integer values) with the textbox. But the page reloads on the image button click even if I enter alphanumeric characters, along with showing the error message.
I tried using a regularexpressionvalidator instead but the problem persists.
But when i used a simple asp:button instead and binded it with textbox validation, its working fine (i.e. postback does not occur on incorrect value of textbox) and same is true with dropdownlist also (no postback occuring).
Please suggest.
Here's the code-
#peroija : Here's the code
<asp:ImageButton ID="btnSearch" runat="server" OnClick="btnSearch_Click"
ToolTip="Search" ValidationGroup="valControl" CausesValidation="true" />
<asp:TextBox ID="txtWidth" CssClass="TextFont" runat="server"
Width="233px" MaxLength="20"
ValidationGroup="valControl" CausesValidation="true"></asp:TextBox>
<asp:CompareValidator runat="server" ID="cmpValWidth"
ErrorMessage="Please enter integer values" ControlToValidate="txtWidth" Display="Dynamic"
Operator="DataTypeCheck" ValidationGroup="valControl"Type="Integer"/>
Sounds to me like you need to write
if(!isPostBack)
{
"your code"
}
in the code behind. To prevent the code from being run if the page is not viewed for the first time
Remove this from your textbox, you only need it on the validator and the button:
ValidationGroup="valControl" CausesValidation="true"
If javascript is disabled, then there will be no client side validation, so always check the validity on the server side also:
if(Page.IsValid)
{
"your btnSearch_Click code"
}

Server Control Auto Postback with form has _blank attribute

My case is I have an asp.net page has a form
<form id="form1" runat="server" target="_blank">
and a button redirect to another page and this page will open in a new window because of the target attribute of the form .
<asp:Button ID="button1" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />
and I have a dropdownlist has auto postback = true to post the past to fill another dropdownlist by selected data .
<asp:dropdownliast id="Make" name="Make" runat="server" autopostback="true"></asp:dropdownlist>
the question is : why when I select item from the auto postbacked dropdown an blank page opened ?
I need a way to post the page by the dropdownlist without openning a blank page ..
Thank you,
For lack of a better idea, you could just remove the target="_blank" attribute from your markup, and when your button is clicked, modify the form tag with JavaScript and set the attribute.
You can set the OnClientClick property and run JavaScript when it's clicked. For example:
<asp:Button ID="button1" OnClientClick="document.getElementById('form1').setAttribute('target', '_blank')" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />
You could always just adjust your buttonpress code to open a new window such as this:
<asp:Button ID="myBtn" runat="server" Text="Click me"
onclick="myBtn_Click" OnClientClick="window.open('kindofpage.aspx', 'kindofpage');" />
then remove the:
target="_blank"
From the form tag.
I struggled with a similar situation but solved it in the following way.
As mentioned in this answer, you can use the OnClientClick property to set the target to "_blank". E.g.
<asp:Button ID="button1" OnClick="codebehind_method" OnClientClick="document.forms[0].target = '_blank';" runat="server" Text="targets new window" />
Then, in the aspx page that my "codebehind_method" function redirects to, I reset the target of the opener form like so:
<script type="text/javascript">
function resetTarget() {
opener.document.forms[0].target = '';
}
</script>
<body onload="resetTarget()">
Now, if you go back to your opener form and use a control that does not have the "OnClientClick" property set, the AutoPostBack should occur in the same tab.
If you want to find your form by ID, replace "document.forms[0]" with:
document.getElementByID('yourFormName')
<form id="form1" runat="server">

Setting DefaultButton to button.UniqueID throws exception

The problem I'm trying to solve:
I have several text boxes in an asp:Panel. When the user hits Enter from any of those boxes, I want the form to submit as if they've clicked btnAddTag. (When the cursor is not in those boxes, I have a different default submit button.)
The aspx:
<asp:Panel id="thePanel" runat="server">
<asp:Button ID="btnAddTag" Text="Add Tag" runat="server" />
</asp:Panel>
The vb:
tagPanel.DefaultButton = btnAddTag.UniqueID
The exception:
The DefaultButton of 'tagPanel' must
be the ID of a control of type
IButtonControl.
The value of btnAddTag.UniqueID is ctl00$phMain$btnAddTag (there's a master page, this section is called phMain).
I've also tried CType(tagPanel.FindControl("btnAddTag"), Button).UniqueID.
do:
tagPanel.DefaultButton = btnAddTag.ID
more info here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx
You should set the control's ID not UniqueID:
tagPanel.DefaultButton = btnAddTag.ID

ASP.NET Enter key triggering unwanted button

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>

ASP.NET WebForms - Keeping contextual focus on Button controls

Consider the following:
<form runat="server">
<div>
<asp:TextBox runat="server" ID="tb1" />
<asp:Button runat="server" ID="b1" OnClick="b1_Click" />
</div>
<div>
<asp:TextBox runat="server" ID="tb2" />
<asp:Button runat="server" ID="b2" OnClick="b2_Click" />
</div>
<div>
<asp:TextBox runat="server" ID="tb3" />
<asp:Button runat="server" ID="b3" OnClick="b3_Click" />
</div>
</form>
Each TextBox has an associated Button. I want to be able to switch the focus on each of these Button controls, so that when I place my cursor in the 2nd textbox (tb2) and press Enter, the associated button (b2) gets clicked and the associated OnClick event gets fired.
I've got a few ideas myself, but I'd like you guys' feedback/lessons-learned before I start potentially wasting time on implementing a broken solution.
NOTE:
Using the HTML fieldset element is not an option--Some of the interfaces are very complex.
There can be multiple inputs associated with one button.
You could trap the keydown event on the Textbox and then fire the button's callback javascript if it's the enter key. You can get the callback reference using ClientScriptManager.GetPostBackEventReference
Alternatively you could wrap every textbox in it's own Panel, which exposes a DefaultButton property.
Well you could do a nice simple route using jQuery if you are using it.
Simply doing the following might work nicely:
<script language="javascript" type="text/javascript">
jQuery(function(){
jQuery('input').keydown(function(e){
if (e.keyCode == 13) {
jQuery(this).next().trigger('click');
return false;
}
});
});
</script>
And then code side you would have the relevant event handler triggered, or just simply see which button was clicked by querying the sender object id

Resources