开发者

Programmatically set asp:ListView into edit mode then update and save

开发者 https://www.devze.com 2023-02-14 12:01 出处:网络
I am having a problem that I just can\'t seem to figure out. The sprint item that I am working on requires me to place an asp.net button in the item template for each item. This button is displayed

I am having a problem that I just can't seem to figure out.

The sprint item that I am working on requires me to place an asp.net button in the item template for each item. This button is displayed only if a specific criteria is met. This is working fine, using a ItemDataBound delegate to determine if said button should be shown. Also, in the item template, there is an edit button, which has been in play for some time. Many times, users click the edit button and then click, in the edit template, a button called 'Save and Close'. This sets the Complete flag in the DB to true. To simplify this process, users want to, in the item template, be able to click save instead of edit that immediately does the same thing as 'Save and Close' button when an item is being edited.

The save and close button in the edit template has a command name of 'Update' and a command argument of 'Close'. In the ItemCommand delegate method, I find a checkbox control which is bound to a property on my linq object and I set the checkbox.Checked to true.

I'm currently trying to determine what the best approach would be. At the moment, I have a button in my item template with a CommandName of 'Update' and a CommandArgument of 'CloseReadOnly'. Then, in my ItemCommand delegate method I do the following:

protected void ActivityListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "Update" && e.CommandArgument == "Close")
    {
        开发者_StackOverflow中文版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;
    }
}

When I do this, I debug and add a watch of ActivityListView.EditItem, and I get an item that seems to have transitioned to edit mode.

I also have an ItemUpdating delegate method declared that is normally used when a user clicks the edit button in the item template. This delegate method does a few things such as finding a specific control and updating the NewValues collection in the arguments parameter.

ex:

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

Well, the problem is, FindControl returns null and obviously when trying to convert to int from an object that is null will throw an exception.

So perhaps I'm not in edit mode because if I were, those FindControl's would work.

Any one have any ideas?


Hard to debug without all of the code and your code seems right, but here are some guesses.

Check no rebinding of the listview happens before you get to that event. The "Update" command is used to invoke some built in functionality, maybe using that argument is somehow messing up with your functionality. See details of the command names here

Another way around could be to update your data directly (not changing the controls value) and then rebind your list to display your changes.

Hope any of these ideas work for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消