0

奇妙なことに、チェックボックス(生成されたHTMLコード)の一部をチェックすると、サブ関数がどのチェックボックスがチェックされているかを警告(またはメッセージ表示)しませんでした。

私は何かが足りないのですか?

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>

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!";
        }



    }
4

2 に答える 2

1

コードからこの部分を取り出して、動作するかどうかを確認しますrunat=\"server\"

あなたも試しているので、ASP.netサーバーコントロールを作成してできるかどうかはわかりませんrunat=\"server\"。サーバーによってレンダリングされている間に出力をクライアントに送信しています。HTMLコードをチェックして、それがどのように見えるかを確認してください。

HTML コントロールを動的に作成するか、ケースで機能する場合は単純な HTML コントロールを作成します。

実際にコントロールを作成する方法の例を見てください

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

于 2012-10-30T08:30:12.137 に答える
0

KnowledgeSeeker によって行われたポイントに加えて、そのスタイルで asp コントロールを作成することはできません。

作成されたチェックボックスの値を取得するには、FindControl メソッドを使用して要素を見つけてから値を取得するか、C# で次のようなコードを追加します。

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);

}

そのインスタンスでは、0 から 5 までループし、チェックボックスを作成して、それらのコントロールをフォームに追加しています。

于 2012-10-30T08:40:24.897 に答える