完全定制的可以重用ASP+垂直/水平字幕消费 xml/ms SQL

分享于 

10分钟阅读

Web开发

  繁體

介绍

在每个项目中,我都需要一种方法来显示最新的文章。新闻。weathercast。等等的一些项目过程链接。 我理想的解决方案将是一个快速,简单,轻量级的网页自定义控件。

如果你不熟悉自定义网页控件,下面是来自Microsoft文档的部分定义:

Web控件在服务器上运行,包括表单控件,如按钮和文本框以及特殊的用途控件。 这允许你以编程方式控制网页上的这些元素。 Web控件比HTML控件更抽象。 他们的对象模型不一定反映了HTML语法。 ( 请参见 MSDN。)

功能

  • 伟大的设计时间属性。
  • 允许你选择数据库: XML或者 SQL Server。
  • 你可以应用你的网页模板。
  • 每个主题都有标识。说明。日期。锚 URL。锚文本。

数据库

在XML作为数据库的情况下:

<?xmlversion="1.0"encoding="utf-8"?><marquee><topictpID='1'postDate='1/1/2002'><urlvalue='http://www.....com'> Link... </url><hint><![CDATA[ Link Description goes here ]]></hint></topic></marquee>

使用代码

我们的控件有三个类:

  • 字幕:WebControl ( 呈现控件)。
  • Data ( 从数据库读取数据( XML或者 SQL Server ) 并生成主题集合)。
  • Anchor ( 数据持有者)。

首先,我们将预览 Data 类代码:

publicclass Data
{
 public ArrayList GetNews(string str,bool _isXML)
 { 
 //setup Array List ArrayList AnchorsArrayList=new ArrayList();
 if(!_isXML)// Read From MSSQL DB ::::::::::::: {
 try {
 //Select SQl Query : text,url,id,date,title, Table Name linksstring sqlSelectStr="SELECT text, url, id, date, title FROM links";
 SqlConnection myConnection;
 //connect DB And check without overload on the DB Server. myConnection=new SqlConnection(str);
 if(myConnection.State==ConnectionState.Closed){myConnection.Open();}
 //pass our SQL Str within new Command SqlCommand myCommand=new SqlCommand(sqlSelectStr,myConnection);
 //Creating A Reader to store Returned Database Data SqlDataReader myReader=myCommand.ExecuteReader();
 //looping thrugh Reader.while(myReader.Read())
 {
 //intialize Anchor Object Anchor anc=new Anchor();
 //set Object id anc.Id=Convert.ToInt32(myReader["id"]);
 //set Object url anc.Url=myReader["url"].ToString();
 //set Object text anc.Text=myReader["text"].ToString();
 //set Object date anc.Date=(DateTime)myReader["date"];
 //set Object Hints anc.Title=myReader["title"].ToString();
 //add it to our Collection AnchorsArrayList.Add(anc);
 }
 //Dispose any relation with DB myReader.Close();
 myConnection.Close();
 }
 catch(Exception ex)
 {
 throw ex;
 }
 }
 else// Read From XML DB :::::::::::::::::::::::::::::::::: {
 try {
 // create XMLDocument and Load Xml File XmlDocument xmlDoc=new XmlDocument();
 xmlDoc.Load(str);
 //create XPath Navigator in order to navigate XML Nodes... XPathNavigator nav=xmlDoc.CreateNavigator();
 // create Two NodeItrators in order to loop throw Topics XPathNodeIterator navIterator;//topic XPathNodeIterator navIterator2;//details//Set XPath to Topic Node... navIterator=nav.Select("marquee/topic");
 //topic numberint topicNo=1;
 while(navIterator.MoveNext())
 {
 //intialize Anchor Object Anchor anc=new Anchor();
 //read current topic ID and Date...I didnt //use id or Date in this article //but you can use it in sorting //search or Quary Strings between pages anc.Id=
 Convert.ToInt32(navIterator.Current.GetAttribute("tpID",
 "").ToString());
 anc.Date=
 Convert.ToDateTime(navIterator.Current.GetAttribute("postDate",
 "").ToString());
 //set Anchor/Link details XPath  navIterator2=nav.Select("marquee/topic["+topicNo+"]/url");
 navIterator2.MoveNext();
 //read URL and Text o the current topic anc.Url=
 navIterator2.Current.GetAttribute("value",
 "").ToString();
 anc.Text=navIterator2.Current.Value.ToString();
 //set description XPath  navIterator2=nav.Select("marquee/topic["+topicNo+"]/hint");
 navIterator2.MoveNext();
 //read anc.Title=navIterator2.Current.Value.ToString();
 //finally... Save Our Link AnchorsArrayList.Add(anc);
 //moveNext topicNo++;
 }
 }
 catch(Exception ex)
 {
 throw ex;
 }
 }
 //you are welcome......return AnchorsArrayList;
 }
}

现在我们来看看 Marquee 类。 有两种方法:

  • Render(...)
  • AnchorsListHtmlGenerator(.....)
///<summary>/// Generate Links Html Tags according to user setting.///</summary>///<paramname="aList">Type:ArrayList :collection/// of Anchor Objects</param>///<returns>HTML as string</returns>protectedstring AnchorsListHtmlGenerator(ArrayList aList)
{
 StringBuilder s = new StringBuilder();
 string dir;
 bool pFlag;
 //Set Langauge Direction Left-Right or Right-Leftif (arabicSupport){dir = "rtl";}else{dir = "ltr";}
 //Set Marquee direction up-Down-Let-Right using enumswitch (marqueeDirection)
 {
 case Direction.up:
 case Direction.down:
 {
 pFlag = true;
 break;
 }
 default:
 {
 pFlag = false;
 break;
 }
 }
 //looping through Arraylist collection of Links.for (int i = 0; i < aList.Count; i++)
 {
 //Check If User need to Show Hints Or not.if (showTitle)
 {
 //Is Boldif (TitleFontBold) { s.Append("<b>"); }
 // <p> tagif (pFlag) { s.Append("<p dir="" + dir + "">"); }
 //Font and Size s.Append("<font size="" + titleFontSize +
 "" color="" +
 urlTitleForeColor.Name.ToString() +
 """ + ">");
 //retrive Titles by looping through Passed ArrayList s.Append((((Anchor)aList[i]).Title).ToString());
 //</font> s.Append("</font>");
 //</p>if (pFlag) { s.Append("</p>"); }
 //</b>if (TitleFontBold) { s.Append("</b>"); }
 }
 //<p> tag and set Directionif (pFlag) { s.Append("<p dir="" + dir + "">"); }
 //set Image Sourceif (showImage)
 {
 s.Append("<img src=" + listItemImageSrc + ">");
 }
 ////<a> Tag s.Append("<A href="");
 //retrive and set Url from Passed ArrayList s.Append((((Anchor)aList[i]).Url).ToString());
 s.Append(""");
 //URL UnderLine Check and close </a>if (urlUnderLine)
 {
 s.Append(" style="text-decoration: none"");
 }
 s.Append(">");
 //Set text Font&Color s.Append("<font size="");
 s.Append(linkFontSize + "" color="" +
 urlForeColor.Name.ToString() + """ + ">");
 s.Append((((Anchor)aList[i]).Text).ToString());
 s.Append("</font></A>");
 //////</p>if (pFlag) { s.Append("</p><p> </p>"); }
 }
 //Return Full HTML Code As Stringreturn s.ToString();
}

Render 方法:

///<summary>/// Render this control to web solution///</summary>///<paramname="output"> The HTML writer to write out to </param>protectedoverridevoid Render(HtmlTextWriter output)
{
 try {
 // make instance from Data Class Data myData = new Data();
 //Write Marquee Tag with Custome Attributes output.Write("<marquee onmouseover=this.stop()" +
 "onmouseout=this.start() scrollamount=1" +
 "scrolldelay=10 direction=" +
 marqueeDirection.ToString() + " width=" +
 width + " height=" + height + ">");
 //check Bold Optionif (FontBold) { output.Write("<b>"); }
 //write the Links Tag which returned//by GetNew(connectionStr) Method output.Write(AnchorsListHtmlGenerator(myData.GetNews(connectionStr,
 isXML)));
 //Bold Close Tagif (FontBold) { output.Write("</b>"); }
 //Marquee Close Tage output.Write("</marquee>");
 }
 catch (Exception ex)
 {
 output.Write(ex.Message);
 }
}

怎么了。

在你的web解决方案中注册DLL文件后,必须:

  • 确定数据库类型:XML或者 MS SQL Server。 如果你选择的是 XML,那么设置属性 IsXML=true

  • 然后指定你的XML文件完整路径 connectionstring=":resourcesxmlxmlfile1.xml" 或者你的MS SQL DB连接字符串,在 IsXml=false


相关文章