Making advertisement management program with asp.net and XML

Source: Internet
Author: User
Tags add object net string sort tostring xmlns
asp.net|xml| Program | advertising

This article describes the use of XML in ASP.net, where flying knives I just show the use of XML in conjunction with a dataset. In fact, asp.net on the operation of XML and the object is a lot of, such as Xmldatadocument,xmltextreader, these objects are fully functional, but also very practical.

This article takes the XML used by the AdRotator control as an example (the novice can look at the usage of the AdRotator control) and write an ad management program. This program, the control used by flying cutter is mainly a DataGrid, you can say that the DataGrid has been used almost all the functions, we are concerned about the XML at the same time, you can learn how to use the DataGrid.

Let us explain some of the methods used in this example:

Datasets are mainly used in ReadXml and WriteXml methods. Their function to see the name to know, flying knives are not wordy.

A lot of methods and objects are used in the DataGrid. For example pagination uses the AllowPaging property, the sort uses the AllowSorting attribute, in this case all events in the DataGrid, such as Onitemcommand,oneditcommand,oncancelcommand, are used. Onupdatecommand and so on.

Look carefully at the handling of these events.

In addition, when you look at the program, think again, if this program uses ASP to write how many pages, oh, with ASP.net only need one page, and unlimited use, this is the advantages of asp.net.

In the program I made some simple comments, the most important thing is everyone's understanding. Oh, recently our studio people are busy a step confused, so please forgive us. Flying knives These days is not not write procedures, just write procedures need to be published in the book, (for example, and the pursuit of the same function of the Web page, really can achieve the same function as the pursuit, no false) can not be posted online. This will be scolded:)

Let's look at the contents of the XML file we're working with:

 
<?xml version= "1.0" standalone= "yes"?
<Advertisements>
<xsd:schema id= "Advertisements" targetnamespace= "xmlns=" "
Xmlns:xsd= "Http://www.w3.org/1999/XMLSchema"
Xmlns:msdata= "Urn:schemas-microsoft-com:xml-msdata" >
<xsd:element name= "Ad" >
<xsd:complextype content= "Elementonly"
<xsd:all>
<xsd:element name= "ImageUrl" minoccurs= "0" type= "xsd:string"/>
<xsd:element name= "NavigateUrl" minoccurs= "0" type= "xsd:string"/>
<xsd:element name= "AlternateText" minoccurs= "0" type= "xsd:string"/>
<xsd:element name= "Keyword" minoccurs= "0" type= "xsd:string"/>
<xsd:element name= "Impressions" minoccurs= "0" type= "xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:element name= "Advertisements" msdata:isdataset= "True"
<xsd:complexType>
<xsd:choice maxoccurs= "unbounded"
<xsd:element ref= "Ad"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<Ad>
<ImageUrl> ASPCN_BBS1.GIFC </ImageUrl>
<NavigateUrl> <ImageUrl> aspcn_bbs3.gif </ImageUrl>
<NavigateUrl> <ImageUrl> aspcn_bbs2.gifc </ImageUrl>
<NavigateUrl> Aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> I am a Chinese/alternatetext>
<Keyword> Chinese/keyword>
<Impressions>% </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> http://www.aspcn.com </NavigateUrl>
<AlternateText> aaaddd </AlternateText>
<Keyword> Aaadd </Keyword>
<Impressions> 98090 </Impressions>
</Ad>
<Ad>
<ImageUrl> Http://www.aspcn.com/aspcn.gif </ImageUrl>
<NavigateUrl> </NavigateUrl>
<AlternateText> Aaasdfasfad </AlternateText>
<Keyword> Aaadd </Keyword>
<Impressions> 98090 </Impressions>
</Ad>
<Ad>
<ImageUrl> http://wwwaspcn.com </ImageUrl>
<NavigateUrl> http://</NavigateUrl>
<AlternateText> </AlternateText>
<Keyword> </Keyword>
<Impressions> </Impressions>
</Ad>
</Advertisements>


There are some problems in Chinese, but in the following program can be normal display, because of the internationalization of technology, haha, this example with a lot of stuff yo:

This is the standard format XML referenced by the AdRotator control.

Let's take a look at ASP.net's source code:

Because the XML file occupies a lot of space, the source program has to be posted on this page. (is also very long yo, we have the patience to see the line)

 
<% @ Page language= "C #" responseencoding= "gb2312"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Xml"%>
<% @ Import namespace= "System.IO"%>

if (! Page.IsPostBack)
{
CreateTable ();
}
}

file://Bundle Binder
public void CreateTable ()
{

Dgxml.datasource = DV;
Dgxml.databind ();
}

When file://the page
public void dgxml_changed (Object Sender,datagridpagechangedeventargs e)
{
CreateTable ();
}

file://Delete
public void Delitem (Object Sender,datagridcommandeventargs e)
{
if ((LinkButton) e.commandsource). CommandName = "Del")
{
file://first gets the number of rows and CurrentPageIndex for the current update page
int CPI = (int) Dgxml.currentpageindex;
int EII = (int) E.item.itemindex;
int row = CPI*5+EII;

Lb. Text = row. ToString ();

file://Delete
Dv. Delete (row);

Dgxml.edititemindex =-1;
Ds. WriteXml (spath);

CreateTable ();
}
}

