首页 > 开发 > XML > 正文

XML、DataSet、DataGrid结合写成广告管理程序(二)

2024-09-05 20:55:58
字体:
来源:转载
供稿:网友

源代码如下:

<% @ page language="c#" responseencoding="gb2312" %>
<% @ import namespace="system.data" %>
<% @ import namespace="system.xml" %>
<% @ import namespace="system.io" %>
<script language="c#" runat="server">
dataset ds;        //定义公用的dataset
dataview dv;  //定义公用的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();
    }
}

//捆绑binder
public void createtable()
{

    dgxml.datasource = dv;
    dgxml.databind();
}

//翻页时
public void dgxml_changed(object sender,datagridpagechangedeventargs e)
{
    createtable();
}

//删除
public void delitem(object sender,datagridcommandeventargs e)
{
    if(((linkbutton)e.commandsource).commandname == "del")
    {
        //首先取得当前更新页的行数与currentpageindex
        int cpi = (int)dgxml.currentpageindex;
        int eii = (int)e.item.itemindex;
        int row = cpi*5+eii;

        lb.text = row.tostring();

        //删除
        dv.delete(row);

        dgxml.edititemindex = -1;
        ds.writexml(spath);

        createtable();
    }
}

//取消
public void dgxml_cancel(object sender,datagridcommandeventargs e)
{
    dgxml.edititemindex = -1;
    createtable();
}

//编辑
public void dgxml_edit(object sender,datagridcommandeventargs e)
{
    dgxml.edititemindex = (int)e.item.itemindex;
    createtable();
}

//更新
public void dgxml_update(object sender, datagridcommandeventargs e)
{
    try
    {
        //首先取得当前更新页的行数与currentpageindex
        int cpi = (int)dgxml.currentpageindex;
        int eii = (int)e.item.itemindex;
        int row = cpi*5+eii;
        //lb.text = row.tostring();

        //取得各项值

        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>
<html>
<head>
<title></title>
</head>
<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="前页"
pagerstyle-nextpagetext="后页"
onpageindexchanged="dgxml_changed"
oneditcommand="dgxml_edit"
oncancelcommand="dgxml_cancel"
onupdatecommand="dgxml_update"
onitemcommand="delitem"
autogeneratecolumns="false"
>
<property name="columns">
    <asp:buttoncolumn headertext="删除" text="删除" commandname="del" />
   。糰sp:editcommandcolumn edittext="修改" canceltext="取消" updatetext="更新" itemstyle-wrap="false" headertext="操作区" headerstyle-wrap="false" />
    <asp:boundcolumn headertext="图片地址(相对)" sortfield="imageurl" datafield="imageurl" />
    <asp:boundcolumn headertext="链接url" sortfield="navigateurl" datafield="navigateurl" />
    <asp:boundcolumn headertext="图片说明" sortfield="alternatetext" datafield="alternatetext" />
    <asp:boundcolumn headertext="类别" sortfield="keyword" datafield="keyword" />
    <asp:boundcolumn headertext="显示时间" sortfield="impressions" datafield="impressions" />
</property>
</asp:datagrid>
<hr>
<asp:button id="vi" text="添加新项" onclick="panelshow" runat="server"/>
<br>
<asp:panel id="additem" runat="server" visible="false">
<table>
    <tr bgcolor="#aaaadd"><td colspan=2>添加新的广告页面</td></tr>
    <tr>
        <td>广告图片url:</td>
        <td><asp:textbox id="murl" runat="server" text="http://" /></td>
    </tr>
    <tr>
        <td>广告链接地址:</td>
        <td><asp:textbox id="aurl" runat="server" text="http://" /></td>
    </tr>
    <tr>
        <td>图片说明:</td>
        <td><asp:textbox id="mtext" runat="server" /></td>
    </tr>
    <tr>
        <td>广告所属类别:</td>
        <td><asp:textbox id="akey" runat="server" /></td>
    </tr>
    <tr>
        <td>显示时间</td>
        <td><asp:textbox id="atime" runat="server" /></td>
    </tr>
    <tr><td>
   <asp:button id="submit" text="提交" onclick="additem_click" runat="server" /></td></tr>
</table>
</asp:panel>
<asp:label id="lb" runat="server" />
</form>
</body>
</html> 

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表