Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

ASP.NET

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
Avatar of samtran0331
samtran0331
Flag of United States of America image

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

Avatar of Larry Brister

ASKER

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

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)

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

ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial