2010-06-18 68 views
5

我正在使用一个列表视图里面的项目模板中,我正在使用一个标签和一个复选框。 我希望每当用户点击复选框时,值应该在表中更新。我在listview.on中使用datakeys,datakey值的基础应该在表中更新。查询:如何在ASP.NET的ListView中的复选框的CheckedChanged事件上找到数据键?

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

我也想在显示结果的,因为它是table.means内,如果针对特定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> 

帮助我。

+0

检查更新的答案 – 2010-06-18 10:48:25

+0

这是必须的问题? – Subbu 2010-07-27 13:00:23

+0

它不是强制性的,你必须接受,如果它的工作其他它可以我会删除我的答案在这种情况下 – 2010-08-11 07:13:16

回答

4

检查了这一点:可能有助于解决您的歌厅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(); 
} 
+0

我使用Visual Web开发人员2008年快递版本没有这样的DisplayIndex项目显示itellisense。 – Subbu 2010-06-18 10:29:18

+0

use item.Index属性 – 2010-06-18 10:32:22

+0

此属性也不可用 – Subbu 2010-06-18 10:37:28

1

使用数据绑定表达式

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

在你chkFocusArea_CheckedChanged事件处理程序,执行数据库插入和重新绑定数据。

相关问题