ASP.NET基础教程-Web 自定义控件的使用-根据属性值从数据库中提取数据并在页面上自动生成一个表格...
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET基础教程-Web 自定义控件的使用-根据属性值从数据库中提取数据并在页面上自动生成一个表格...
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、新建一個(gè)Web 控件庫;
二、在WebCustomControl1.cs文件中編制如下代碼:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel; namespace WebControlLibrary1
{
??? /// <summary>
?? /// WebCustomControl1 的摘要說明。
?? /// </summary>
?? [DefaultProperty("Text"),
????? ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
??? public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
??? {
??????? private string text;//存儲用戶輸入的文本內(nèi)容
??????? public string tablename;//存儲用戶輸入的屬性值
?????? [Bindable(true),Category("Appearance"),?DefaultValue("")] Public string Tablename //添加用戶自定義文本輸出次數(shù)的屬性
?????? {
???????? get {
?????????????? return tablename;
???????????? }
???????? set
??????????? {
??????????????? tablename=value;
??????????? }
??????? }
????? public string Text? //添加用戶自定義文本屬性
????? {
???????? get
???????? {
??????? ?return text;?
???????? }
???????? set
???????? {
???????? ?text = value;??
???????? }
????? }
/// <summary>
?? /// 將此控件呈現(xiàn)給指定的輸出參數(shù)。
?? /// </summary>
?? /// <param name="output"> 要寫出到的 HTML 編寫器 </param>
?? //protected訪問僅限于包含類或從包含類派生的類型。
???? 使用 override 修飾符來修改方法、屬性、索引器或事件。
???? HtmlTextWriter類在 Web 窗體頁上寫出一系列連續(xù)的 HTML 特
???? 定字符和文本。
??? 此類提供 ASP.NET 服務(wù)器控件在將 HTML 內(nèi)容呈現(xiàn)給
???? 客戶端時(shí)所使用的格式化功能。 protected override void Render(HtmlTextWriter output)
?? {
????? SqlConnection con=new SqlConnection(@System.Configuration.ConfigurationSettings.AppSettings["server"]);
????? con.Open();
????? string oSql="select count(id) from sysobjects where name='"+tablename.ToString()+"'";
????? SqlCommand comm=new SqlCommand(oSql,con);
????? int jl=(Int32)comm.ExecuteScalar();
????? con.Close();
????? if(jl>0)?? {
?????? con.Open();
?????? oSql="select * from "+tablename.ToString();
?????? SqlDataAdapter da=new SqlDataAdapter(oSql,con);
?????? DataSet ds=new DataSet();
?????? da.Fill(ds,"Query");
?????? con.Close();
?????? string Text_Value="";
?????? for(int i=0;i<ds.Tables["Query"].Rows.Count;i++){
??????????? Text_Value=Text_Value+"<TR>";
??????????? for(int j=0;j<ds.Tables["Query"].Columns.Count;j++){
?Text_Value=Text_Value+"<TD>"+ds.Tables["Query"].Rows[i][j].ToString()+"</TD>";}
??????????? Text_Value=Text_Value+"</TR>";}
?????? output.Write("<TABLE id='Table1' style='Z-INDEX: 115; LEFT: 0px; POSITION: absolute; TOP: 0px'
???????? cellSpacing='1' cellPadding='1' width='300' border='1'>"+Text_Value.ToString()+"</TABLE>");
?????? }
??????? else{output.Write("對不起,找不到數(shù)據(jù)源,請確認(rèn)你輸入到“Tablename”屬性的表名在數(shù)據(jù)庫中存在");}
}
三、將控件編譯生成WebControlLibrary1.dll;
四、新建一個(gè)ASP.NET應(yīng)用程序;
五、將生成的WebControlLibrary1.dll文件添加到工具欄中;
六、在工具欄中將添加的用戶自定義控件添加到頁面;
七、設(shè)置控件的Tablename屬性為“成績表”。 八、運(yùn)行結(jié)果
?
二、在WebCustomControl1.cs文件中編制如下代碼:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel; namespace WebControlLibrary1
{
??? /// <summary>
?? /// WebCustomControl1 的摘要說明。
?? /// </summary>
?? [DefaultProperty("Text"),
????? ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
??? public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
??? {
??????? private string text;//存儲用戶輸入的文本內(nèi)容
??????? public string tablename;//存儲用戶輸入的屬性值
?????? [Bindable(true),Category("Appearance"),?DefaultValue("")] Public string Tablename //添加用戶自定義文本輸出次數(shù)的屬性
?????? {
???????? get {
?????????????? return tablename;
???????????? }
???????? set
??????????? {
??????????????? tablename=value;
??????????? }
??????? }
????? public string Text? //添加用戶自定義文本屬性
????? {
???????? get
???????? {
??????? ?return text;?
???????? }
???????? set
???????? {
???????? ?text = value;??
???????? }
????? }
/// <summary>
?? /// 將此控件呈現(xiàn)給指定的輸出參數(shù)。
?? /// </summary>
?? /// <param name="output"> 要寫出到的 HTML 編寫器 </param>
?? //protected訪問僅限于包含類或從包含類派生的類型。
???? 使用 override 修飾符來修改方法、屬性、索引器或事件。
???? HtmlTextWriter類在 Web 窗體頁上寫出一系列連續(xù)的 HTML 特
???? 定字符和文本。
??? 此類提供 ASP.NET 服務(wù)器控件在將 HTML 內(nèi)容呈現(xiàn)給
???? 客戶端時(shí)所使用的格式化功能。 protected override void Render(HtmlTextWriter output)
?? {
????? SqlConnection con=new SqlConnection(@System.Configuration.ConfigurationSettings.AppSettings["server"]);
????? con.Open();
????? string oSql="select count(id) from sysobjects where name='"+tablename.ToString()+"'";
????? SqlCommand comm=new SqlCommand(oSql,con);
????? int jl=(Int32)comm.ExecuteScalar();
????? con.Close();
????? if(jl>0)?? {
?????? con.Open();
?????? oSql="select * from "+tablename.ToString();
?????? SqlDataAdapter da=new SqlDataAdapter(oSql,con);
?????? DataSet ds=new DataSet();
?????? da.Fill(ds,"Query");
?????? con.Close();
?????? string Text_Value="";
?????? for(int i=0;i<ds.Tables["Query"].Rows.Count;i++){
??????????? Text_Value=Text_Value+"<TR>";
??????????? for(int j=0;j<ds.Tables["Query"].Columns.Count;j++){
?Text_Value=Text_Value+"<TD>"+ds.Tables["Query"].Rows[i][j].ToString()+"</TD>";}
??????????? Text_Value=Text_Value+"</TR>";}
?????? output.Write("<TABLE id='Table1' style='Z-INDEX: 115; LEFT: 0px; POSITION: absolute; TOP: 0px'
???????? cellSpacing='1' cellPadding='1' width='300' border='1'>"+Text_Value.ToString()+"</TABLE>");
?????? }
??????? else{output.Write("對不起,找不到數(shù)據(jù)源,請確認(rèn)你輸入到“Tablename”屬性的表名在數(shù)據(jù)庫中存在");}
}
三、將控件編譯生成WebControlLibrary1.dll;
四、新建一個(gè)ASP.NET應(yīng)用程序;
五、將生成的WebControlLibrary1.dll文件添加到工具欄中;
六、在工具欄中將添加的用戶自定義控件添加到頁面;
七、設(shè)置控件的Tablename屬性為“成績表”。 八、運(yùn)行結(jié)果
?
轉(zhuǎn)載于:https://blog.51cto.com/chenxing/58197
總結(jié)
以上是生活随笔為你收集整理的ASP.NET基础教程-Web 自定义控件的使用-根据属性值从数据库中提取数据并在页面上自动生成一个表格...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CCIE学习(34)—— EIGRP配置
- 下一篇: position:relative 与