首页 > 编程 > .NET > 正文

用vb.net做的校友录……(附所有源代码)

2024-07-10 12:58:37
字体:
来源:转载
供稿:网友
想必大家都上过校友录吧,里面的功能虽然不是很强大,但是为我们这些毕业之后各奔西东的学子来说,到真是提供了一个好的聚集点,下面是我用vb.net做的校友录,当然也不能说是校友录了,只能说是我们班的学友录了:)不过只要你掌握了这种编程思想,校友录就不在话下了。这里我将重点谈一下关于管理员权限赋予。
以前有这个想法的时候,难就难在管理员身份的赋予上,开始想如果一般用户在被提升为管理员之后,我可以把他的信息转到另外一个表中,以后登陆的时候先检查manage表中是否有他就行了,这种办法我已经实现了,是不是有些笨……可是正在我要正式开工的时候,一个想法突然冒了出来,是什么呢?呵呵,就是在数据库中再增加一个字段了如果是第一次注册就让这个字段item(int)的值为0,要是被提升之后就update为1,班级创始人呢?当然了,在他申请时付给他一个班级id,然后先判断班级id为这个已经注册的人信息条数是否为0,如果是,那么就付给他item=2好了。下面的代码中,我没有实现这个功能,因为我做的是班友录的,当然了,我就是管理员喽:)

在这里,我使用的是checkboxlist(两个),一个用来转换数据(visibe=fause),一个用来显示数据,还有一个checkbox用来写全部选中的事件。当然了还有两个按钮事件,一个是用来提升为管理员的,一个是用来降级为一般用户的,这两个按钮在判断为一般用户时
enable都是为fause的,只有当判断登录为管理员时才为true。当然了,如果你是班级创始人,是不可能被降级的:)
这里用来显示信息的我用的是一个datagrid,当判断为非管理员时,删除栏的visible将为fause,为管理员的时候,才为true,也就是说只有管理员才可以删除信息。而不是注册的用户是不能发言的所有的按钮控件的enable将都为fause。
代码如下:board.aspx
<%@ page language="vb" autoeventwireup="false" codebehind="boaman.aspx.vb" inherits="_99re1.boaman"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
    <head>
        <title></title>
        <meta content="microsoft visual studio.net 7.0" name="generator">
        <meta content="visual basic 7.0" name="code_language">
        <meta content="javascript" name="vs_defaultclientscript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
    </head>
    <body background="image/bg.gif" ms_positioning="gridlayout">
        <form id="form1" method="post" runat="server">
            <asp:checkbox id="yourcheck" style="z-index: 101; left: 84px; position: absolute; top: 71px" runat="server" text="全部选中" autopostback="true" forecolor="saddlebrown" font-bold="true"></asp:checkbox>
            <asp:button id="button1" style="z-index: 102; left: 34px; position: absolute; top: 43px" runat="server" text="提升为管理员" height="20px" width="100px" backcolor="gainsboro" bordercolor="lavender" borderwidth="2px" borderstyle="outset"></asp:button>
            <asp:button id="button2" style="z-index: 103; left: 142px; position: absolute; top: 43px" runat="server" text="降级为一般用户" height="20px" width="100px" backcolor="gainsboro" bordercolor="lavender" borderwidth="2px" borderstyle="outset"></asp:button>
            <asp:checkboxlist id="checkboxlist1" style="z-index: 104; left: 14px; position: absolute; top: 10px" runat="server" visible="false"></asp:checkboxlist>
            <asp:checkboxlist id="mycheck" style="z-index: 105; left: 98px; position: absolute; top: 99px" runat="server" forecolor="navy" font-size="x-small"></asp:checkboxlist>
            <asp:label id="label1" style="z-index: 106; left: 82px; position: absolute; top: 14px" runat="server" forecolor="red" font-names="方正姚体">(已注册用户)</asp:label>
            <asp:image id="image1" style="z-index: 107; left: 260px; position: absolute; top: 180px" runat="server" height="124px" width="221px" imageurl="image/99re1-1.gif"></asp:image>
            <asp:datagrid id="datagrid1" style="z-index: 108; left: 250px; position: absolute; top: 83px" runat="server" height="113px" width="461px" bordercolor="#ffcc66" autogeneratecolumns="false" headerstyle-font-size="9" headerstyle-horizontalalign="center" headerstyle-forecolor="red" headerstyle-font-bold="true">
                <columns>
                    <asp:hyperlinkcolumn itemstyle-forecolor="navy" itemstyle-horizontalalign="center" itemstyle-height="24" itemstyle-font-size="9" datanavigateurlfield="stu_id" datanavigateurlformatstring="querry.aspx?stu_id={0}" datatextfield="stu_id" headertext="学号"></asp:hyperlinkcolumn>
                    <asp:boundcolumn itemstyle-forecolor="navy" itemstyle-horizontalalign="center" itemstyle-width="100" itemstyle-font-size="9" datafield="tel" headertext="电话"></asp:boundcolumn>
                    <asp:boundcolumn itemstyle-forecolor="navy" itemstyle-horizontalalign="center" itemstyle-width="80" itemstyle-font-size="9" datafield="oicq" headertext="oicq"></asp:boundcolumn>
                    <asp:boundcolumn itemstyle-forecolor="navy" itemstyle-horizontalalign="center" itemstyle-width="140" itemstyle-font-size="9" datafield="email" headertext="e-mail"></asp:boundcolumn>
                    <asp:boundcolumn itemstyle-forecolor="navy" itemstyle-horizontalalign="center" itemstyle-width="60" itemstyle-font-size="9" datafield="point" headertext="登录次数"></asp:boundcolumn>
                </columns>
            </asp:datagrid>
        </form>
    </body>
