Solved

ASP.NET

Posted on 2007-11-26
5
199 Views
Last Modified: 2013-11-26
I have a usercontrol (login.ascx) that gets loaded through the masterpage on load.

There's a submit button on it that I need to make the default button when someone doesn't click on it but just hits the <ENTER> key.  I know I can do that on an ASPX page...but how in a user control?

Thanks
0
Comment
Question by:lrbrister
[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
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 20350881
This worked for me.
Page_Load is on the master page.
My test user control has a button named "Button1", which I'm setting as the default button on the master page:
<%@ Master Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ctrl As Control = LoadControl("ucDefaultButton.ascx")
        Dim btn As Button = CType(ctrl.FindControl("Button1"), Button)
        Me.ContentPlaceHolder1.Controls.Add(ctrl)
        Me.form1.DefaultButton = btn.ID
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server" defaultbutton="Button1">
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Comment

by:lrbrister
ID: 20351215
samtram0331,
  Trying to figure out why this doesn't work...


 Select Case CStr(Request.Params("page"))
            Case "home"
                Session("info") = " Home Page"
                Dim uc As UserControl = CType(LoadControl("includes/login.ascx"), UserControl)
                Dim btn As Button = CType(uc.FindControl("ImageButton1"), Button)
                ContentPlaceHolder1.Controls.Add(uc)
                Me.form1.DefaultButton = btn.ID
                uc.ID = "uc1"

Open in new window

0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20351331
it looks like you're trying to convert an image button to a button....try changing the line:

Dim btn As Button = CType(uc.FindControl("ImageButton1"), Button)
to
Dim btn As ImageButton = CType(uc.FindControl("ImageButton1"), ImageButton)

0
 

Author Comment

by:lrbrister
ID: 20351497
samtram...It forced me to use the IButton code..below.  But when I load that I'm getting the error message in the attached snippet
Session("info") = "Home Page"
                Dim uc As UserControl = CType(LoadControl("includes/login.ascx"), UserControl)
                Dim btn As IButtonControl = CType(uc.FindControl("ImageButton1"), IButtonControl)
                ContentPlaceHolder1.Controls.Add(uc)
                Me.form1.DefaultButton = btn
                uc.ID = "uc99"
Unable to cast object of type 'System.Web.UI.WebControls.ImageButton' to type 'System.String'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ImageButton' to type 'System.String'.
 
Source Error: 
 
 
Line 176:                Dim btn As IButtonControl = CType(uc.FindControl("ImageButton1"), IButtonControl)
Line 177:                ContentPlaceHolder1.Controls.Add(uc)
Line 178:                Me.form1.DefaultButton = btn
Line 179:                uc.ID = "uc99"
Line 180:        End Select
 

Open in new window

0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 20352351
I don't have the time at the moment to figure out why it won't let you set the defaultbutton in codebehind, but I found that if you wrap the user control in a panel and set the panel's default button, it works...below is a simple uc I'm testing with:
<%@ Control ClassName="ucDefaultButton" Language="VB" %>
 
<script runat="server">
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Label1.Text = "Image button clicked! " & Now.ToString & "<br />Textbox: " & TextBox1.Text
    End Sub
</script>
 
<asp:Panel ID="Panel1" runat="server" DefaultButton="ImageButton1">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <br />
    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/disk_green.png" OnClick="ImageButton1_Click" />
    </asp:Panel>

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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