我有一個動態表與用戶從我的asp.net應用程序(使用成員資格類)。 我試圖在每個角色單元格內添加一個控件,以便使用javascript打開一個彈出窗口。現在我只檢查功能是否啓動。但是當我單擊單元格時發生錯誤:「未捕獲的SyntaxError:意外的令牌}」。意外的令牌「}」與LiteralControl錯誤
這裏是我的aspx:
<head runat="server">
<title></title>
<script type="text/javascript">
function alert_function(id)
{
alert(id);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeholder ID="Placeholder1" runat="server"></asp:placeholder>
</div>
</form>
</body>
</html>
和我的代碼背後:
protected void Page_Load(object sender, EventArgs e)
{
MembershipUserCollection users = Membership.GetAllUsers();
Table tbl = new Table();
TableRow h_row = new TableRow();
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
TableCell c3 = new TableCell();
TableCell c4 = new TableCell();
TableCell c5 = new TableCell();
TableCell c6 = new TableCell();
c1.Text = "משתמש מחובר?";
c2.Text = "התחבר לאחרונה";
c3.Text = "נוצר ב";
c4.Text = "דואר אלקטרוני";
c5.Text = "תפקיד";
c6.Text = "שם משתמש";
h_row.Controls.Add(c1);
h_row.Controls.Add(c2);
h_row.Controls.Add(c3);
h_row.Controls.Add(c4);
h_row.Controls.Add(c5);
h_row.Controls.Add(c6);
tbl.Controls.Add(h_row);
foreach (MembershipUser u in users)
{
TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
TableCell cell5 = new TableCell();
TableCell cell6 = new TableCell();
cell1.Text = u.IsOnline.ToString();
cell2.Text = u.LastLoginDate.ToString();
cell3.Text = u.CreationDate.ToString();
cell4.Text = u.Email;
string user_name=u.UserName;
string[] role = Roles.GetRolesForUser(user_name);
if (role.Length < 1)
cell5.Text = "אין תפקיד";
**else
{
LiteralControl lc=new LiteralControl("<a onclick='alert_function('"+u.UserName.ToString()+"')'>" + role[0] + "</a>");
cell5.Controls.Add(lc);
}**
cell6.Text = u.UserName;
row1.Controls.Add(cell1);
row1.Controls.Add(cell2);
row1.Controls.Add(cell3);
row1.Controls.Add(cell4);
row1.Controls.Add(cell5);
row1.Controls.Add(cell6);
tbl.Controls.Add(row1);
}
Placeholder1.Controls.Add(tbl);
}
**這很奇怪,因爲當我改變加亮後行代碼:
LiteralControl lc=new LiteralControl("<a onclick='alert_function("+u.UserName.ToString()+")'>" + role[0] + "</a>");
(移除在「」「「)它的工作原理,但用戶名不派作爲一個字符串,因此只有數字被警告。**
如果你使用一個字符串,它需要被引用。數字不需要被引用,否則,JavaScript將它解釋爲一個變量的引用......這可能沒有被定義爲 – Ian
但它被引用..這個錯誤很奇怪.. – Dvirski
你需要看看源代碼實際上在瀏覽器中結束了,從中可以看出問題的原因,很容易。 – Pointy