?
Solved

Aspx - defaultbutton does not work

Posted on 2014-12-27
5
Medium Priority
?
83 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 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 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