ListView CheckBox问题

时间:2015-02-05 06:33:36

标签: c# asp.net listview checkbox

我的网格格式为ListView,其中包含标题为(NAMEIDADDRESSGENDERMOBILENO PHOTOBLOCK)。

BLOCK列每行包含CheckBox

BLOCK列中选中复选框后, 复选框的特定行应更改为ReadOnly, 导致特定行的背景颜色发生变化。

我在aspx中的listview编码是......

        <AlternatingItemTemplate>
            <tr  class="AITtr">

                <td>
                    <asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="ADDRESSLabel" runat="server" Text='<%# Eval("ADDRESS") %>' />
                </td>
                <td>
                    <asp:Label ID="GENDERLabel" runat="server" Text='<%# Eval("GENDER") %>' />
                </td>
                <td>
                    <asp:Label ID="MOBILENOLabel" runat="server" Text='<%# Eval("MOBILENO") %>' />
                </td>
                <td>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("PHOTO") %>' Height="30" Width="30" />
                </td>
                <td>
                    <asp:CheckBox ID="BLOCKCheckBox" runat="server"  Checked="false" Enabled="true" />
                </td>
                <td>
                   <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light" />
                   <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light"/>
                </td>
            </tr>
        </AlternatingItemTemplate>

        <EditItemTemplate>
            <tr class="EITtr">

                <td>
                    <asp:TextBox ID="NAMETextBox" runat="server" Text='<%# Bind("NAME") %>' />
                </td>
                <td>
                    <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ADDRESSTextBox" runat="server" Text='<%# Bind("ADDRESS") %>' />
                </td>
                <td>
                    <asp:TextBox ID="GENDERTextBox" runat="server" Text='<%# Bind("GENDER") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MOBILENOTextBox" runat="server" Text='<%# Bind("MOBILENO") %>' />

                </td>
                <td>
                    <asp:TextBox ID="PHOTOTextBox" runat="server" Text='<%# Bind("PHOTO") %>' />
                </td>
                <td>
                    <asp:CheckBox ID="BLOCKCheckBox" runat="server" Checked="false" Enabled="true"   />
                </td>
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                        Text="Update" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Cancel" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light" />
                </td>
            </tr>
        </EditItemTemplate>

        <EmptyDataTemplate>
            <table runat="server" class="EDTtable" >
                <tr>
                    <td>
                        No data was returned.</td>
                </tr>
            </table>
        </EmptyDataTemplate>

        <InsertItemTemplate>
            <tr>

                <td>
                    <asp:TextBox ID="NAMETextBox" runat="server" Text='<%# Bind("NAME") %>' />
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:TextBox ID="ADDRESSTextBox" runat="server" Text='<%# Bind("ADDRESS") %>' />
                </td>
                <td>
                    <asp:TextBox ID="GENDERTextBox" runat="server" Text='<%# Bind("GENDER") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MOBILENOTextBox" runat="server" Text='<%# Bind("MOBILENO") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PHOTOTextBox" runat="server" Text='<%# Bind("PHOTO") %>' />
                </td>
                <td>
                    <asp:CheckBox ID="BLOCKCheckBox" runat="server"  Checked="false" Enabled="true"  />
                </td>
                 <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                        Text="Insert" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light"/>
                    <asp:Button ID="CancelButton1" runat="server" CommandName="Cancel" 
                        Text="Clear" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light" />
                </td>
            </tr>
        </InsertItemTemplate>

        <ItemTemplate>
            <tr class="ITtr">

                <td>
                    <asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="ADDRESSLabel" runat="server" Text='<%# Eval("ADDRESS") %>' />
                </td>
                <td>
                    <asp:Label ID="GENDERLabel" runat="server" Text='<%# Eval("GENDER") %>' />
                </td>
                <td>
                    <asp:Label ID="MOBILENOLabel" runat="server" Text='<%# Eval("MOBILENO") %>' />
                </td>
                <td>
                    <asp:Image ID="Image" runat="server" ImageUrl='<%#Eval("PHOTO") %>' Height="30" Width="30" />
                </td>
                <td>
                    <asp:CheckBox ID="BLOCKCheckBox" runat="server"  Checked="false" Enabled="true" />
                </td>
                 <td>
                   <asp:Button ID="DeleteButton1" runat="server" CommandName="Delete" 
                        Text="Delete" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light" />
                    <asp:Button ID="EditButton1" runat="server" CommandName="Edit" Text="Edit" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light"/>
                </td>
            </tr>
        </ItemTemplate>

        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="1" class="LTtable" >

                      <tr runat="server" Class="LTtr">

                                <th runat="server">
                                    NAME</th>
                                <th runat="server">
                                    ID</th>
                                <th runat="server">
                                    ADDRESS</th>
                                <th runat="server">
                                    GENDER</th>
                                <th runat="server">
                                    MOBILENO</th>
                                <th runat="server">
                                    PHOTO</th>
                                <th runat="server">
                                    BLOCK</th>
                                <th id="Th1" runat="server">
                                </th>
                            </tr>
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" class="LTtd">
                    </td>
                </tr>
            </table>
        </LayoutTemplate>

        <SelectedItemTemplate>
            <tr Class=".SITtr">

                <td>
                    <asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="ADDRESSLabel" runat="server" Text='<%# Eval("ADDRESS") %>' />
                </td>
                <td>
                    <asp:Label ID="GENDERLabel" runat="server" Text='<%# Eval("GENDER") %>' />
                </td>
                <td>
                    <asp:Label ID="MOBILENOLabel" runat="server" Text='<%# Eval("MOBILENO") %>' />
                </td>
                 <td>
                     <asp:Label ID="PHOTOLabel" runat="server" Text='<%# Eval("PHOTO") %>' />
                </td>
                 <td>
                    <asp:CheckBox ID="BLOCKCheckBox" runat="server" Checked="false" Enabled="true"  />
                </td>
                <td>
                    <asp:Button ID="DeleteButton2" runat="server" CommandName="Delete" 
                        Text="Delete" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light" />
                    <asp:Button ID="EditButton2" runat="server" CommandName="Edit" Text="Edit" BackColor="#577109" ForeColor="Black" BorderStyle="Solid" Font-Names="Copperplate Gothic Light"/>
                </td>
            </tr>
        </SelectedItemTemplate>

    </asp:ListView>

我的aspx.cs编码是......

 protected void ListView1_OnItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            CheckBox BLOCKCheckBox = (e.Item.FindControl("BLOCKCheckBox") as CheckBox);
            if (BLOCKCheckBox.Checked)
            {

            }
            else
            {

            }

        }
    }

请帮助我......

谢谢....

1 个答案:

答案 0 :(得分:1)

为块复选框添加oncheckedChanged并使用autopostback = true

                

<强> aspx.cs

 protected void BLOCKCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkbox = (CheckBox)sender;
        ListViewItem itm = (ListViewItem)checkbox.NamingContainer;
        if (lstview1.EditIndex == itm.DisplayIndex)
        {

            (itm.FindControl("NAMETextBox") as TextBox).ReadOnly = true;
        }
    }