views:

664

answers:

3

I am trying to add a checkbox in a listview with value as ids of the records from the database so I can allow the user to check the ones they want to delete and when they click the delete button I can get value collection of checkbox with request.form.

My problem is, because checkbox in a listview ASP.NET renders the listview name into the name property of the checkbox, it prevents me to do request.form["checkboxname"].

I don't want to use Listviews delete commands but simply use request.form to get the collection of checked values.

How can I set name of the htmlinput checkbox so .NET doesn't change it in render time?

I have tried:

        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)e.Item.FindControl("CheckBoxDelete");

            _CheckBoxDelete.Visible = true;
            _CheckBoxDelete.Value = DataBinder.Eval(dataItem.DataItem, "id").ToString();
            _CheckBoxDelete.Name = "deletechecked";

But still it renders like:

<input name="PmList$ctrl0$CheckBoxDelete" type="checkbox" id="PmList_ctrl0_CheckBoxDelete" value="3" />
A: 

I have sorted it out with:

    string idCollectionTodelete = string.Empty;

    foreach (string x in Request.Form)
    {
        if (x.IndexOf("CheckBoxDelete") > -1)
        {
             idCollectionTodelete += Request.Form[x] + ",";
        }

    }
    new DB().DeleteUserPm(
      ActiveUsername(), subdomain, idCollectionTodelete.TrimEnd(','));

It is not an ideal solution but it does work for me.

nLL
+1  A: 

This is happening because ListView is a Naming Container. You can get around this in a couple of ways, but they all boil down to the choice of:

  • Rendering the HTML you want.
  • Pulling out the checked items in a different way.

The former is doable, but you'll likely loose a lot of ASP.NET's built in functionality. I'd advise against it unless you're deeply familiar with the control life cycle.

You've got everything you need for the pulling the values out in a way ASP is expecting:

HtmlInputCheckBox _CheckBoxDelete = (HtmlInputCheckBox)item.FindControl("CheckBoxDelete");

You just need to wait for the control hierarchy to be populated, and then loop over the ListView.Items looking for the checkboxes. Your "Delete" button's event handler is probably a good place to call this from.

Incidentally, why are you using a HtmlInputCheckbox, rather than a CheckBox?

Dave Roberts
I am new to asp.net and exprimenting with controls, i find htmlinputs easier to manage. if i use asp checkbox i will have to go throgh postbacks and events and in this project getting data with request.form makes life easier to me
nLL
Fair enough. The whole page/control life cycle business can be daunting at first, but it's quite powerful once you get to know it. Good luck with your learning!
Dave Roberts
A: 

I do this

    List<HtmlInputCheckBox> chkDeleteContacts = new List<HtmlInputCheckBox>();

    foreach (RepeaterItem item in rptrFamilyContacts.Items)
    {
        chkDeleteContacts.Add((HtmlInputCheckBox)item.FindControl("chkDeleteContact"));
    }

    foreach(HtmlInputCheckBox chkDeleteContact in chkDeleteContacts)
    {
        //Delete Contact
        if(chkDeleteContact.Checked)
            blnStatus = BusinessUtility.DeleteConsumerContact(LoginConsumerID, chkDeleteContact.Value);
    }

Slightly easier in my opinion

smith288