但是删除的问题始终没有解决,这次做留言本 http://thcjp.cnblogs.com/archive/2006/06/26/435962.html
我实在想不到好办法来维护广告控件使用的那个XML文件,所以想到使用对DataSet操作以达到操作XML文件的目的,具体如下!!
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
fill();
}
protectedvoidfill()
{
DataSetds=newDataSet();
ds.ReadXml(Server.MapPath("..\\App_Data\\ad.xml"));
DataList1.DataSource=ds.Tables[0].DefaultView;
DataList1.DataBind();
Session["dst"]=ds;
}
protectedvoidDataList1_CancelCommand(objectsource,DataListCommandEventArgse)
{//取消
DataList1.EditItemIndex=-1;
fill();
}
protectedvoidDataList1_DeleteCommand(objectsource,DataListCommandEventArgse)
{//删除事件
DataSetds=(DataSet)Session["dst"];
ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[e.Item.ItemIndex]);//删除了,我晕,太简单了撒!
Session["dst"]=ds;
ds.WriteXml(Server.MapPath("..\\App_Data\\ad.xml"));
fill();
}
protectedvoidDataList1_EditCommand(objectsource,DataListCommandEventArgse)
{
DataList1.EditItemIndex=e.Item.ItemIndex;
fill();
}
protectedvoidDataList1_UpdateCommand(objectsource,DataListCommandEventArgse)
{//修改广告,因为我对XML类的操作确实还不熟练,所以就使用DataSet,反正结果是一样
DataSetds=(DataSet)Session["dst"];
ds.Tables[0].Rows[e.Item.ItemIndex]["ImageUrl"]=((TextBox)e.Item.FindControl("TextBox2")).Text.ToString();
ds.Tables[0].Rows[e.Item.ItemIndex]["NavigateUrl"]=((TextBox)e.Item.FindControl("TextBox5")).Text.ToString();
ds.Tables[0].Rows[e.Item.ItemIndex]["AlternateText"]=((TextBox)e.Item.FindControl("TextBox1")).Text.ToString();
ds.Tables[0].Rows[e.Item.ItemIndex]["Keyword"]=((TextBox)e.Item.FindControl("TextBox3")).Text.ToString();
ds.Tables[0].Rows[e.Item.ItemIndex]["Impressions"]=((TextBox)e.Item.FindControl("TextBox4")).Text.ToString();
Session["dst"]=ds;
ds.WriteXml(Server.MapPath("..\\App_Data\\ad.xml"));
DataList1.EditItemIndex=-1;
fill();
}
protectedvoidButton5_Click(objectsender,EventArgse)
{//新增广告
DataSetds=(DataSet)Session["dst"];
DataRowdr=ds.Tables[0].NewRow();
dr["ImageUrl"]=url.Text.ToString();
dr["NavigateUrl"]=ng.Text.ToString();
dr["AlternateText"]=na.Text.ToString();
dr["Keyword"]=kw.Text.ToString();
dr["Impressions"]=qz.Text.ToString();
ds.Tables[0].Rows.Add(dr);
Session["dst"]=ds;
ds.WriteXml(Server.MapPath("..\\App_Data\\ad.xml"));
fill();
}
protectedvoidLinkButton1_Click(objectsender,EventArgse)
{//上传图片第一步
DIV1.Visible=true;
}
protectedvoidButton6_Click(objectsender,EventArgse)
{//上传图片第二步
stringname=fu.FileName.ToString();//得到文件名字
intcut=Convert.ToInt32(fu.PostedFile.ContentLength);//文件大小
stringtyp=fu.PostedFile.ContentType.ToString();
stringtypp=typ.Substring(0,5);
if(typp=="image")
{
if(cut<Convert.ToInt32(odb.scr("Selectadcutfromconfig")))
{
fu.PostedFile.SaveAs(Server.MapPath("../images/")+"\\"+name);
img.ImageUrl=(Server.MapPath("../images/")+"\\"+name);
url.Text=(Server.MapPath("../images/")+"\\"+name);
}
else
{
url.Text="只是个广告,图片有必要那么大吗?";
}
}
else
{
url.Text="不被允许的类型";
}
}
}
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
<linkhref="../images/news.css"rel="stylesheet"type="text/css"/>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:DataListID="DataList1"runat="server"CellPadding="4"ForeColor="#333333"OnCancelCommand="DataList1_CancelCommand"OnDeleteCommand="DataList1_DeleteCommand"OnEditCommand="DataList1_EditCommand"OnUpdateCommand="DataList1_UpdateCommand">
<ItemTemplate>
<tablewidth="500">
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
广告名称</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;"colspan="2">
<asp:LabelID="Label1"runat="server"Text='<%#Eval("AlternateText")%>'></asp:Label></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
图片地址</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;"colspan="2">
<asp:LabelID="Label2"runat="server"Text='<%#Eval("ImageUrl")%>'></asp:Label></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
关键词</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;"colspan="2">
<asp:LabelID="Label3"runat="server"Text='<%#Eval("Keyword")%>'></asp:Label></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
显示权重</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;"colspan="2">
<asp:LabelID="Label4"runat="server"Text='<%#Eval("Impressions")%>'></asp:Label></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;height:22px">
广告指向</td>
<tdcolspan="2"style="400px;border-bottom:#0099ff1pxdashed;height:22px;
background-color:#99ccff">
<ahref='<%#Eval("NavigateUrl")%>'target=_blank><asp:LabelID="Label5"runat="server"Text='<%#Eval("NavigateUrl")%>'></asp:Label></a></td>
</tr>
<tr>
<tdalign="center"colspan="3"style="border-bottom:#0099ff1pxdashed;height:27px;
background-color:#99ccff">
<asp:ButtonID="Button1"runat="server"CommandName="edit"Text="修改广告"/>
<asp:ButtonID="Button2"runat="server"CommandName="delete"Text="删除广告"/></td>
</tr>
</table>
</ItemTemplate>
<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>
<SelectedItemStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>
<AlternatingItemStyleBackColor="White"/>
<ItemStyleBackColor="#EFF3FB"/>
<HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>
<EditItemTemplate>
<tablewidth="500">
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
广告名称</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;"colspan="2">
<asp:TextBoxID="TextBox1"runat="server"Text='<%#Eval("AlternateText")%>'></asp:TextBox></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
图片地址</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;"colspan="2">
<asp:TextBoxID="TextBox2"runat="server"Text='<%#Eval("ImageUrl")%>'></asp:TextBox></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
关键词</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;"colspan="2">
<asp:TextBoxID="TextBox3"runat="server"Text='<%#Eval("Keyword")%>'></asp:TextBox></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;height:27px;">
显示权重</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;background-color:#99ccff;height:27px;"colspan="2">
<asp:TextBoxID="TextBox4"runat="server"Text='<%#Eval("Impressions")%>'></asp:TextBox></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;height:27px">
广告指向</td>
<tdcolspan="2"style="400px;border-bottom:#0099ff1pxdashed;height:27px;
background-color:#99ccff">
<asp:TextBoxID="TextBox5"runat="server"Text='<%#Eval("NavigateUrl")%>'></asp:TextBox></td>
</tr>
<tr>
<tdalign="center"colspan="3"style="border-bottom:#0099ff1pxdashed;background-color:#99ccff">
<asp:ButtonID="Button3"runat="server"CommandName="update"Text="更新"/>
<asp:ButtonID="Button4"runat="server"CommandName="cancel"Text="取消"/></td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList> <tablewidth="500">
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
广告名称</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;"colspan="2">
<asp:TextBoxID="na"runat="server"Text='<%#Eval("AlternateText")%>'></asp:TextBox></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
图片地址</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;"colspan="2">
<asp:TextBoxID="url"runat="server"Text='<%#Eval("ImageUrl")%>'></asp:TextBox>
<asp:LinkButtonID="LinkButton1"runat="server"OnClick="LinkButton1_Click">上传图片</asp:LinkButton>
<divid="DIV1"runat="server"style="400px;height:100px"visible="false">
<tablestyle="400px;height:100px">
<tr>
<tdcolspan="2"style="height:55px">
<asp:ImageID="img"runat="server"Height="50px"Width="400px"/></td>
</tr>
<tr>
<tdstyle="300px">
<asp:FileUploadID="fu"runat="server"Width="287px"/></td>
<tdstyle="100px">
<asp:ButtonID="Button6"runat="server"OnClick="Button6_Click"Text="上传"/></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;">
关键词</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;"colspan="2">
<asp:TextBoxID="kw"runat="server"Text='<%#Eval("Keyword")%>'></asp:TextBox></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;height:27px;">
显示权重</td>
<tdstyle="400px;border-bottom:#0099ff1pxdashed;"colspan="2">
<asp:TextBoxID="qz"runat="server"Text='<%#Eval("Impressions")%>'></asp:TextBox></td>
</tr>
<tr>
<tdstyle="100px;border-bottom:#0099ff1pxdashed;height:27px">
广告指向</td>
<tdcolspan="2"style="400px;border-bottom:#0099ff1pxdashed">
<asp:TextBoxID="ng"runat="server"></asp:TextBox></td>
</tr>
<tr>
<tdalign="center"colspan="3"style="border-bottom:#0099ff1pxdashed;background-color:#99ccff">
<asp:ButtonID="Button5"runat="server"OnClick="Button5_Click"Text="增加"/></td>
</tr>
</table>
</div>
</form>
</body>
</html>