Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# 以编程方式将asp:ListView设置为编辑模式,然后进行更新和保存_C#_.net_Asp.net_Web Applications - Fatal编程技术网

C# 以编程方式将asp:ListView设置为编辑模式,然后进行更新和保存

C# 以编程方式将asp:ListView设置为编辑模式,然后进行更新和保存,c#,.net,asp.net,web-applications,C#,.net,Asp.net,Web Applications,我遇到了一个我似乎无法解决的问题 我正在处理的sprint项目要求我在每个项目的项目模板中放置一个asp.net按钮。仅当满足特定条件时,才会显示此按钮。使用ItemDataBound委托来确定是否应该显示所述按钮,这工作正常。此外,在项目模板中,还有一个编辑按钮,该按钮已经使用了一段时间。很多时候,用户单击编辑按钮,然后在编辑模板中单击名为“保存并关闭”的按钮。这会将数据库中的Complete标志设置为true。为了简化此过程,用户希望在项目模板中,能够单击“保存”而不是“编辑”,在编辑项目时

我遇到了一个我似乎无法解决的问题

我正在处理的sprint项目要求我在每个项目的项目模板中放置一个asp.net按钮。仅当满足特定条件时,才会显示此按钮。使用ItemDataBound委托来确定是否应该显示所述按钮,这工作正常。此外,在项目模板中,还有一个编辑按钮,该按钮已经使用了一段时间。很多时候,用户单击编辑按钮,然后在编辑模板中单击名为“保存并关闭”的按钮。这会将数据库中的Complete标志设置为true。为了简化此过程,用户希望在项目模板中,能够单击“保存”而不是“编辑”,在编辑项目时,该操作将立即执行与“保存并关闭”按钮相同的操作

编辑模板中的“保存并关闭”按钮的命令名为“更新”,命令参数为“关闭”。在ItemCommand委托方法中,我找到了一个绑定到linq对象属性的checkbox控件,并将checkbox.Checked设置为true

我目前正试图确定最好的方法是什么。目前,我的项目模板中有一个按钮,命令名为“Update”,命令参数为“CloseReadOnly”。然后,在ItemCommand委托方法中,我执行以下操作:

protected void ActivityListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "Update" && e.CommandArgument == "Close")
    {
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        CheckBox completedCheckBox = (CheckBox)dataItem.FindControl("CompletedCheckBox");
        completedCheckBox.Checked = true;
    }

    // new code
    if (e.CommandName == "Update" && e.CommandArgument == "CloseReadOnly")
    {
        ActivityListView.EditIndex = ((ListViewDataItem)e.Item).DataItemIndex;
        // If I put this into play here, I get an exception. A NullReferenceException to be precise.
        //ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        //CheckBox completedCheckBox = (CheckBox)dataItem.FindControl("CompletedCheckBox");
        //completedCheckBox.Checked = true;
    }
}
执行此操作时,我调试并添加ActivityListView.EditItem的监视,然后我得到一个似乎已转换到编辑模式的项

我还声明了一个ItemUpdate委托方法,该方法通常在用户单击item模板中的edit按钮时使用。此委托方法执行一些操作,例如查找特定控件和更新arguments参数中的NewValues集合

例:

问题是FindControl返回null,显然当试图从null对象转换为int时会抛出异常

所以我可能没有处于编辑模式,因为如果我处于编辑模式,那些FindControl就可以工作了


有人有什么想法吗?

没有所有的代码很难调试,而且您的代码似乎是正确的,但这里有一些猜测

检查在到达该事件之前,是否没有重新绑定listview。 “Update”命令用于调用某些内置功能,可能使用该参数会在某种程度上破坏您的功能。请参阅命令名的详细信息

另一种方法是直接更新数据(不更改控件值),然后重新绑定列表以显示更改

希望这些想法对你有用

protected void ActivityListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
    DropDownList assignedTo = (DropDownList)ActivityListView.Items[e.ItemIndex].FindControl("somecontrol");
    e.NewValues["AssignedTo"] = Convert.ToInt32(assignedTo.SelectedValue);
}