XML, DataSet, DataGrid in combination with advertising management program (II.)

Source: Internet
Author: User

The source code is as follows:

<% @ Page language= "C #" responseencoding= "gb2312"%>
<% @ Import namespace= "System.Data"%>
<% @ Import namespace= "System.Xml"%>
<% @ Import namespace= "System.IO"%>
<script language= "C #" runat= "Server" >
DataSet ds; Define a common dataset
DataView DV; Define a public DataView
String SortField;
String spath;
public void Page_Load (Object Src,eventargs E)
{
if (state["Adxml"]==null)
{
spath = Server.MapPath (".") + "\\AdBanners\\ad_gb.xml";
ds = new DataSet ();
Ds. READXML (spath);
state["adxml"] = ds;
}
Else
{
ds = (DataSet) state["Adxml"];
}
DV = ds. Tables[0]. DefaultView;
Dv. Sort = "IMAGEURL";

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

Bundle Binder
public void CreateTable ()
{

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

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

Delete
public void Delitem (Object Sender,datagridcommandeventargs e)
{
if ((LinkButton) e.commandsource). CommandName = "Del")
{
First get 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 ();

Delete
Dv. Delete (row);

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

CreateTable ();
}
}

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

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

Update
public void Dgxml_update (Object sender, DataGridCommandEventArgs e)
{
Try
{
First get 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 ();

Get the values

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-nextpagetext= "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"/>
。 Mixture Sp: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" &GT;&LT;TD colspan=2> Add a new ad page </td></tr>
<tr>
<td> Advertising Picturesurl:</td>
<td><asp:textbox id= "Murl" runat= "Server" text= "http://"/></td>
</tr>
<tr>
<td> Advertising 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> Advertising 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= "submitted" runat= "Server"/></td></tr>
</table>
</asp:Panel>
<asp:label id= "lb" runat= "Server"/>
</form>
</body>



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.