file://canceled.
public void Dgxml_cancel (Object Sender,datagridcommandeventargs e)
{
Dgxml.edititemindex =-1;
CreateTable ();
}

file://Editor
public void Dgxml_edit (Object Sender,datagridcommandeventargs e)
{
Dgxml.edititemindex = (int) E.item.itemindex;
CreateTable ();
}

file://Update
public void Dgxml_update (Object sender, DataGridCommandEventArgs e)
{
Try
{
file://first gets the number of rows and CurrentPageIndex for the current update page
int CPI = (int) Dgxml.currentpageindex;
int EII = (int) E.item.itemindex;
int row = CPI*5+EII;
File://lb.Text = row. ToString ();

file://get each value

String ImageUrl = ((TextBox) e.item.cells[2]. Controls[0]). Text;
String NavigateUrl = ((TextBox) e.item.cells[3]. Controls[0]). Text;
String AlternateText = ((TextBox) e.item.cells[4]. Controls[0]). Text;
String Keyword = ((TextBox) e.item.cells[5]. Controls[0]). Text;
String impressions = ((TextBox) e.item.cells[6]. Controls[0]). Text;

Dv. Delete (row);


DataRow dr = ds. Tables[0]. NewRow ();
Dr[0] = IMAGEURL;
DR[1] = NavigateUrl;
DR[2] = AlternateText;
DR[3] = Keyword;
DR[4] = impressions;
Ds. Tables[0]. Rows.Add (DR);
Ds. WriteXml (spath);
}
catch (Exception ee)
{
Lb. Text = ee. ToString ();
}

Dgxml.edititemindex =-1;
CreateTable ();


}

public void Panelshow (Object Sender,eventargs e)
{
Additem.visible = true;
}

public void Additem_click (Object Sender,eventargs e)
{
DataRow dr = ds. Tables[0]. NewRow ();
Dr[0] = Murl.text;
DR[1] = Aurl.text;
DR[2] = Mtext.text;
DR[3] = Akey.text;
DR[4] = Atime.text;
Ds. Tables[0]. Rows.Add (DR);
Ds. WriteXml (spath);
CreateTable ();
Additem.visible=false;
}
</script>
<title> </title>
<body>
<form id= "Form1" runat= "Server"
<asp:datagrid id= "Dgxml" runat= "Server"
Allowpaging= "True"
Pagesize= "10"
Bordercolor= "BLACK"
Borderwidth= "1"
Cellpadding= "3"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Pagerstyle-mode= "NumericPages"
Pagerstyle-horizontalalign= "Right"
pagerstyle-prevpagetext= "front page"
pagerstyle-next page: ASP.net 2.0 Login Control Introduction text= "Back Page"
Onpageindexchanged= "Dgxml_changed"
Oneditcommand= "Dgxml_edit"
Oncancelcommand= "Dgxml_cancel"
Onupdatecommand= "Dgxml_update"
Onitemcommand= "Delitem"
Autogeneratecolumns= "false"
>
<property name= "Columns"
<asp:buttoncolumn headertext= "Delete" text= "delete" Commandname= "Del"
<asp:editcommandcolumn edittext= "Modify" canceltext= "Cancel" updatetext= "Update" itemstyle-wrap= "false"
headertext= "Operation Area" headerstyle-wrap= "false"/"
<asp:boundcolumn headertext= "picture address (relative)" sortfield= "ImageUrl" datafield= "IMAGEURL"/>
<asp:boundcolumn headertext= "link url" sortfield= "NavigateUrl" datafield= "NavigateUrl"/>
<asp:boundcolumn headertext= "Picture description" sortfield= "AlternateText" datafield= "AlternateText"/>
<asp:boundcolumn headertext= "category" Sortfield= "Keyword" datafield= "Keyword"/>
<asp:boundcolumn headertext= "Show Time" sortfield= "Impressions" datafield= "impressions"/>
</property>
</asp:DataGrid>
<asp:button id= "VI" text= "Add New Item" runat= "Server"/>
<br>
<asp:panel id= "AddItem" runat= "Server" visible= "false"
<table>
<TR bgcolor= "#aaaadd" ><td colspan=2> Add a new ad page </td> </tr>
<tr>
<td> advertisement picture URL: </td>
<td> <asp:textbox id= "Murl" runat= "Server" text= "http://"/> </td>
</tr>
<tr>
<td> AD link Address: </td>
<td> <asp:textbox id= "Aurl" runat= "Server" text= "http://"/> </td>
</tr>
<tr>
<td> Picture Description: </td>
<td> <asp:textbox id= "Mtext" runat= "Server"/> </td>
</tr>
<tr>
<td> ad-owning Category: </td>
<td> <asp:textbox id= "Akey" runat= "Server"/> </td>
</tr>
<tr>
<td> Show Time </td>
<td> <asp:textbox id= "aTime" runat= "Server"/> </td>
</tr>
<tr> <td>
<asp:button id= "Submit" text= "commit" runat= "server"/> </td> </tr>
</table>
</asp:Panel>
<asp:label id= "lb" runat= "Server"/>
</form>
</body>

Let's have a good analysis.



Related Article

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.