Web user control 使用的屬性,事件和方法

來源:互聯網
上載者:User
ascx使用辦法:屬性、方法和事件

ascx是一個整體,你要是想使用它的東西,只有三種辦法:屬性、方法和事件

像如要使用它裡面的dg,可以用屬性把dg的應用調出來,下面是我寫個一個例子,你可以看看

使用者控制項
usc/dg.ascx

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="dg.ascx.cs" Inherits="startmssql.ucs.dg" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:DataGrid id="dgsy" runat="server"></asp:DataGrid>


usc/dg.ascx.cs

namespace startmssql.ucs
{using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class dg : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid dgsy;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
}
# Web Form設計器產生的程式碼
public DataGrid DGrid
{
get
{
return this.dgsy;
}
set
{
this.dgsy=value;
}
}
}
}具體應用

dgridsy.aspx

<%@ Register TagPrefix="uc1" TagName="dg" Src="ucs/dg.ascx" %>
<%@ Page language="c#" Codebehind="dgridsy.aspx.cs" AutoEventWireup="false" Inherits="startmssql.dgridsy" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>dgridsy</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋體">
<uc1:dg id="dguc" runat="server"></uc1:dg></FONT>
</form>
</body>
</HTML>dgridsy.aspx.cs
using System.Drawing;using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;
using System.Data.SqlClient;using System.Configuration;
namespace startmssql
{public class dgridsy : System.Web.UI.Page
{
SqlConnection conn=null;
SqlCommand cmd=null;
protected startmssql.ucs.dg dguc;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
setup();}
#表單設計器產生的程式碼
private void setup()
{
string strconn=ConfigurationSettings.AppSettings["wei"];
conn=new SqlConnection(strconn);
string sql="select top 8 * from [authors] order by au_id desc";
cmd=new SqlCommand(sql,conn);
try
{
conn.Open();
dguc.DGrid.DataSource=cmd.ExecuteReader();
dguc.DGrid.DataBind();
}
catch(SqlException ex)
{
Response.Write(ex.Message);
} finally
{
conn.Close();
}
}}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.