DataGrid Bind Checkbox....

 
DataGrid Bind Checkbox
 
 
前台面页Menu_Config.ascx
 
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Menu_Config.ascx.vb" Inherits="LiveWork.Menu_Confirg" %>
 
<table border="0" cellpadding="0" cellspacing="0" height="100%" style="width: 100%">
    <tr>
        <td valign="top">
            &nbsp;<table border="1" bordercolor="#888888" bordercolordark="#ffffff" cellpadding="0"
                cellspacing="0" width="100%">
                <tr>
                    <td colspan="2" style="height: 14px" valign="top">
                        Role Information</td>
                </tr>
            </table>
            <table border="1" bordercolor="#888888" bordercolordark="#ffffff" cellpadding="0"
                cellspacing="0" width="100%">
                <tr>
                    <td style="width: 3201px; height: 11px" valign="top">
                                    <asp:Label ID="Label_Role" runat="server" Text="Role" Width="90px"></asp:Label>
                                    <asp:DropDownList ID="DropDownList_Role" runat="server" Width="183px" Height="27px">
                                    </asp:DropDownList>
                        <asp:Button ID="Button_Retrieve" runat="server" OnClientClick="Retrieve_Click" Text="Retrieve" /></td>
                    <td style="width: 679px; height: 11px" valign="top">
                                    <asp:HyperLink ID="HyperLink2" runat="server" Font-Size="X-Small" NavigateUrl="~/role.aspx"><img src="img/add.gif" border="0" align="absbottom">&nbsp;New Role</asp:HyperLink></td>
                </tr>
            </table>
            <table border="1" bordercolor="#888888" bordercolordark="#ffffff" cellpadding="0"
                cellspacing="0" width="100%">
                <tr>
                    <td style="width: 671px; height: 7px" valign="top">
                    </td>
                </tr>
            </table>
            <table border="1" bordercolor="#888888" bordercolordark="#ffffff" cellpadding="0"
                cellspacing="0" width="100%">
                <tr>
                    <td style="width: 100%; height: 7px" valign="top">
                        <asp:DataGrid ID="DataGrid_MenuConfigList" runat="server" AutoGenerateColumns="False" BackColor="#F7F7F7"
                            BorderColor="#F7F7F7" BorderStyle="Solid" BorderWidth="1px" CssClass="table"
                            DataKeyField="Menu_ID" Font-Size="X-Small" Width="100%">
                            <FooterStyle CssClass="headcenter" />
                            <EditItemStyle BackColor="Black" ForeColor="Black" Width="100%" />
                            <SelectedItemStyle BackColor="Transparent" />
                            <AlternatingItemStyle BackColor="White" BorderColor="White" />
                            <ItemStyle BackColor="#DEDFDE" CssClass="textcenter" />
                            <HeaderStyle CssClass="headcenter" />
                            <Columns>
                            <asp:TemplateColumn HeaderText="Choice ALL">
                            <HeaderTemplate>                       
                                <asp:CheckBox id="CheckBox_All" runat="server" Text="ALL" OnCheckedChanged="CheckAll" AutoPostBack="True"></asp:CheckBox>
                            </HeaderTemplate>                          
                                <ItemTemplate>
                                <asp:CheckBox id="CheckBox_Role" runat="server"></asp:CheckBox>
                                </ItemTemplate>
                                    <HeaderStyle Width="10px" />
                            </asp:TemplateColumn>
                               
                                <asp:BoundColumn DataField="Role_Name" HeaderText="Role">
                                <HeaderStyle Width="60px" />
                                 </asp:BoundColumn>
                                  <asp:BoundColumn DataField="Menu_ID" HeaderText="Menu ID">
                                  <HeaderStyle Width="0px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="Menu_Name" HeaderText="Menu name">
                                </asp:BoundColumn>
                               <asp:TemplateColumn HeaderText="Permissions">
                                <ItemTemplate>
                                <asp:DropDownList ID="DropDownList_Flag" runat="server" Width=160></asp:DropDownList>
                                </ItemTemplate>
                                <HeaderStyle Width="160px" />
                               </asp:TemplateColumn>
                            </Columns>
                       </asp:DataGrid></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
 
 
