Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选中复选框后,使用listview字段更新数据库_C#_Listview_Webforms - Fatal编程技术网

C# 选中复选框后,使用listview字段更新数据库

C# 选中复选框后,使用listview字段更新数据库,c#,listview,webforms,C#,Listview,Webforms,我使用listview显示数据库中的各种列/行。其中一列是“id”,对用户是隐藏的。另一列是一个复选框,用户可以单击该复选框“选择任务” 我不太熟悉web表单和ListView的使用,因此非常感谢您的帮助 除了您可能会在回发时丢失SelectedItem之外,您的代码还有一些问题。所以我只想在这里补充几点 protected void check1_CheckedChanged(object sender, EventArgs e) { //this is null... Lis

我使用listview显示数据库中的各种列/行。其中一列是“id”,对用户是隐藏的。另一列是一个复选框,用户可以单击该复选框“选择任务”


我不太熟悉web表单和ListView的使用,因此非常感谢您的帮助

除了您可能会在回发时丢失SelectedItem之外,您的代码还有一些问题。所以我只想在这里补充几点

protected void check1_CheckedChanged(object sender, EventArgs e) {
    //this is null...
    ListViewItem item = (ListViewItem)listViewDataSource.SelectedValue;

    if (item == null) {
        if (listViewDataSource.Items.Count > 0) {
            item = (ListViewItem)listViewDataSource.Items[0];
        }
    }

    if (item != null) {
        //this returns a checkbox = true value and upon expanding has lots of data that's not super helpful, except maybe the clientID which has the row number
        CheckBox myCheckBox = (CheckBox)sender;

        // add NULL CHECK

        //FindControl doesn't seem to find anything, gets null
        CheckBox chk = (CheckBox)item.FindControl("bitIsPickedUp");

        // add NULL CHECK

        if (chk.Checked) {
            string intId = ((Label)item.FindControl("id")).Text;
            bool success = Int32.TryParse(intId, out int id);
            if (success) {
                GoQuote.InsertIntoGoList(id, 16073);
            }
        }
    } else {
        // log error -- execute additonal logic
    }

你正在使用
?@JohnPete22不,只是隐藏它。你的ListView是动态创建的吗?也就是说,它需要在回发时再次进行数据绑定。只是一个想法。@JohnPete22是的,它是动态创建的。要验证您是否需要重新绑定。。。调试,然后在回发时查看即时窗口中的
listViewDataSource
,或者在到达该代码行时将鼠标悬停在变量上。查看
Items
集合,看看它是否有任何项目。如果没有,你知道你需要重新绑定。我还为“空检查”添加了一些注释——因为任何时候你必须找到一个控件或强制转换一个控件,确保你有一个对象总是一个好主意。我读了很多关于如何使这项工作正常进行的帖子,所有这些都融合在一起了。我现在看到的是:listViewDataSource.SelectedIndex为0;listViewDataSource.SelectedValue为1,listViewDataSource.SelectedDataKey返回的datakey对象具有(“Id”,1)…不是实际Id,Id为158004。也许我在aspx页面上设置了一些错误?我想你需要在你的原始帖子中显示ListView的HTML,这可能有助于了解一些情况。所以我更新了原始的ask。我知道check1_CheckedChanged中的代码不正确,我正试图让其中的某些东西工作。现在看起来DataKeyNames并没有像我期望的那样工作。我从来没有在
DataKeyNames
中使用过多个键——只使用了一个唯一的值,然后可以用来查找我需要获取的对象/类/数据。
<tr runat="server">
<td class="hidden">
   <asp:Label ID="id" runat="server" Text='<%#Eval("id") %>' /> 
</td>
<td>
  <asp:CheckBox ID="check" runat="server" OnCheckedChanged="check1_CheckedChanged" AutoPostBack="True" Checked='<%# (bool)Eval("bitIsPickedUp") %>' />
  <asp:Label ID="CheckLabel" runat="server" Text='<%#Eval("bitIsPickedUp") %>' />
</td>
    protected void check1_CheckedChanged(object sender, EventArgs e)
    {
        //in here, I want to get the id, which I should be able to use listViewDataSource.SelectedDataKey but the datakey has one value and it's not correct, makes me think it's being cached somehow?
        var datakey = listViewDataSource.SelectedDataKey;
        CheckBox chk = (CheckBox)item.FindControl("bitIsPickedUp");


    }
protected void check1_CheckedChanged(object sender, EventArgs e) {
    //this is null...
    ListViewItem item = (ListViewItem)listViewDataSource.SelectedValue;

    if (item == null) {
        if (listViewDataSource.Items.Count > 0) {
            item = (ListViewItem)listViewDataSource.Items[0];
        }
    }

    if (item != null) {
        //this returns a checkbox = true value and upon expanding has lots of data that's not super helpful, except maybe the clientID which has the row number
        CheckBox myCheckBox = (CheckBox)sender;

        // add NULL CHECK

        //FindControl doesn't seem to find anything, gets null
        CheckBox chk = (CheckBox)item.FindControl("bitIsPickedUp");

        // add NULL CHECK

        if (chk.Checked) {
            string intId = ((Label)item.FindControl("id")).Text;
            bool success = Int32.TryParse(intId, out int id);
            if (success) {
                GoQuote.InsertIntoGoList(id, 16073);
            }
        }
    } else {
        // log error -- execute additonal logic
    }