Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically create and use a list of checkboxes from ASP.NET?

Tags:

I have a page with a table of stuff and I need to allow the user to select rows to process. I've figured out how to add a column of check boxes to the table but I can't seem to figure out how to test if they are checked when the form is submitted. If they were static elements, I'd be able to just check do this.theCheckBox but they are programaticly generated.

Also I'm not very happy with how I'm attaching my data to them (by stuffing it in there ID property).

I'm not sure if it's relevant but I'm looking at a bit of a catch-22 as I need to known which of the checkboxes that were created last time around were checked before I can re-run the code that created them.


Edit: I've found an almost solution. By setting the AutoPostBack property and the CheckedChanged event:

checkbox.AutoPostBack = false;
checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);

I can get code to be called on a post back for any check box that has changed. However this has two problems:

  • The call back is processed after (or during, I'm not sure) Page_Load where I need to use this information
  • The call back is not called for check boxes that were checked when the page loaded and still are.

Edit 2:

What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of Form_Load:

foreach (string v in this.Request.Form.AllKeys)
{
    if (v.StartsWith(Prefix))
    {
        var data = v.Substring(Prefix.Length);
    }
}

everything else seems to run to late.

like image 385
BCS Avatar asked Jul 24 '09 22:07

BCS


2 Answers

I'm going to assume you're using a DataList but this should work with and Control that can be templated. I'm also going to assume you're using DataBinding.

Code Front:

<asp:DataList ID="List" OnItemDataBound="List_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:CheckBox ID="DeleteMe" runat="server"/>
        <a href="<%# DataBinder.Eval(Container, "DataItem.Url")%>" target="_blank">
            <%# DataBinder.Eval(Container, "DataItem.Title")%></a>
    </ItemTemplate>
</asp:DataList>
<asp:Button ID="DeleteListItem" runat="server" OnClick="DeleteListItem_Click" ></asp:Button>

Code Behind:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadList();
    }

    protected void DeleteListItem_Click(object sender, EventArgs e)
    {
        foreach (DataListItem li in List.Items)
        {
            CheckBox delMe = (CheckBox)li.FindControl("DeleteMe");

            if (delMe != null && delMe.Checked)
                    //Do Something
            }
        }

        LoadList();
    }

    protected void LoadList()
    {
        DataTable dt = //Something...
        List.DataSource = dt;
        List.DataBind();
    }

    protected void List_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string id = DataBinder.Eval(e.Item.DataItem, "ID").ToString();
            CheckBox delMe = (CheckBox)e.Item.FindControl("DeleteMe");

            if (delMe != null)
                delMe.Attributes.Add("value", id);                
        }
    }
}
like image 126
MyItchyChin Avatar answered Oct 27 '22 19:10

MyItchyChin


First, make sure that each Checkbox has an ID and that it's got the 'runat="server"' in the tag.

then use the FindControl() function to find it.

For example, if you're looping through all rows in a GridView..

foreach(GridViewRow r in Gridview1.Rows)
{

    object cb = r.FindControl("MyCheckBoxId");
    if(r != null)
    {
      CheckBox chk = (CheckBox)cb;
      bool IsChecked = chk.Checked;
    }

}
like image 42
David Avatar answered Oct 27 '22 19:10

David