Solved

Aspx - defaultbutton does not work

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
InputLanguage 1 25
ASP.NET(C#) Eliminating weekends from a date range 2 33
asp enable view state 1 24
ASP.NET MVC -Add authentication 2 21
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

775 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