0

I found strange is that when I check some of the checkbox (generated HTML code), the sub function did not alert (or display message) which checkbox is being checked.

Am I missing something?

Inside the WebForm.aspx

 <script type="text/javascript">

 function sub()
 {

    for (i=0; i<arrChecks.length; i++)
    {
        var attribute = arrChecks[i].getAttribute("xid")
        if (attribute == elementName)
        {
            // if the current state is checked, unchecked and vice-versa
            if (arrChecks[i].checked)
            {
                arrChecks[i].checked = false;
            } else {
                arrChecks[i].checked = true;
                alert("Checked!");
            }

        } else {
            arrChecks[i].checked = false;
        }
    }

 }


 </script>

Inside the WebForm.cs

    protected void ButtonCheckDate_Click(object sender, EventArgs e)
    {
        SqlConnection conn = ConnectDB("Data Source=JACKSERVERA;Initial Catalog=tablea;User Id=tableuser;Password=tablepassword");


        if ((TextBox2.Text != null) && (TextBox2.Text != ""))
        {
            try
            {
                String res = "";

                SqlCommand command = conn.CreateCommand();
                SqlDataReader reader;
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = "SELECT * from overviewtable where [dated] = @PDATED";
                command.Parameters.AddWithValue("@PDATED", TextBox2.Text);
                command.Connection = conn;
                sqlstmt.Text = command.CommandText;

                conn.Open();
                reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    res = res + "<form name=\"input\" runat=\"server\"><table border=1>";
                    while (reader.Read())
                    {
                        res = res + "<tr>";


                        string thetime = (string)reader["time"];
                        string thevenue = (string)reader["venue"];
                        int numfreeseat = (int)reader["freeseat"];

                        int occupiedseat = 0;

                        SqlCommand bkcommand = conn.CreateCommand();
                        SqlDataReader bookinglist;
                        bkcommand.CommandType = System.Data.CommandType.Text;
                        bkcommand.CommandText = "SELECT count(*) from venuea where [dated] = @PDATED";
                        bkcommand.Parameters.AddWithValue("@PTIME", thetime);
                        bkcommand.Connection = conn;
                        sqlstmt.Text = bkcommand.CommandText;
                        bookinglist = bkcommand.ExecuteReader();

                        while (bookinglist.Read())
                        {
                            if (bookinglist.HasRows)
                            {
                                occupiedseat = (int)bookinglist.GetValue(0);
                            }
                        }


                        int leftnumofseat = numfreeseat - occupiedseat;

                        string color = "";
                        Boolean fullyoccupied = false;

                        if (leftnumofseat > 0)
                        {
                            if (leftnumofseat == numfreeseat)
                            {
                                // white
                                color = "#FFFFFF";
                            }
                            else
                            {
                                // light gray - partial occupied
                                color = "#B8B8B8";
                            }
                        }
                        else
                        {
                            // dark gray - fully occupied
                            color = "#505050";
                            fullyoccupied = true;
                        }


                        res = res + "<td bgcolor=\"" + color + "\">";
                        res = res + "Available: " + leftnumofseat + "/" + numfreeseat + "";
                        res = res + "</td>";

                        string checkboxval = TextBox2.Text + "_" + thetime + "_" + thevenue;

                        res = res + "<td>";
                        if (fullyoccupied == false)
                        {
                            res = res + "<input type=\"checkbox\" name=\"xid\" value=\"" + checkboxval + "\" runat=\"server\" />";
                        }
                        else
                        {
                            res = res + "FULL";
                        }
                        res = res + "</td>";

                        res = res + "</tr>";
                    }
                    res = res + "</table><input type=\"submit\" value=\"Submit\" OnClick=\"sub\" runat=\"server\"></form>";
                }
                LabelDateSelection.Text = res;

                conn.Close();



            }
            catch (Exception err)
            {
                errormsg.Text = err.Message;
            }
        }
        else
        {
            LabelDateSelection.Text = "Enter Date!";
        }



    }

2 Answers 2

1

Take out this part from you code & see if it works runat=\"server\"

i am not sure if you can create and ASP.net server control as you are trying too & you are sending output to the client while runat=\"server\" is rendered by the server . check you HTML code & see how it looks.

Create HTML control dynamically or create simple HTML control if that can work with your case.

look at the example how to pragmatically create controls

http://msdn.microsoft.com/en-us/library/kyt0fzt1%28v=vs.100%29.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I tried remove the runat=\"server\" but it still does not work. I need to create the table with a list of checkbox dynamically and thus I can't use simple HTML code.
@KnowledgeSeeker is correct you can't create ASP.net controls using JS, all ASP controls must be created through code for them to be properly registered as available.
Sorry, I dynamically created my HTML table and HTML checkbox using CS. I did not use Javascript to dynamically create them. The problem is that I need to dynamically created so that I can detect which check box are checked. (and another problem is that there could be over 200 checkboxes and definitely, I don't want to use static HTML to create them one by one). I was thinking if there are other methods in asp.net that can help me create checkboxes dynamically.
0

In addition to the points made by KnowledgeSeeker, you can't create asp controls in that style.

You can either get the values of the created checkboxes by using the FindControl method to locate the element and then get the value from it or in the C# add code similar to:

for (int i = 0; i < 5; i++)

{

CheckBox chk = new CheckBox();

chk.ID = Convert.ToString(i);

chk.Text = Convert.ToString(i);

form1.Controls.Add(chk);

}

In that instance you are looping from 0 to 5 and creating checkboxes and adding those controls to the form.

2 Comments

Thanks Ryan for your help but there could be 1 to more than 200 checkboxes at anytime (as created dynamically and depend on the database records). Notice that I also wish to find the value of the checkbox that is checked so that I can find which records that the end-user is selecting.
Well yes then you loop through however many records are returned, the 0 to 5 was just example code. If you have set naming conventions for the 200 boxes you can FindControl in a loop searching for checkbox_i - i being the integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.