开发者

List of checkboxes

Happy New Year to all. I'm a newbie in VB.NET and ASP.NET. This is my problem:

I retrieve a list of records from DB and, for every row, I need to show 4 checkboxes. I can use a checkboxlist for every rows, but it's not so clear how I can process the results after the submit.

I've some object and some operations available for that object. From database I extract a list of object with all operations. For every operation I want to show a check box to enable or disable the operation. The result is something like that:

OBJ1 - url - [] [x] []

OBJ2 - url - [] [x] [x]

On url I've an href to another page created using the Id retrieved from DB. To c开发者_如何学Create that I used this code:

<td class="column-filename">
    <strong>
        <asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "GroupName")%>'></asp:Label>
    </strong>                    
</td>    
<td align="left">
    <span style="vertical-align:middle">
    <asp:CheckBoxList runat="server" ID="operations" RepeatDirection="Horizontal" RepeatLayout="Table">
        <asp:ListItem Text="View"></asp:ListItem>
        <asp:ListItem Text="Upload"></asp:ListItem>
        <asp:ListItem Text="Move"></asp:ListItem>
        <asp:ListItem Text="Delete"></asp:ListItem>
        <asp:ListItem Text="Rename"></asp:ListItem>
        <asp:ListItem Text="Replace"></asp:ListItem>
    </asp:CheckBoxList>
    </span>
</td>
</asp>
            </asp>

my problem is: how can I parse all checkboxes?

could you help me or send me a link or any other resources to solve my issue?

many thanks! Andrea


Look at the DataGrid class - you can databind it and style a column to be a checkbox column.

It is a very heavy control (in particular the ViewState), so you may want to substitute for something more lightweight with more control (and more work to implement) such as a Repeater.

In either case, you will be able to access the values in the different postback events.


If you need to display these with each record in a DB, you can use a GridView control. Create the grid with the columns for the record. Then add a GridTemplateField to the control, and add your Checkboxlist. Bind your checkboxlist using a data source control; otherwise, you will have to bind in RowDataBound, and also select the items there too (in both cases). The user can make and save changes in bulk, and you can retrieve the values via:

foreach (GridViewRow row in this.rg.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        var list = (CheckBoxList)row.Cells[<index>].FindControl("chk");
        //Get checked items, save to DB
    }

}


Now it works fine. It's my solution (I post it here because I'm quite sure that next month I'll have the same problem....)

    <asp:DataGrid ID="GroupList" Runat="server" CssClass="widefat"
           AutoGenerateColumns="False"
           BorderColor="#999999" 
           BorderStyle="None" 
           BorderWidth="1px" 
           BackColor="White" 
           CellPadding="3" 
           GridLines="Vertical">
        <Columns>          
            <asp:BoundColumn HeaderText="Status" DataField="GroupName" />
            <asp:BoundColumn HeaderText="Status" DataField="GroupActive" />
            <asp:BoundColumn HeaderText="Status" DataField="GroupID" 
                    Visible="false" />
            <asp:TemplateColumn HeaderText="Users"  
                    ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:HyperLink runat="server"  
                    NavigateUrl="User_manager.aspx" > 
                    <asp:Image runat="server"  
                    ImageUrl="~/images/folders/users.png"  
                    Width="24" /></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="View"  
                    ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="chkView" Runat="server"  
                    Checked='<%# Eval("View").ToString().Equals("1")%>' />
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Upload"  
                    ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="chkUpload" Runat="server"  
                    Checked='<%# Eval("Upload").ToString().Equals("1")%>'/>
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Delete"  
                    ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="chkDelete" Runat="server"  
                    Checked='<%# Eval("Delete").ToString().Equals("1")%>' />
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Move"  
                    ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="chkMove" Runat="server"  
                    Checked='<%# Eval("Move").ToString().Equals("1")%>' />
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Rename"  
                    ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="chkRename" Runat="server"  
                    Checked='<%# Eval("Rename").ToString().Equals("1")%>' />
                </ItemTemplate>
            </asp:TemplateColumn>   
            <asp:TemplateColumn HeaderText="Replace"  
                    ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="chkReplace" Runat="server"  
                    Checked='<%# Eval("Replace").ToString().Equals("1")%>' />
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
        <AlternatingItemStyle BackColor="#f9f9f9" />
        <ItemStyle ForeColor="Black" BackColor="#EEEEEE" BorderStyle="Solid"  
                    BorderColor="#dfdfdf" BorderWidth="1px" />
        <headerStyle Font-Bold="True" ForeColor="Black" BackColor="#dfdfdf"  
                    HorizontalAlign="Center" />
    </asp:DataGrid>

And to parse the result after the submit:

        For intRow = 0 To intRows

            GridItem = GroupList.Items(intRow)            
            groupID = GridItem.Cells(2).Text().Trim()

            If (DirectCast(GridItem.FindControl("chkView"), 
                    CheckBox).Checked) Then
                db.insertGroupOperation(repository_selected, 
                    groupID, op_view)
            End If
            [..]
        Next

Thanks all for the help!

bye, Andrea

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