想必大家都上過(guò)校友錄吧,里面的功能雖然不是很強(qiáng)大,但是為我們這些畢業(yè)之后各奔西東的學(xué)子來(lái)說(shuō),到真是提供了一個(gè)好的聚集點(diǎn),下面是我用vb.net做的校友錄,當(dāng)然也不能說(shuō)是校友錄了,只能說(shuō)是我們班的學(xué)友錄了:)不過(guò)只要你掌握了這種編程思想,校友錄就不在話(huà)下了。這里我將重點(diǎn)談一下關(guān)于管理員權(quán)限賦予。
以前有這個(gè)想法的時(shí)候,難就難在管理員身份的賦予上,開(kāi)始想如果一般用戶(hù)在被提升為管理員之后,我可以把他的信息轉(zhuǎn)到另外一個(gè)表中,以后登陸的時(shí)候先檢查manage表中是否有他就行了,這種辦法我已經(jīng)實(shí)現(xiàn)了,是不是有些笨……可是正在我要正式開(kāi)工的時(shí)候,一個(gè)想法突然冒了出來(lái),是什么呢?呵呵,就是在數(shù)據(jù)庫(kù)中再增加一個(gè)字段了如果是第一次注冊(cè)就讓這個(gè)字段item(int)的值為0,要是被提升之后就update為1,班級(jí)創(chuàng)始人呢?當(dāng)然了,在他申請(qǐng)時(shí)付給他一個(gè)班級(jí)id,然后先判斷班級(jí)id為這個(gè)已經(jīng)注冊(cè)的人信息條數(shù)是否為0,如果是,那么就付給他item=2好了。下面的代碼中,我沒(méi)有實(shí)現(xiàn)這個(gè)功能,因?yàn)槲易龅氖前嘤唁浀?,?dāng)然了,我就是管理員嘍:)
在這里,我使用的是checkboxlist(兩個(gè)),一個(gè)用來(lái)轉(zhuǎn)換數(shù)據(jù)(visibe=fause),一個(gè)用來(lái)顯示數(shù)據(jù),還有一個(gè)checkbox用來(lái)寫(xiě)全部選中的事件。當(dāng)然了還有兩個(gè)按鈕事件,一個(gè)是用來(lái)提升為管理員的,一個(gè)是用來(lái)降級(jí)為一般用戶(hù)的,這兩個(gè)按鈕在判斷為一般用戶(hù)時(shí)
enable都是為fause的,只有當(dāng)判斷登錄為管理員時(shí)才為true。當(dāng)然了,如果你是班級(jí)創(chuàng)始人,是不可能被降級(jí)的:)
這里用來(lái)顯示信息的我用的是一個(gè)datagrid,當(dāng)判斷為非管理員時(shí),刪除欄的visible將為fause,為管理員的時(shí)候,才為true,也就是說(shuō)只有管理員才可以刪除信息。而不是注冊(cè)的用戶(hù)是不能發(fā)言的所有的按鈕控件的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="降級(jí)為一般用戶(hù)" 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="方正姚體">(已注冊(cè)用戶(hù))</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="學(xué)號(hào)"></asp:hyperlinkcolumn>
                    <asp:boundcolumn itemstyle-forecolor="navy" itemstyle-horizontalalign="center" itemstyle-width="100" itemstyle-font-size="9" datafield="tel" headertext="電話(huà)"></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="登錄次數(shù)"></asp:boundcolumn>
                </columns>
            </asp:datagrid>
        </form>
    </body>
</html>
下面是codebehind中的內(nèi)容: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
        '////////////////////判斷是否為過(guò)客
        if session("stu_id") = "" then
            button1.enabled = false : button2.enabled = false
            '////////////////////調(diào)用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
                        '///////////////////判斷是否為一般用戶(hù)
                        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>(班級(jí)創(chuàng)始人)<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>(班級(jí)創(chuàng)始人)<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()
                '////////////////////////取出數(shù)據(jù),填充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
    '///////////////////////書(shū)寫(xiě)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>(班級(jí)創(chuàng)始人)<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
    '///////////////////////提升一般用戶(hù)為管理員
    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
    '///////////////////////降級(jí)管理員為一般用戶(hù)
    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
當(dāng)然了,這里面還有好多不足的地方,懇求高手批評(píng)指正。
校友錄還有一些其它的功能就很簡(jiǎn)單了,這里就不再探討了。