• GridView使用XmlDataSource控件来操作Xml数据的更新


    前台代码:

    代码
    <div style="float:left">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor
    ="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
    CellPadding
    ="3" CellSpacing="2" DataSourceID="XmlDataSource1" Height="223px"
    onrowupdating
    ="GridView1_RowUpdating" Width="561px">
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <Columns>
    <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
    <asp:BoundField DataField="img" HeaderText="img" SortExpression="img" />
    <asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
    <asp:BoundField DataField="target" HeaderText="target"
    SortExpression
    ="target" />
    <asp:CommandField ShowSelectButton="True" />
    <asp:CommandField ShowEditButton="True" />
    </Columns>
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server"
    DataFile
    ="~/viewerData.xml"></asp:XmlDataSource>
    </div>
    <div style="float:left;">
    &nbsp;&nbsp;&nbsp;<asp:Button ID="btnRefresh" Text="更新" runat="server"
    onclick
    ="btnRefresh_Click" />
    </div>

    后台代码:

    代码
    public partial class _Default : System.Web.UI.Page
    {
    DataSet ds
    = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    DataLoad();
    }
    }
    private void DataLoad()
    {
    GridView1.DataSourceID
    = "XmlDataSource1";
    GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    string strTitle = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
    string strImgPath = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
    string strUrl = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
    string strTarget = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
    GridViewRow gvr
    =GridView1.Rows[e.RowIndex];
    string filepath = Server.MapPath("viewerData.xml");
    ds.ReadXml(filepath);
    DataRow dr
    = ds.Tables[1].Rows[gvr.DataItemIndex];
    dr[
    0] = strTitle;
    dr[
    1] = strImgPath;
    dr[
    2] = strUrl;
    dr[
    3] = strTarget;
    ds.WriteXml(filepath);
    GridView1.DataSourceID
    = "XmlDataSource1";
    GridView1.DataBind();
    e.Cancel
    = true;//表示取消使用GridView的缺省的数据更新操作
    GridView1.EditIndex = -1;//要退出编辑模式,把GridView对象的当前编辑行设成-1
    //GridView会自动设法调用XmlDataSource的update方法(通过IDataSourceConrol/DataSourceView接口),但是XmlDataSource不支持自动更新
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
    DataLoad();
    }
    }
  • 相关阅读:
    flex
    当前不会命中断点 源代码与原始版本不一致
    c setjmp longjmp
    VS 快捷键设置
    Lua C API 遍历 table
    lua class
    复习 C++ 中类的函数指针
    apache ab
    rabbitmq
    协程 coroutine
  • 原文地址:https://www.cnblogs.com/strivers/p/1904833.html
Copyright © 2020-2023  润新知