Solved

Aspx - defaultbutton does not work

Posted on 2014-12-27
5
75 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 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