【问题标题】:How to find the data key on CheckedChanged event of checkbox in ListView in ASP.NET?如何在 ASP.NET 的 ListView 中找到复选框 CheckedChanged 事件的数据键?
【发布时间】:2011-03-05 08:40:11
【问题描述】:

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

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

我还需要一些帮助来显示结果,因为它在表内。意味着如果特定 pkid 的表中列的值为 1,则应选中复选框。

这里是sn-p的代码:

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

帮帮我。

【问题讨论】:

  • 检查更新的答案
  • 它不是强制性的,如果它有效,你必须接受,否则没关系,在这种情况下我会删除我的答案

标签: c# asp.net listview checkbox


【解决方案1】:

使用数据绑定表达式

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

在您的 chkFocusArea_CheckedChanged 事件处理程序中,执行您的数据库插入并重新绑定数据。

【讨论】:

    【解决方案2】:

    检查一下:可能有助于解决您获取数据密钥的问题

    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 没有这样的 DisplayIndex 项目在 itellisense 中显示。
    • 兄弟这个属性也不可用
    • 我会将最后一行更改为:lvFocusArea.DataKeys[dataItem.DisplayIndex]["PK_ID"];
    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2013-04-01
    • 2013-03-24
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多