Nhiều runat=server forms trên cùng một trang (ASP.NET)

Một phần của tài liệu Meo_lap_trinh_tech24.vn (Trang 26 - 29)

ASP.NET không hỗ trợ nhiều runat=server forms trên cùng một trang. Để khắc phục việc này, bạn có thể đặt mỗi form trong mỗi Panel control riêng biệt, và cho phép người dùng dễ dàng chuyển giữa các panel bằng click một radio button.

Bạn có thể tham khảo ví dụ sau: 2FormExamplẹaspx

<%@ Page language=''c#'' Codebehind=''2FormExamplẹcs'' AutoEventWireup=''falsé' Inherits=''_3leaf_app.C2FormExamplé' %>

<html><head>

<meta name=vs_targetSchema content=''HTML 4.0''>

<meta name=''GENERATOR'' Content=''Microsoft Visual Studio 7.0''> <meta name=''CODE_LANGUAGÉ' Content=''C#''></head>

<form method=''post'' runat=''server'' ID=Form1> <p>Lookup by

<asp:RadioButton id=RadioButton1 runat=''server'' Text=''First Namé' AutoPostBack=''Trué' groupname=g1 checked=True></asp:RadioButton>

<asp:RadioButton id=RadioButton2 runat=''server'' Text=''Last Namé' AutoPostBack=''Trué' groupname=g1></asp:RadioButton></p>

<p></p> <p>

<asp:Panel id=Panel1 runat=''server'' visible=True>

First Name :

<asp:TextBox id=TextBox1 runat=''server''></asp:TextBox>

<asp:RequiredFieldValidator id=RequiredFieldValidator1 runat=''server'' ErrorMessage=''*'' ControlToValidate=''TextBox1''></asp:RequiredFieldValidator>

<asp:Button id=Button1 runat=''server'' Text=''Submit''></asp:Button>

</asp:Panel>

<asp:Panel id=Panel2 runat=''server'' visible=False>

Last Name :

<asp:TextBox id=TextBox2 runat=''server''></asp:TextBox>

<asp:RequiredFieldValidator id=RequiredFieldValidator2 runat=''server'' ErrorMessage=''*'' ControlToValidate=''TextBox2''></asp:RequiredFieldValidator>

<asp:Button id=Button2 runat=''server'' Text=''Submit''></asp:Button>

</asp:Panel>

<p></p> <p>

<asp:label id=Label1 runat=''server''></asp:label> </p> </form> </body></html> 2FormExamplẹcs namespace _3leaf_app { using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UỊWebControls; using System.Web.UỊHtmlControls; /// <summary>

/// Summary Description for C2FormExamplẹ /// </summary>

public class C2FormExample : System.Web.UỊPage {

protected System.Web.UỊWebControls.Button Button2;

protected System.Web.UỊWebControls.RequiredFieldValidator RequiredFieldValidator2; protected System.Web.UỊWebControls.TextBox TextBox2;

protected System.Web.UỊWebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UỊWebControls.TextBox TextBox1;

protected System.Web.UỊWebControls.Label Label1; protected System.Web.UỊWebControls.Panel Panel2; protected System.Web.UỊWebControls.Panel Panel1;

protected System.Web.UỊWebControls.RadioButton RadioButton2; protected System.Web.UỊWebControls.RadioButton RadioButton1;

public C2FormExample() {

PagẹInit += new System.EventHandler(Page_Init); }

protected void Page_Load(object sender, EventArgs e) {

if (!IsPostBack) {

} }

protected void Page_Init(object sender, EventArgs e) {

//

// CODEGEN: This call is required by the ASP+ Windows Form Designer. //

InitializeComponent(); }

/// <summary>

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary>

private void InitializeComponent() {

RadioButton1.CheckedChanged += new System.EventHandler (this.RadioButton1_CheckedChanged); Button1.Click += new System.EventHandler (this.Button1_Click);

RadioButton2.CheckedChanged += new System.EventHandler (this.RadioButton2_CheckedChanged); Button2.Click += new System.EventHandler (this.Button2_Click);

this.Load += new System.EventHandler (this.Page_Load); }

public void Button2_Click (object sender, System.EventArgs e) {

Label1.Text = ''You want to search on last namé'; }

public void Button1_Click (object sender, System.EventArgs e) {

Label1.Text = ''You want to search on first namé'; }

public void RadioButton2_CheckedChanged (object sender, System.EventArgs e) {

Panel2.Visible = true; }

public void RadioButton1_CheckedChanged (object sender, System.EventArgs e) { Panel1.Visible = true; Panel2.Visible = false; } } }

Một phần của tài liệu Meo_lap_trinh_tech24.vn (Trang 26 - 29)

Tải bản đầy đủ (PDF)

(79 trang)