使用c#脚本在SharePoint页面

本文关键字:SharePoint 页面 脚本 使用 | 更新日期: 2023-09-27 18:04:44

我已经做了相当多的搜索(几个小时实际上),但我还没有能够得到这个工作。基本上,我有这个按钮:

<asp:Button runat="server" Text="Go!" id="go" onClick="getDoc()" />

和这段脚本:

<script type="c#" runat="server">
public void getDoc(object sender, EventArgs e) {
    // Test to see if function was running (it's not...)
    DocFrame.Attributes["src"] = "http://www.google.com";
    // Get the current state of the dropdowns   
    String dropYear = (String)Year.SelectedValue;
    String dropDiv = (String)Division.SelectedValue;
    String dropControl = (String)Control.SelectedValue;
    String dropQuart= (String)Quarter.SelectedValue;
    // Get the Site where the list is
    using (SPSite siteCol = new SPSite("http://portal/Corporate/IT/")) {
        using (SPWeb web = siteCol.RootWeb){
            // Get the list items we need
            SPListItemCollection items = list.GetItems("Year", "Division", "Control", "Quarter");
            SPListItem item = null;
            // Loop through them until we find a matching everything
            foreach (SPListItem it in items){
                if(it.Year == dropYear && it.Division == dropDiv && it.Control == dropControl && it.Quarter == dropQuart){
                    item = it;
                    break;
                }
            }
            // Assign the item as a string  
            String URL = (String)item["Title"];
            // Set the iframe to the new URL
            DocFrame.Attributes["src"] = URL;
        }   
    }
}

这一切都在发生的页面上,请记住,我使用sharepoint不到一个星期,只写过c++代码,所以我可能做的一切都是可怕的错误。无论如何,似乎getDoc()甚至从来没有被调用,所以谁能指出我做错了什么?

使用c#脚本在SharePoint页面

代替

onClick="getDoc()"

你应该做

OnClick="getDoc"

这是连接事件的正确方法。

顺便说一下,你应该考虑遵循c#命名准则。如果您使用更好的命名,它可能看起来像这样:
<asp:Button runat="server" Text="Go!" id="GoBtn" onClick="GoBtn_Click" />

通常惯例是将事件名称附加在控件的ID之后。这不是必需的,但它看起来更干净,其他开发人员在查看您的代码时喜欢看到它。

此外,DocFrame.Attributes["src"] = "http://www.google.com";不是查看函数是否正在运行的好方法。它不会实时更新页面,因为整个服务器端函数都在执行,然后将结果发送到客户端。相反,使用IDE的调试工具来连接到服务器并设置代码中断等。或者让代码给我发一封邮件,我为此创建了一个小工具库