[英]Web application with generated HTML code consist of checkbox not display message. (Asp.net, C#)
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. 我发现奇怪的是,当我选中某些复选框(生成的HTML代码)时,子功能没有警告(或显示消息)正在选中哪个复选框。
Am I missing something? 我想念什么吗?
Inside the WebForm.aspx 在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 在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!";
}
}
Take out this part from you code & see if it works runat=\\"server\\"
从您的代码中删除此部分,并查看它是否可以运行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 . 我不确定是否可以像尝试一样创建和ASP.net服务器控件,并且在服务器渲染runat=\\"server\\"
时将输出发送到客户端。 check you HTML code & see how it looks. 检查您的HTML代码并查看其外观。
Create HTML control dynamically or create simple HTML control if that can work with your case. 动态创建HTML控件或创建简单的HTML控件(如果可以的话)。
look at the example how to pragmatically create controls 看这个例子,如何实际地创建控件
http://msdn.microsoft.com/en-us/library/kyt0fzt1%28v=vs.100%29.aspx http://msdn.microsoft.com/zh-CN/library/kyt0fzt1%28v=vs.100%29.aspx
In addition to the points made by KnowledgeSeeker, you can't create asp controls in that style. 除了KnowledgeSeeker提出的要点外,您不能以该样式创建ASP控件。
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: 您可以通过使用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);
}
In that instance you are looping from 0 to 5 and creating checkboxes and adding those controls to the form. 在这种情况下,您要从0循环到5,并创建复选框并将这些控件添加到表单中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.