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.
shanemayAsked:
Who is Participating?
 
eguilhermeCommented:
just add a panel inside each updatepanel and set the defaultbutton to the id of the button inside the updatepanel that you want to be the default.. eg:



        <asp:UpdatePanel ID="updMain" runat="server">
            <ContentTemplate>
                <asp:Panel ID="pnlMain" runat="server" DefaultButton="Button1">
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
0
 
XGISCommented:
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?
0
 
shanemayAuthor Commented:
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.  
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
XGISCommented:
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

0
 
XGISCommented:
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

0
 
shanemayAuthor Commented:
Thank you, that worked perfectly.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.