Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting ImageButton as DefaultButton ASP.NET 2.0

Posted on 2007-03-21
10
Medium Priority
?
1,340 Views
Last Modified: 2008-02-26
Hello.  I hope there is a more definitive answer than what I've been reading.  I have an imagebutton on an ASP.NET form that I want to set as the default button (when the user presses ENTER).  Does anybody have a solution for this?
0
Comment
Question by:adwooley2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 11

Expert Comment

by:lijunguo
ID: 18768593
0
 

Author Comment

by:adwooley2
ID: 18768844
That didn't work for me.  What I did was I added Implements iButtonControl on the image button and voila!
Thanks anyway.
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18769366
<form id="form1" runat="server" defaultbutton="ImageButton1">

Can this work for you?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:adwooley2
ID: 18769468
No, can't use that, since I am using a master page in my environment.  Thanks anyway.
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18769489
In that case, you can do that in code behind

this.Form.DefaultButton = "ImageButton1";
0
 

Author Comment

by:adwooley2
ID: 18775945
Have you ever tested that? Image buttons cannot be set as the default button alone.  They don't support IButtonControl.  That is why I  added Implements iButtonControl on the image button and then set it as the DefaultButton.  In other words, I've already solved this issue.
0
 
LVL 16

Accepted Solution

by:
Edwin_C earned 2000 total points
ID: 18777497
Glad to know you have solve the problem!
Does DefaultButton need to be iButtonControl?  It worked for me!

    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:ImageButton ID="ImageButton1"
            runat="server" ImageUrl="~/button-next.gif" OnClick="ImageButton1_Click" />
    </div>
    </form>

Code behind:
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Form.DefaultButton = "ImageButton1";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Button clicked");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("ImageButton clicked");
    }

When I pressed ENTER, ImageButton1 was clicked.
0
 

Author Comment

by:adwooley2
ID: 18777575
Really!?  It didn't work for me, and from everything that I was reading on the web, you're not supposed to be able to do what you did.  What version of Visual Studio are you using?
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 18778934
I am using VS2005 and ASP.NET 2.0.

After consulting the reference material, you are correct in the way that DefaultButton must implement IButtonControl but ImageButton does implement IButtonControl.

0
 

Expert Comment

by:gmain
ID: 24675866
To further clarify things, as of ASP.NET 2.0 you can set the DefaultButton property on the form object or on a panel object.  So, for the example given in this thread, if you are in a masterpage/child page situation with the form being on the masterpage, you could create a panel object on your child page and set the default button on that panel.  That way you do not need to modify the masterpage's form object in any way.
If you have multiple default buttons set (due to multiple panels), when the page postback is processed, the default will be determined by the context of the currently selected control.
for further details see:  http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

671 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