C#-web User Controls

Source: Internet
Author: User
Tags tagname

To pass data from a user control to a page:
Method One: Use session pass.
1. When the button is clicked, place the value in the session.
2. Rewrite the page's Onloadcomplete method to remove the value from the session in this method.
Note: Do not remove the session from the Page_Load. The reason: Every time the button is clicked, Page_Load is always triggered before the click of the button.


Method Two: Use the proxy (delegate delegate) to pass the value to the page
What is an agent? --The proxy is a pointer to a method.
Proxies are very similar to classes, but they are very different.
Classes and objects:
First step: Define a new class using the class keyword
public class Ren
{
public void Speak ()
{
....
}
}
Step Two: Use this class to define variables.
Private Ren R;

Step three: Instantiate the Ren object and assign the object to the variables in the stack
R = new Ren ();

The fourth step: the operation of the object in the heap is achieved by invoking the variables in the stack.
R.xxxx

Agent delegate:
First step: Define a new proxy type using the delegate keyword.
The return type proxy name of the public delegate agent (parameter list);
Public delete void Showdelegate (string s);

Step Two: Use the proxy type above to define the proxy variables.
Private Showdelegate Show;

Step three: Specify a function to assign the function to the proxy variable show
void Haha (String aaa)
{
Console.WriteLine (AAA);
}
Show = new Showdelegate (Haha);

Fourth step: Use the function that the proxy call points to
Show ("Hello");

Two user controls, displayed on the page, proxy instances:

User Control 1:

<%@ Control language= "C #" autoeventwireup= "true" codefile= "Seach1.ascx.cs" inherits= "Seach1"%>
<asp:label id= "Label1" runat= "server" text= "Please enter:" ></asp:Label>
<asp:textbox id= "TXT" runat= "server" ></asp:TextBox>
<asp:button id= "Cha" runat= "Server" onclick= "Cha_click" text= "Query"/>

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

public partial class Seach1:System.Web.UI.UserControl
{
Private Carsdatacontext context = new Carsdatacontext ();
public delegate void Showshuju (list<car> List);
Public Showshuju Showdele;

protected void Page_Load (object sender, EventArgs e)
{

}
protected void Cha_click (object sender, EventArgs e)
{
var qu = context. Car.where (p = p.name.contains (txt. Text));
if (Showdele! = null)
{
Showdele (Qu. ToList ());
}
}
}

User Control 2:

<%@ Control language= "C #" autoeventwireup= "true" codefile= "Seach2.ascx.cs" inherits= "Seach2"%>
<asp: GridView id= "GridView1" runat= "Server" cellpadding= "4" forecolor= "#333333" gridlines= "None" width= "100%";
< AlternatingRowStyle backcolor= "White"/>
<editrowstyle backcolor= "#2461BF"/>
<footerstyle Backcolor= "#507CD1" font-bold= "true" forecolor= "white"/>
<pagerstyle backcolor= "#2461BF" forecolor= "white" horizontalalign= "Center"/>
<rowstyle backcolor= "#EFF3FB"/>
<selectedrowstyle backcolor= "#D1DDF1" font-bold= "True" forecolor= "# 333333 "/>
<sortedascendingcellstyle backcolor=" #F5F7FB "/>
<sortedascendingheaderstyle Backcolor= "#6D95E1"/>
<sorteddescendingcellstyle backcolor= "#E9EBEF"/>
< Sorteddescendingheaderstyle backcolor= "#4870BE"/>
</asp:gridview>

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

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

}
public void Bindcar (list<car> List)
{
Gridview1.datasource = list;
Gridview1.databind ();
}
}

Show Page:

<%@ page language= "C #" autoeventwireup= "true" codefile= "Seach-ye.aspx.cs" inherits= "Seach_ye"%>

<%@ Register src= "Seach1.ascx" tagname= "Seach1" tagprefix= "uc1"%>
<%@ Register src= "Seach2.ascx" tagname= "Seach2" tagprefix= "UC2"%>

<! DOCTYPE html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>

<uc1:seach1 id= "Seach11" runat= "Server"/>
<br/>
<br/>
<br/>
<UC2:SEACH2 id= "Seach21" runat= "Server"/>

</div>
</form>
</body>

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

public partial class Seach_ye:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Seach11.showdele = new Seach1.showshuju (Seach21.bindcar);
}
}

A user control, displayed on the page, session instance:

Control:

<%@ Control language= "C #" autoeventwireup= "true" codefile= "Kongjian.ascx.cs" inherits= "Kongjian"%>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "Control (inside)"/>
<br/>
<asp:label id= "Label1" runat= "Server" text= "Label Inside" ></asp:Label>

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

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

}
protected void Button1_Click (object sender, EventArgs e)
{
session["AA"] = TextBox1.Text;
}
}

Show Page:

<%@ page language= "C #" autoeventwireup= "true" codefile= "pages. Aspx.cs" inherits= "page"%>

<%@ Register src= "Kongjian.ascx" tagname= "Kongjian" tagprefix= "uc1"%>

<! DOCTYPE html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "Server" text= "Outside"/>
<br/>
<asp:label id= "Label1" runat= "Server" text= "Label out" ></asp:Label>
<div>

<br/>
<uc1:kongjian id= "Kongjian1" runat= "Server"/>

</div>
</form>
</body>

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

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

}
protected override void Onloadcomplete (EventArgs e)
{
Base. Onloadcomplete (e);
if (session["AA"]! = NULL)
{
TextBox1.Text = session["AA"]. ToString ();
}
}
}

A user control that displays the proxy instance on the page:

Control:

<%@ Control language= "C #" autoeventwireup= "true" codefile= "Kongjian2.ascx.cs" inherits= "kongjian2"%>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "proxy"/>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>

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

public partial class Kongjian2:System.Web.UI.UserControl
{
public delegate void Showdele (String str);
Public Showdele Textdele;
protected void Page_Load (object sender, EventArgs e)
{

}
protected void Button1_Click (object sender, EventArgs e)
{
if (Textdele! = null)
{
Textdele (TextBox1.Text);
}
}
}

Show Page:

<%@ page language= "C #" autoeventwireup= "true" codefile= "page 2.aspx.cs" inherits= "Page 2"%>

<%@ Register src= "Kongjian2.ascx" tagname= "kongjian2" tagprefix= "uc1"%>

<! DOCTYPE html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>

<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "being proxied"/>
<br/>
<br/>
<br/>
<br/>
<asp:label id= "Label1" runat= "Server" text= "Label" ></asp:Label>
<br/>
<uc1:kongjian2 id= "kongjian21" runat= "Server"/>

</div>
</form>
</body>

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

Public partial class page 2:system.web.ui.page
{
Private delegate void Textdele (String str);
Private Textdele Deletext;
protected void Page_Load (object sender, EventArgs e)
{
Kongjian21.textdele = new kongjian2. Showdele (setshow);
}
protected void Button1_Click (object sender, EventArgs e)
{
Deletext = new Textdele (Show);
Deletext ("This agent has done!") ");
}
public void Show (string s)
{
Label1.Text = s;
}
public void Setshow (string s)
{
Label1.Text = s;
}
}

C#-web User Controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.