gpt4 book ai didi

c# - LiteralControl 出现意外标记 "}"错误

转载 作者:行者123 更新时间:2023-12-02 18:31:44 24 4
gpt4 key购买 nike

我有一个动态表,其中包含来 self 的 asp.net 应用程序的用户(使用成员(member)级别)。我试图在每个 Angular 色单元格内添加一个控件,以便使用 javascript 打开弹出窗口。现在我只检查该功能是否启动。但是当我单击该单元格时,会发生错误:“Uncaught SyntaxError:意外的 token }”

这是我的 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>");

(删除“' '”)它可以工作,但用户名不会作为字符串发送,因此只会提醒数字。**

最佳答案

试试这个

LiteralControl lc = new LiteralControl(String.Format("<a 
onclick=\"alert_function('{0}')\">{1}</a>", u.UserName.ToString(), role[0]));

或者

string link = String.Format("<a onclick=\"alert_function('{0}')\">{1}</a>", 
u.UserName.ToString(), role[0]);
LiteralControl lc = new LiteralControl(link);

关于c# - LiteralControl 出现意外标记 "}"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com