简体   繁体   English

如何在ASP.NET的ListView中的CheckedChanged事件的复选框中找到数据键?

[英]How to find the data key on CheckedChanged event of checkbox in ListView in ASP.NET?

I am using a list view inside that in item template i am using a label and a checkbox. 我正在使用项目模板中的列表视图,正在使用标签和复选框。 I want that whenever user clicks on the check box the value should be updated in a table.i am using a datakeys in listview.on the basis of datakey value should be updated in the table. 我希望每当用户单击复选框时,值都应该在表中更新。我正在使用listview中的数据键。基于数据键的值应该在表中更新。 Query is: 查询是:

string updateQuery = "UPDATE [TABLE] SET [COLUMN] = " + Convert.ToInt32(chk.Checked) + " WHERE PK_ID =" + dataKey + " ";` 

also i want some help in displaying the result as it is inside the table.means if the value for column in table for a particular pkid is 1 then the checkbox shoul be checked. 我也希望在显示结果时有所帮助,因为它位于table.means中。如果表中特定pkid的列的值为1,则应选中此复选框。

Here is the code snippet: 这是代码片段:

<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>

Help me. 帮我。

Check this out : may help to resolve your issue of geting datakey 检查一下:可能有助于解决您获取数据密钥的问题

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();
}

Use Data Binding Expression 使用数据绑定表达式

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

In your chkFocusArea_CheckedChanged event handler, perform your database insertion and rebind the data. 在chkFocusArea_CheckedChanged事件处理程序中,执行数据库插入并重新绑定数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM