2010-02-22 26 views
1

我有一个建立在SharePoint中的.aspx页面,我想要获取当前登录用户的用户名(通过ASP),然后操作它(使用javascript)。我收到了“预期的对象”错误。下面是一些代码:SharePoint - 获取登录用户与ASP,操纵与JavaScript?

function checkGroupPermissions() { var loginNameVar = document.getElementById("LoginName1").innerHTML;

--snip--

}

</script> <div id="loginNameDiv" style="display:none"> <asp:LoginName runat="server" id="LoginName1"></asp:LoginName> </div>

我很感激任何和所有的帮助...请,并感谢:)

回答

3

您不能通过直接引用其Id属性引用客户端JavaScript的asp.net服务器控件。你必须使用控件的ClientID属性。由于JavaScript是您的SharePoint的.aspx网页内,你可以称其为:

document.getElementById('<%=LoginName1.ClientID %>'); 

看到这个更多细节:

MSDN article on Client Script in .Net Pages

+0

非常感谢!我得到它的工作和 - 更重要的是 - 我受过更多的教育:) – ACal 2010-02-23 21:35:11

+0

很高兴为你工作。点击复选标记将其选为“已回答”。 – gn22 2010-02-23 22:52:02

0

您还可以使用SharePoint服务。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> 
//you can download this from http://spservices.codeplex.com/releases/view/64390** 
<script type="text/javascript" src="http://yourdomain/site/Style Library/js/jquery.SPServices-0.6.2.min.js"></script> 
<script type="text/javascript"> 

$(document).ready(function(){ 
var curentUserName = $().SPServices.SPGetCurrentUser({ 
fieldName: 'UserName', 
debug: true 
}); 
//Now you can use it howvere you want in My example I write to a Form Text Box with the Client ID: ctl00_m_g_b943e23d_d209_49f9_bf07_9ba3fbf37f17_ff1_new_ctl00_ctl00_TextField That I copied from View Source. 
document.aspnetForm.ctl00_m_g_b943e23d_d209_49f9_bf07_9ba3fbf37f17_ff1_new_ctl00_ctl00_TextField.value = curentUserName.toString();