tags:

views:

217

answers:

3

Hi everyone, I'm working on creating a dynamic list of checklists and came to the below implementation. I have been trying for a week now to get the fnameID text values on the submit button click to send the values into the database. I do not want to use the postback oncheckedchanged.on each check because the checklist is over 1000 rows long and each postback/reload wastes too many resources. I just want to be able to use some method to be able to grab the checked values so I can insert them into the database on the submit button "Click Me!" click.

I googled and found the FindControl method but i still am not able to grab the fnameID values. I either get a undefined or it errors out on me. Any help would be greatly appreciated! Thanks!

aspx:

<div id="aGAccountabilityCheckListBox">
  "Panel1" runat="server">

<asp:LinkButton ID="LinkButton1" Width="66px" runat="server" onclick="LinkButton1_Click">
  Click Me!
</asp:LinkButton>

code behind:

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 50; i++)
    {
        CheckBox _checkbox = new CheckBox();
        _checkbox.ID = "dynamicCheckListBox" + Convert.ToString(i);

        Panel1.Controls.Add(_checkbox);
        Panel1.Controls.Add("&nbsp; <span id='fnameID" + i + "' >test" + i + "&lt;/span>");
    }
}


protected void LinkButton1_Click(object sender, EventArgs e)
{

    SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cnDatabase"].ToString());
    SqlCommand cmd = new SqlCommand("usp_CreateUser", cn);

    cmd.CommandType = CommandType.StoredProcedure;

    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();

}

Thanks!

+3  A: 

State is restored to controls before the load event runs. If your controls don't already exist by then, they'll lose their state and you won't know they were checked. Create your checkboxes in the Init or PreInit event instead.

Joel Coehoorn
A: 

move the checkbox creation to the CreateChildControls page method

to retrieve checkbox state in the LinkButton1_Click handler you can use the following code

for (int i = 0; i < 50; i++)
    {
        string cbxId = string.Format("dynamicCheckListBox{0}", i);
        CheckBox _checkbox = Panel1.FindControl(cbxId) as CheckBox;
        if (_checkbox == null )
          continue;

        if ( _checkbox.Checked )
          //do something
    }
Jason
A: 

Your fnameID's are spans created as a literal control. There is no post back value you are going to get if you set it up that way. It's just arbitrary html or text from the asp.net perspective.

Why are you not using the Text property for CheckBox?

Mufaka
Because for simplicity's sake of showing my code i've cut off three html controls from that literal control. It has to exist instead of using the Text property for CheckBox. Thanks