Solved

Aspx - defaultbutton does not work

Posted on 2014-12-27
5
72 Views
Last Modified: 2015-01-02
Hi

I have a page that has a logout button at the top of my page.
<asp:ImageButton ID="btnLogOut" runat="server" ImageUrl="/ASPImages/staff_logout.gif"
                                    OnClick="btnLogOut_Click" UseSubmitBehavior="false" TabIndex="50" />

Open in new window


My page is populated programmatically as a table with drop down boxes, textboxes and save button (final result example at bottom of this question).

 TableCell tc53 = new TableCell();
                            tc53.HorizontalAlign = HorizontalAlign.Left;
                            tc53.ColumnSpan = 3;
                            tc53.Controls.Add(new LiteralControl("Comment<br />"));
                            TextBox tb = new TextBox();
                            tb.ID = "txt" + auditdetailid;
                            tb.MaxLength = 255;
                            tb.Width = 330;
                            tb.Text = comment;
                            tc53.Controls.Add(tb);
                            tc53.Width = 440;
                            tr5.Cells.Add(tc53);

Open in new window


I have a form and straight after a panel

<form id="form1" runat="server" defaultbutton="btnDoNothing">

Open in new window

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnDoNothing">

Open in new window


Yet whenever I press the 'Enter' key the logout button gets fired and not btnDoNothing.

How do I get around this? Why is this happening? I need a fix as this is driving me crazy.

Below is the end result of programmatically creating the table. of course this is only 1 question of many.  
<tr>
									<td colspan="8">
									<hr noshade="noshade" size="1" /></td>
								</tr>
								<tr class="bold">
									<td style="width: 55px;">&nbsp;</td>
									<td style="width: 110px;">Question 1</td>
									<td colspan="4" style="width: 440px;">&nbsp;</td>
									<td align="center" style="width: 110px;">
									<input id="btn1" name="btn1" type="submit" value="Save" /></td>
									<td style="width: 35px;">&nbsp;</td>
								</tr>
								<tr>
									<td colspan="8" style="width: 750px;">&nbsp;</td>
								</tr>
								<tr class="bold">
									<td style="width: 55px;">&nbsp;</td>
									<td style="width: 110px;">Q.1</td>
									<td colspan="5" style="width: 550px;">This is my first Audit question?</td>
									<td style="width: 35px;">&nbsp;</td>
								</tr>
								<tr>
									<td colspan="2" style="width: 165px;">&nbsp;</td>
									<td align="left" style="width: 110px;">Reponse<br />
									<select id="dd70599" name="dd70599">
									<option selected="selected" value="">
									</option>
									<option value="Yes">Yes</option>
									<option value="No">No</option>
									<option value="Can't Audit">Can&#39;t Audit
									</option>
									</select></td>
									<td align="left" colspan="3" style="width: 440px;">
									Comment<br />
									<input id="txt70599" maxlength="255" name="txt70599" style="width: 330px;" type="text" /></td>
									<td style="width: 35px;">&nbsp;</td>
								</tr>
								<tr>
									<td colspan="8" style="width: 750px;">&nbsp;</td>
								</tr>

Open in new window

0
Comment
Question by:elmbrook
  • 2
  • 2
5 Comments
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
Comment Utility
Where is the btnDoNothing defined in your mark-up? Please post its  mark up, hopefully it should look like:
<asp:Panel id="Panel1" runat="server" DefaultButton="btnDoNothing">
    <!-- All controls here including: -->
    <asp:Button id="btnDoNothing" runat="server" Text="" />
</asp:Panel>

Open in new window

]If your only challenge is the "disable enter" scenario, then you could solve it by calling the following JavaScript to your onkeydown handler on <form> tag of your page.
function DisableEnterKey()
{
var evt = window.event ;
if (evt.keyCode == 13) 
{
    if (evt.preventDefault) evt.preventDefault();
    evt.returnValue=false; 
    evt.cancel = true;
}
}

Open in new window

0
 
LVL 12

Expert Comment

by:Ammar Gaffar
Comment Utility
You can set the default button for enter key at form level, in your Page Load event place this code:
  
                this.form1.DefaultButton = "btnDoNothing";
           
            

Open in new window


The above code assumes that you form name is form1, you can change it if not.
Note: this code shouldn't place inside !PostBack check
Good Luck
0
 
LVL 12

Assisted Solution

by:Ammar Gaffar
Ammar Gaffar earned 250 total points
Comment Utility
Minor update to my above mentioned comment, you can set the same property defaultbutton in your form tag (html) when you are populating page pragmatically
0
 

Accepted Solution

by:
elmbrook earned 0 total points
Comment Utility
@miguel

It does not matter where I put btnDoNothing (top, middle, bottom of panel) it still does not get clicked.

I also decided to not go with the javascript function.

In the end I changed all the buttons on the form to UseSubmitBehavior="false" and that seemed to have stopped it.

@ammar - I forgot to mention that one. I already set the default button to do nothing in the Page Load. This did not work.
0
 

Author Closing Comment

by:elmbrook
Comment Utility
Setting all buttons UseSubmitBehavior="false" (including the ones generated programmatically) as well as removing the logout button (which was not really necessary) seemed to have worked.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now