Solved

Aspx - defaultbutton does not work

Posted on 2014-12-27
5
73 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
ID: 40520049
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
ID: 40520265
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
ID: 40520267
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
ID: 40520775
@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
ID: 40527491
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Auto refresh part of an MVC5 View 7 59
Two different visual studio versions 3 23
Error on Add method 1 38
C# Single Form 8 28
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

15 Experts available now in Live!

Get 1:1 Help Now