后台代码
Namespace LiveWork
    Partial Class Menu_Confirg
        Inherits System.Web.UI.UserControl
 
        Sub InitializeComponent()
            AddHandler Me.Load, AddressOf Me.Page_Load
        End Sub
 
        Private Sub BindRole()
            Dim sql As String
            sql = "select * from tb_role where status='A' "
            Me.DropDownList_Role.DataSource = Live.Ado.ADOProxy.GetRowsByQuery(sql)
            Me.DropDownList_Role.DataTextField = "Role_Name"
            Me.DropDownList_Role.DataValueField = "Role_ID"
            Me.DropDownList_Role.SelectedIndex = 0
            Me.DropDownList_Role.DataBind()
        End Sub
 
        Private Sub BindFlag()
            Dim ds As DataSet = Live.Ado.ADOProxy.GetAllRows("TB_Flag")
 
            For Each lobj_dgi As DataGridItem In Me.DataGrid_MenuConfigList.Items
                Dim dl As DropDownList = CType(lobj_dgi.FindControl("DropDownList_Flag"), DropDownList)
                dl.DataSource = ds
                dl.DataTextField = "Flag_Name"
                dl.DataValueField = "Flag_ID"
                dl.DataBind()
            Next
        End Sub
 
        Private Sub MenuConfig(ByVal lstg_role_id As String)
            Dim sql As New System.Text.StringBuilder
            sql.Append("(select a.role_id,c.role_name,b.menu_id,b.menu_name,a.flag_id,d.flag_name ")
            sql.Append("from tb_role_menu a,tb_menu b,tb_role c,tb_flag d ")
            sql.Append("where a.role_id=c.role_id ")
            sql.Append("and a.menu_id = b.menu_id ")
            sql.Append("and a.flag_id = d.flag_id ")
            sql.Append("and a.status ='A' ")
            sql.Append("and a.role_id='" & lstg_role_id & "')")
            sql.Append("union all ")
            sql.Append("( ")
            sql.Append("select role_id='-',role_name='-',menu_id,menu_name,flag_id='-',flag='-' ")
            sql.Append("from tb_menu ")
            sql.Append("where menu_id not in ")
            sql.Append("(select menu_id from tb_role_menu ")
            sql.Append("where role_id ='" & lstg_role_id & "') ")
            sql.Append(") ")
 
            Dim ds As DataSet = Live.Ado.ADOProxy.GetRowsByQuery(sql.ToString)
            Me.DataGrid_MenuConfigList.DataSource = ds
            Me.DataGrid_MenuConfigList.DataBind()
        End Sub
 
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                BindRole()
                'MenuConfig(Me.DropDownList_Role.SelectedValue)
            End If
 
        End Sub
 
        Protected Sub CheckAll(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim sql As String
            sql = "select * from tb_role where role_id='" & Me.DropDownList_Role.SelectedValue & "' and status='A'"
            Dim lobj_cb As CheckBox = CType(sender, CheckBox)
            Dim ds As DataSet = Live.Ado.ADOProxy.GetRowsByQuery(sql)
            For Each lobj_dgi As DataGridItem In Me.DataGrid_MenuConfigList.Items
                Dim cb As CheckBox = CType(lobj_dgi.FindControl("CheckBox_Role"), CheckBox)
                cb.Checked = lobj_cb.Checked
            Next
 
        End Sub
 
        Private Function GetRoleMenuFlag(ByVal lstg_menu_id As String) As DataSet
            Dim sql As String
            sql = "select a.Flag_ID,b.Flag_Name from tb_role_menu a,tb_flag b where role_id='" & Me.DropDownList_Role.SelectedValue & "' and a.flag_id=b.flag_id and status='A'"
            Dim ds As DataSet = Live.Ado.ADOProxy.GetRowsByQuery(sql)
            Return ds
        End Function
 
        Protected Sub CheckRoleMenu()
 
            For Each lobj_dgi As DataGridItem In Me.DataGrid_MenuConfigList.Items
                If lobj_dgi.Cells(1).Text <> "-" Then
                    Dim cb As CheckBox = CType(lobj_dgi.FindControl("CheckBox_Role"), CheckBox)
                    cb.Checked = True
                    Dim dl As DropDownList = CType(lobj_dgi.FindControl("DropDownList_Flag"), DropDownList)
 
                    dl.SelectedValue = GetRoleMenuFlag(lobj_dgi.Cells(2).Text).Tables(0).Rows(0).Item("flag_id").ToString
 
                End If
            Next
 
        End Sub
 
        'Protected Sub Button_Retrieve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_Retrieve.Click
        '    MenuConfig(Me.DropDownList_Role.SelectedValue)
        '    BindFlag()
        '    CheckRoleMenu()
        'End Sub
 
 
        Protected Sub Retrieve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_Retrieve.Click
            MenuConfig(Me.DropDownList_Role.SelectedValue)
            BindFlag()
            CheckRoleMenu()
        End Sub
    End Class
End Namespace
 
 

 Note:

 

Result:

 

posted on 2008-02-11 15:48  封起De日子  阅读(94)  评论(0编辑  收藏  举报

导航