【问题标题】:On Checkbox selection, update database using listview fields在复选框选择时,使用列表视图字段更新数据库
【发布时间】:2020-03-06 17:24:42
【问题描述】:

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

<asp:ListView ID="listViewDataSource" ConvertEmptyStringToNull="true"  ItemPlaceholderID="itemPlaceholder" runat="server" DataKeyNames="id,intFrmID" SelectedIndex="0">

然后在ItemTemplate中:

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

当有人点击该复选框时,我想在数据库中创建一条新记录。为此,我需要获取隐藏的“id”字段并将其传递给添加记录的函数。

    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");


    }

我对 Web 表单和使用 ListView 不是非常熟悉,因此非常感谢您的帮助。

【问题讨论】:

  • 您使用的是&lt;asp:HiddenField /&gt; 吗?
  • @JohnPete22 不,只是隐藏它。
  • 你的 ListView 是动态创建的吗?这意味着,它需要在 PostBacks 上再次进行数据绑定。只是一个想法。
  • @JohnPete22 是的,它是动态创建的。
  • 要验证您是否需要重新绑定... 调试,然后在 PostBack 上查看即时窗口中的 listViewDataSource,或者在到达该行时将鼠标悬停在变量上代码。查看Items 集合,看看它是否真的有任何项目。如果没有,你知道你需要重新绑定。

标签: c# listview webforms


【解决方案1】:

除了您可能会在 PostBacks 上丢失 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
    }

【讨论】:

  • 我还添加了一些用于“NULL 检查”的 cmets——因为无论何时你必须找到一个控件或强制转换一个控件,确保你有一个对象总是一个好主意。
  • 我已经阅读了很多关于如何完成这项工作的帖子,以至于它们都融合在一起了。我现在看到的是:listViewDataSource.SelectedIndex 为 0; listViewDataSource.SelectedValue 为 1,listViewDataSource.SelectedDataKey 返回一个具有 ("Id", 1) 的 datakey 对象......这不是实际的 id,id 是 158004。也许我在 aspx 页面上设置了错误?
  • 我认为您需要在原始帖子中显示 ListView 的 HTML,这可能有助于阐明一些问题。
  • 所以我更新了原始问题。我知道 check1_CheckedChanged 中的代码不正确,我试图让 something 在那里工作。现在看来,DataKeyNames 没有按我的预期工作。
  • 我从未在DataKeyNames 中使用过多个键——只使用了一个唯一的值,然后可用于查找我需要获取的对象/类/数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 2014-06-27
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多