Link to home
Start Free TrialLog in
Avatar of shanemay
shanemayFlag for United States of America

asked on

Default button on an Update Panel

I have a page within a web application that contains four update panels.  I would like to set the default button of each update panel, but I am not sure how.

Any guidance would be greatly appreciated.
Avatar of XGIS
XGIS
Flag of Australia image

Hey There,

I would just like to clarify, you have multiple <asp:Panels on your page each panel has a number of buttons you want to be able to set a default button in each panel when the panel is active or are the also text boxes in each panel that will require the default button???

i.e. will the end user click in the panel and press enter; or will the user type some thing in a textbox in each panel the press enter?
Avatar of shanemay

ASKER

No, not panels, Update Panels : <asp:UpdatePanel>,
Each panel has a form fields and a submit button, I would like to set the default button for each update panel to the appropriate submit button.  This would allow the user to enter information and hit the enter key on the keyboard.  

I can do this with asp:Panels using the defualtbutton attribute (<asp:Panel runat="server" DefaultButton="SubmitCommand">), however, it does not seem that update panels have this option.  
ASKER CERTIFIED SOLUTION
Avatar of Edgard Yamashita
Edgard Yamashita
Flag of Brazil 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
try this

           <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
             
            <asp:Button ID="Button2" runat="server" Text="Not Default" />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Default" />
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{

    protected void Button1_Click1(object sender, EventArgs e)
    {
       Label3.Text = "Default button pressed.  Time is " + DateTime.Now.ToString();
    }
}

Open in new window

or try this

<!--aspx-->

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script language="javascript" type="text/javascript">
    function WebForm_FireDefaultButton(event, target) {
        if (event.keyCode == 13) {
            var src = event.srcElement || event.target;
            if (!src || (src.tagName.toLowerCase() != "textarea")) {
                var defaultButton;
                if (__nonMSDOMBrowser) {
                    defaultButton = document.getElementById(target);
                }
                else {
                    defaultButton = document.all[target];
                }
                if (defaultButton && typeof (defaultButton.click) != "undefined") {
                    defaultButton.click();
                    event.cancelBubble = true;
                    if (event.stopPropagation) event.stopPropagation();
                    return false;
                }
            }
        }
        return true;
    }
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Panel ID="Panel1" runat="server" DefaultButton="Button2">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Width="400px"></asp:TextBox>
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Default" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
        </asp:Panel>
        <asp:Panel ID="Panel2" runat="server" DefaultButton="Button3">
            <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                <ContentTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Width="400px"></asp:TextBox>
                    <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Default" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
        </asp:Panel>
        <asp:Panel ID="Panel3" runat="server" DefaultButton="Button1">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Width="400px"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Default" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
        </asp:Panel>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox3.Text = "Default button pressed.  Time is " + DateTime.Now.ToString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "Default button pressed.  Time is " + DateTime.Now.ToString();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox2.Text = "Default button pressed.  Time is " + DateTime.Now.ToString();
    }

}

Open in new window

Thank you, that worked perfectly.