2013-07-18 61 views
0

我有一个动态表与用户从我的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>"); 

(移除在“”““)它的工作原理,但用户名不派作为一个字符串,因此只有数字被警告。**

+0

如果你使用一个字符串,它需要被引用。数字不需要被引用,否则,JavaScript将它解释为一个变量的引用......这可能没有被定义为 – Ian

+0

但它被引用..这个错误很奇怪.. – Dvirski

+0

你需要看看源代码实际上在浏览器中结束了,从中可以看出问题的原因,很容易。 – Pointy

回答

0

逃生你的文字中的引号。

LiteralControl lc = 
    new LiteralControl("<a onclick='alert_function(\"" 
     + u.UserName.ToString() + "\")'>" + role[0] + "</a>"); 

LiteralControl lc = 
    new LiteralControl("<a onclick=\"alert_function('" 
     +u.UserName.ToString()+"')\">" + role[0] + "</a>"); 
+0

谢谢!你真是个好兄弟 – Dvirski

+0

很高兴能有所帮助。 –

1

试试这个

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

问题看起来像您使用的是单引号周围的onclick =“alert_function(”我明白你在双引号包裹了整个事情,所以你尽量避免使用alert_function('username')中的双引号但不正确,例如,如果要删除外部引号并将结果看作字符串,则可以使用onclick ='alert_function('username')'。请注意,你用单引号包装了onclick值,但你也使用单引号两个包装用户名,如果你单看这个字符串,你应该能够看到问题,整个字符串应该用单引号括起来,但是在那里有多个单引号将字符串拆分为多个字符串。您仍需要替换单引号和双引号,因此即使在处理字符串内的字符串时也不会发生这种情况。

若要解决此问题,请在要将单引号或双引号嵌套在已被同一引号包围的字符串中的位置,使用反斜杠转义。这将直接将该引号字符作为字符放入字符串中,而不是结束字符串。

LiteralControl lc=new LiteralControl("<a onclick='alert_function(\""+u.UserName.ToString()+"\")'>" + role[0] + "</a>"); 

这基本上是你的第二个解决方案,除了我添加了第二个\”包围在字符串中您的用户名在引号中的价值。