C# 如何在ASP.NET的ListView中查找checkbox的CheckedChanged事件上的数据键?

C# 如何在ASP.NET的ListView中查找checkbox的CheckedChanged事件上的数据键?,c#,asp.net,listview,checkbox,C#,Asp.net,Listview,Checkbox,我在项目模板中使用列表视图,我使用标签和复选框。 我希望每当用户单击复选框时,表中的值都应该更新。我在listview中使用datakeys。基于datakey,表中的值应该更新。查询是: string updateQuery = "UPDATE [TABLE] SET [COLUMN] = " + Convert.ToInt32(chk.Checked) + " WHERE PK_ID =" + dataKey + " ";` 此外,我还需要一些帮助来显示表中的结果。这意味着如果表中特定p

我在项目模板中使用列表视图,我使用标签和复选框。 我希望每当用户单击复选框时,表中的值都应该更新。我在listview中使用datakeys。基于datakey,表中的值应该更新。查询是:

string updateQuery = "UPDATE [TABLE] SET [COLUMN] = " + Convert.ToInt32(chk.Checked) + " WHERE PK_ID =" + dataKey + " ";` 
此外,我还需要一些帮助来显示表中的结果。这意味着如果表中特定pkid的列的值为1,则应选中复选框

以下是代码片段:

<asp:ListView ID="lvFocusArea" runat="server" DataKeyNames="PK_ID" OnItemDataBound="lvFocusArea_ItemDataBound">
    <LayoutTemplate>
        <table border="0" cellpadding="1" width="400px">
            <tr style="background-color: #E5E5FE">
                <th align="left">
                    Focus Area
                </th>
                <th>
                    Is Current Focused
                </th>
            </tr>
            <tr id="itemPlaceholder" runat="server">
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td width="80%">
                <asp:Label ID="lblFocusArea" runat="server" Text=""><%#Eval("FOCUS_AREA_NAME") %></asp:Label>
            </td>
            <td align="center" width="20%">
                <asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr style="background-color: #EFEFEF">
            <td>
                <asp:Label ID="lblFocusArea" runat="server" Text=""><%#Eval("FOCUS_AREA_NAME") %></asp:Label>
            </td>
            <td align="center">
                <asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <SelectedItemTemplate>
        <td>
            item selected
        </td>
    </SelectedItemTemplate>
</asp:ListView>

重点领域
当前的焦点是什么
选定项目

帮助我。

使用数据绑定表达式

<asp:CheckBox ID="chkFocusArea" runat="server" Checked='<%# Eval("[COLUMN]")  %>' oncheckedchanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />


在chkFocusArea\u CheckedChanged事件处理程序中,执行数据库插入并重新绑定数据。

检查此项:可能有助于解决获取数据密钥的问题

protected void chkFocusArea_CheckedChanged(object sender, EventArgs e)  
{  
    CheckBox cb = (CheckBox)sender;  
    ListViewItem item = (ListViewItem)cb.NamingContainer; 
    ListViewDataItem dataItem = (ListViewDataItem)item ;
    string code = ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
}

我使用的是Visual web developer 2008 express edition。itellisense.bro中没有显示这样的DisplayIndex项。此属性也不可用。我会将最后一行更改为:lvfocusrea.DataKeys[dataItem.DisplayIndex][“PK_ID”];检查更新的答案这不是强制性的,你必须接受,如果它有效,否则它可以,我会删除我的答案在这种情况下