首页 > 开发 > XML > 正文

将xml作为DataGrid 操作(Sort, Edit, delete)

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

我们将显示如何将一个xml文件作为datagrid,这样对其的排序,编辑,等操作将是非常简单的事。
<%@ import namespace="system.io" %>
<%@ import namespace="system.data" %>
<html>
<script language="vb" runat="server">

sub page_load(src as object, e as eventargs)
        if not (ispostback)
            dataload("isbn")
        end if
end sub

sub dataload(parmsort as string)
         dim ds as new dataset
        dim fs as new filestream(server.mappath("books.xml"), filemode.open)
        ds.readxml(fs)
        mydatagrid.datasource = new dataview(ds.tables(0))
        mydatagrid.databind()
        fs.close()
end sub

sub datasort(src as object, e as datagridsortcommandeventargs)
    ' bug if we sort, then edit item becomes wrong
    if mydatagrid.edititemindex=-1 then
        dataload(e.sortexpression)
    else
        response.write ("can't sort until editing is done!")
    end if
end sub   

sub datadelete(sender as object, e as datagridcommandeventargs)
    dim deletekey as string
    if mydatagrid.edititemindex=-1 then
        deletekey=mydatagrid.datakeys(cint(e.item.itemindex))
        response.write ("deleted " & deletekey)
    else
        response.write ("can't delete until editing is done!")
    end if
end sub


sub dataedit(sender as object, e as datagridcommandeventargs)
        dim editkey as string
        mydatagrid.edititemindex = cint(e.item.itemindex)
        editkey=mydatagrid.datakeys(cint(e.item.itemindex))
        'response.write ("to be edited" & editkey)
        dataload("")
end sub

sub datacancel(sender as object, e as datagridcommandeventargs)
        mydatagrid.edititemindex = -1
        response.write ("edit was cancelled")
        dataload("")
end sub

sub dataupdate(sender as object, e as datagridcommandeventargs)
        dim editkey as string
        mydatagrid.edititemindex = -1
       editkey = mydatagrid.datakeys(cint(e.item.itemindex))
       response.write ("to be updated " & editkey)
        dataload("")
       ' howmanycols = e.item.cells.count
end sub

</script>

<body>

<h3><font face="verdana">the best books ever</font>
<span runat="server" id="myspan"/></h3>

<form runat="server">
<asp:datagrid id="mydatagrid" runat="server"

   allowsorting="true"
   onsortcommand="datasort"
   ondeletecommand="datadelete"
   oneditcommand="dataedit"
   oncancelcommand="datacancel"
   onupdatecommand="dataupdate"   
   
    datakeyfield="isbn"

   width="100%"
   backcolor="white"
   bordercolor="black"
   showfooter="false"
    cellpadding=3
   cellspacing="0"
   font-name="verdana"
   font-size="8pt"
   headerstyle-backcolor="lightblue"
   headerstyle-font-size="10pt"
   headerstyle-font-style="bold"
   maintainstate="true"
    >

     
     
    <columns>
      <asp:buttoncolumn text="delete book" commandname="delete"/>
     
      <asp:editcommandcolumn edittext="edit" canceltext="cancel" updatetext="update" itemstyle-wrap="false"/>
    </columns>
</asp:datagrid>

</form>
    
</body>
</html>
xml的源文件:
<books>
    <book>
    <isbn>0070653623</isbn>
    <author>jack trout, steve rivkin</author>
    <title>the power of simplicity</title>
    <category>selfhelp</category>
    <comments>a real fun read</comments>
    </book>
    
    <book>
    <isbn>0887306667</isbn>
    <author>al reiss, jack trout</author>
    <title>22 immutable laws of marketing</title>
    <category>marketing</category>
    <comments>this team offers more profound advice about creating world class marketing campaigns that will be viable for a hundred years.</comments>
    </book>

    <book>
    <isbn>0887309372</isbn>
    <author>al reiss, laura reiss</author>
    <title>22 immutable laws of branding</title>
    <category>marketing</category>
    <comments>this book is great for people who used 22 immutable laws of marketing to build a brand and now want to strengthen that brand.</comments>
    </book>
    
    <book>
    <isbn>0679757651</isbn>
    <author>tom peters</author>
    <title>circle of innovation</title>
    <category>marketing</category>
    <comments>his most recent book is his best by far!</comments>
    </book>
    
    <book>
    <isbn>0884270610</isbn>
    <author>eli goldthrait</author>
    <title>the goal</title>
    <category>management</category>
    <comments>advocate of theory of constraints as applied to managment and optimization.</comments>
    </book>

    <book>
    <isbn>068485600x</isbn>
    <author>jeff cox, howard stevens</author>
    <title>selling the wheel</title>
    <category>management</category>
    <comments>excellent treatise/novel on the entire sales cycle</comments>
    </book>

    <book>
    <isbn>0672316498</isbn>
    <author>alan cooper</author>
    <title>the inmates are running the asylum</title>
    <category>management</category>
    <comments>the father of visual basic and creator of the new art of interaction design - very valuable in designing websites. basically the worlds most  cutting edge thinker in user interface design aimed at simplifying software use.</comments>
    </book>
    
</books>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表