July 23, 2011

ASP.NET MultiViewCode Example

When you want multiple forms on a single .aspx page and at a time user should be able to see just one , you can use MultiView Control which divide single page in multiple views below is the code sample for that

ExMultiView.aspx

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


<!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 id="Head1" runat="server">
</HEAD>


<BODY>
    <FORM id="form1" runat="server">
        <DIV>
           <table border="0" cellpadding="2" cellspacing="3" width="100%">
            <tr>
                <td>
                   <asp:LinkButton ID="lnkTab1" runat="server" OnClick="lnkTab1_Click">Reporting of Order On Sender</asp:LinkButton></td>
                <td>
                  <asp:LinkButton ID="lnkTab2" runat="server" OnClick="lnkTab2_Click">Date Wise Reporting of Order</asp:LinkButton></td>
               
            </tr>
            <tr>
                <td colspan="3">
                    <asp:MultiView ID="MultiView1" runat="server">
                         <table width="100%" cellpadding="2" cellspacing="5">
                         <tr>
                            <td>
                            <asp:View ID="View1" runat="server">
                             <table >
                             <tr>
                                 <td> Sender First Name </td>
                                 <td><asp:TextBox ID="txtSenderFName" runat="server"></asp:TextBox></td>
                                
                              </tr>
                              <tr> 
                                    <td>Sender Last Name </td>
                                    <td><asp:TextBox ID="txtSenderLastName" runat="server" ValidationGroup ="Report1"></asp:TextBox></td>
                                    <td><asp:RequiredFieldValidator ID="rfvSenderLastName" runat="server" ErrorMessage="* Required" ControlToValidate ="txtSenderLastName" SetFocusOnError ="true" ValidationGroup ="Report1" ForeColor="red" ></asp:RequiredFieldValidator ></td>
                              
                              </tr>
                              
                                     <tr> 
                                    <td style="height: 26px"> <asp:Button ValidationGroup ="Report1" ID="brnSearch" runat="server" Text="Search"  /></td>
                                    
                               </tr>
                                                           
                              </table>
                               <asp:GridView ID="GridViewReport1" runat="server">
                                </asp:GridView> 
                              </asp:View>
                            </td>
                            <td>
                                <asp:View ID="View2" runat="server">
                         
                           Second Form Comes here
                         
                             </asp:View>
                            </td>
                          
                        </tr>
                        </table>
                   </asp:MultiView>
               </td>
            </tr>
        </table>
    </FORM>
</BODY>
</HTML>

ExMultiView.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ExMultiView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }


   
    protected void lnkTab1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void lnkTab2_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }
    protected void lnkTab3_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 2;
    }
}

below 'll be the 0/p in which first view is enabled 

If Click on the Second tab , second view will be displayed 


No comments:

Post a Comment