</html>


下面是codebehind中的内容:boaman.asp.vb

imports system.data
imports system.data.sqlclient
public class boaman
    inherits system.web.ui.page
        protected withevents yourcheck as system.web.ui.webcontrols.checkbox
    protected withevents button1 as system.web.ui.webcontrols.button
    protected withevents checkboxlist1 as system.web.ui.webcontrols.checkboxlist
    protected withevents mycheck as system.web.ui.webcontrols.checkboxlist
    protected withevents label1 as system.web.ui.webcontrols.label
    protected withevents image1 as system.web.ui.webcontrols.image
    protected withevents datagrid1 as system.web.ui.webcontrols.datagrid
    protected withevents button2 as system.web.ui.webcontrols.button

#region " web form designer generated code "

    'this call is required by the web form designer.
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

    end sub

    private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
        'codegen: this method call is required by the web form designer
        'do not modify it using the code editor.
        initializecomponent()
    end sub

#end region
    dim conn as sqlconnection = new sqlconnection("server=lixinri;uid=sa;pwd=;database=99re1")
    private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
        '////////////////////判断是否为过客
        if session("stu_id") = "" then
            button1.enabled = false : button2.enabled = false
            '////////////////////调用check()事件
            check()
        else
            if not ispostback then
                dim sql as string = "select * from pwd where [email protected]_id"
                dim comm as sqlcommand = new sqlcommand(sql, conn)
                comm.parameters.add(new sqlparameter("@stu_id", sqldbtype.int, 4))
                comm.parameters("@stu_id").value = session("stu_id")
                dim dr as sqldatareader
                conn.open()
                dr = comm.executereader
                while dr.read
                    if dr.item("term") = "0" then
                        '///////////////////判断是否为一般用户
                        dr.close()
                        button1.enabled = false : button2.enabled = false
                        dim sql_1 as string = "select a.stu_id as stu_id,a.term,b.name as name,b.tel as tel,b.call as oicq,b.point as point,b.email from pwd a,stu_base b where a.stu_id=b.stu_id"
                        dim cmd as new sqlcommand(sql_1, conn)
                        dr = cmd.executereader
                        while dr.read
                            if dr.item("term") = "1" then
                                mycheck.items.add("<u>" & dr.item("name") & "</u>" & "<font color=darkorange>(管理员)</font>")
                            elseif dr.item("term") = "2" then
                                mycheck.items.add("<u>" & dr.item("name") & "</u>" & "<font color=red>(班级创始人)<font>")
                            else
                                mycheck.items.add("<u>" & dr.item("name") & "</u>")
                            end if
                            checkboxlist1.items.add(dr.item("stu_id"))
                        end while
                    else
                        '////////////////////判断是否为管理员
                        dr.close()
                        button1.enabled = true : button2.enabled = true
                        dim sql_1 as string = "select a.stu_id as stu_id,a.term,b.name as name,b.tel as tel,b.call as oicq,b.point as point,b.email from pwd a,stu_base b where a.stu_id=b.stu_id"
                        dim cmd as new sqlcommand(sql_1, conn)
                        dr = cmd.executereader
                        while dr.read
                            if dr.item("term") = "1" then
                                mycheck.items.add("<u>" & dr.item("name") & "</u>" & "<font color=darkorange>(管理员)</font>")
                            elseif dr.item("term") = "2" then
                                mycheck.items.add("<u>" & dr.item("name") & "</u>" & "<font color=red>(班级创始人)<font>")
                            else
                                mycheck.items.add("<u>" & dr.item("name") & "</u>")
                            end if
                            checkboxlist1.items.add(dr.item("stu_id"))
                        end while
                    end if
                end while
                dr.close()
                '////////////////////////取出数据,填充dataset
                dim mysql as string = "select a.stu_id as stu_id,a.term,b.name as name,b.tel as tel,b.call as oicq,b.point as point,b.email from pwd a,stu_base b where a.stu_id=b.stu_id"
                comm = new sqlcommand(mysql, conn)
                dim mycomm as sqldataadapter = new sqldataadapter(mysql, conn)
                dim ds as dataset = new dataset()
                mycomm.fill(ds, "base")
                datagrid1.datasource = ds.tables("base").defaultview
                datagrid1.databind()
            end if
        end if
    end sub

    '///////////////////////书写check()事件
    sub check()
        if not ispostback then
            dim mysql as string = "select a.stu_id as stu_id,a.term,b.name as name,b.tel as tel,b.call as oicq,b.point as point,b.email from pwd a,stu_base b where a.stu_id=b.stu_id"
            dim comm as new sqlcommand(mysql, conn)
            dim dr as sqldatareader
            conn.open()
            dr = comm.executereader
            while dr.read
                if dr.item("term") = "1" then
                    mycheck.items.add("<u>" & dr.item("name") & "</u>" & "<font color=darkorange>(管理员)</font>")
                elseif dr.item("term") = "2" then
                    mycheck.items.add("<u>" & dr.item("name") & "</u>" & "<font color=red>(班级创始人)<font>")
                else
                    mycheck.items.add("<u>" & dr.item("name") & "</u>")
                end if
                checkboxlist1.items.add(dr.item("stu_id"))
            end while
            dr.close()
            dim mycomm as sqldataadapter = new sqldataadapter(mysql, conn)
            dim ds as dataset = new dataset()
            mycomm.fill(ds, "base")
            datagrid1.datasource = ds.tables("base").defaultview
            datagrid1.databind()
        end if
    end sub
    '/////////////////////填充yourcheck
    private sub yourcheck_checkedchanged(byval sender as system.object, byval e as system.eventargs) handles yourcheck.checkedchanged
        dim i as integer
        for i = 0 to mycheck.items.count - 1
            if yourcheck.checked then
                mycheck.items(i).selected = true
            else
                mycheck.items(i).selected = false
            end if
        next
    end sub

    '///////////////////////提升一般用户为管理员
    private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
        conn.open()
        dim i as integer
        for i = 0 to mycheck.items.count - 1
            if mycheck.items(i).selected then
                dim sql_1 as string = "update pwd set term=1 where [email protected]_id and term=0"
                dim comm as sqlcommand = new sqlcommand(sql_1, conn)
                comm.parameters.add(new sqlparameter("@stu_id", sqldbtype.int, 4))
                comm.parameters("@stu_id").value = checkboxlist1.items(i).text
                comm.executenonquery()
            end if
        next
        response.redirect("boaman.aspx")
    end sub

    '///////////////////////降级管理员为一般用户
    private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
        conn.open()
        dim i as integer
        for i = 0 to mycheck.items.count - 1
            if mycheck.items(i).selected then
                dim sql_1 as string = "update pwd set term=0 where [email protected]_id and term=1"
                dim comm as sqlcommand = new sqlcommand(sql_1, conn)
                comm.parameters.add(new sqlparameter("@stu_id", sqldbtype.int, 4))
                comm.parameters("@stu_id").value = checkboxlist1.items(i).text
                comm.executenonquery()
            end if
        next
        response.redirect("boaman.aspx")
    end sub
end class


当然了,这里面还有好多不足的地方,恳求高手批评指正。
校友录还有一些其它的功能就很简单了,这里就不再探讨了。
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表