Multiple Default Buttons

Some time we need multiple default buttons in a single form. Like if there are two portion in the from like Login and search or feedback form in single form. Then if you are typing in login part then press and press enter then the Login button is clicked and if in the feedback portion then the button for feedback should be clicked. This can be done by keeping all these portion in a separate panel. Add DefaultButton attribute to panel and give the Id of the button that is inside the Panel. For example Login Button inside Login Panel and Send Buuton in side Feedback Panel.

Below is the code. Its not hard but very simple.

Code for .aspx Page


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


<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Panel ID="pnl1" runat="server" DefaultButton="Button1" style="width:60%; height:200px; border:dotted 1px #000000; padding:10px;">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button One" OnClick="Button1_Click" />
       </asp:Panel>
        
    
    <asp:Panel ID="pnl2" runat="server" DefaultButton="Button2" style="width:60%; height:200px; border:dotted 1px #000000; padding:10px; margin-top:50px;">
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button Two" OnClick="Button2_Click" />
       </asp:Panel>
        <br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    
    
    </form>
</body>
</html>

Code for .cs Page

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 Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "<span style='color:#ff0000; letter-spacing:1px; font-family:Arial; font-size:16px;'>Button One Is Cicked</span>";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label1.Text = "<span style='color:#ff0000; letter-spacing:1px; font-family:Arial; font-size:16px;'>Button Two Is Cicked</span>";
    }
}


Copy the above code and run...

Enjoy... :)

Happy Coding...

Comments

Post a Comment

Popular posts from this blog

Get Query String Values With Javascript

Change Css Class of li in ul onclick

Change Text Color with Javascript