我在一个可视化 Web 部件中创建了一个 jQuery/javascript 中的模态对话框弹出窗口,每次页面加载时都会显示。弹出窗口中有一个提交按钮,单击它时,我将用户名和确认日期存储在自定义列表中。有效的jQ代码:
<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(openDialog, "SP.js");
});
//open dialog
// Call openDialog method on button click or on page load
function openDialog() {
//alert("In funciton opendialog");
var options = {
//html: divModalDialogContent, // ID of the HTML tag
// or HTML content to be displayed in modal dialog
width: 400,
url: "/_layouts/ModalDialog1/ModalDialog.aspx",
height: 125,
title: "Acknowedgement Popup",
dialogReturnValueCallback: dialogCallbackMethod, // custom callback function
allowMaximize: false,
showClose: false
};
SP.UI.ModalDialog.showModalDialog(options);
}
// Custom callback function after the dialog is closed
function dialogCallbackMethod(result, returnValue) {
alert("dialogResult" + result + "nreturnValue" + returnValue);
if(result == SP.UI.DialogResult.OK)
{
alert("You chose the OK button");
//document.title = returnValue;
}
if(result == SP.UI.DialogResult.cancel)
SP.UI.Notify.addNotification("You chose the Cancel button");
}
}
</script>
现在,我正在使用此代码在列表中添加用户名。我使用 aspx 页面将其存储在列表中,该页面在弹出窗口中单击按钮时触发:
protected void Button1_Click(object sender, EventArgs e)
{
Context.Response.Write(@"<script type='text/javascript'>
alert('Thanks for acknowledging!');
window.frameElement.commitPopup();
</script>");
Context.Response.Flush();
Context.Response.End();
string username;
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.RootWeb)
{
web.AllowUnsafeUpdates = true;
SPUser user = web.CurrentUser;
username = user.LoginName.ToString();
//adding list item
SPList l = web.Lists["Acknowledgements"];
SPListItem li = l.Items.Add();
li["User"] = username;
li["Acknowedgement Date"] = System.DateTime.Today;
li.Update();
}
}
现在,客户要求是在月初显示模式对话框弹出窗口,如果自定义列表中已经存在用户名,则不显示该月的模式对话框弹出窗口。我用谷歌搜索并了解了 registerstartupscript,您可以通过它在后面的代码中编写 javascript 并使用它,但它不起作用。我打算比较列表中的用户名和当前登录的用户名,如果他已经确认,则不显示弹出窗口,但它击败了我如何实现这一点,而且,javascript 在客户端。
string Script = @"function openDialog() { var options={
width:400,
url: '/_layouts/ModalDialog/ModalDialog.aspx', height:125,
title: 'Acknowedgement Popup',
dialogReturnValueCallback: dialogCallbackMethod,
allowMaximize: false,
showClose: false
};
SP.UI.ModalDialog.showModalDialog(options);
}";Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openDialog", Script, true);
我在这一切上都走对了吗?我对 SharePoint 有点陌生,所以请帮忙。这让我陷入了困境。提前感谢您的所有帮助。