【问题标题】:How can I add jQuery to a WebPart (to respond to certain events on the WebPart)?如何将 jQuery 添加到 WebPart(以响应 WebPart 上的某些事件)?
【发布时间】:2015-08-15 11:55:43
【问题描述】:

我开始着手将 UpdatePanel 添加到我的 Sharepoint 页面,这样我就可以响应发生的事件(例如检查复选框和混合单选按钮等)。然而,到目前为止,这一切都充满了挫败感和腐烂,这可以从this question 中推断出来。

所以我现在可能要遍历(不是双关语)jQuery 路线,并试图弄清楚/找出如何将 jQuery 添加到 WebPart。

当前,我正在根据用户在 WebPart 的编辑器中选择的内容在 C# 中动态创建控件(如果他们选择显示第 1 节,我将为第 1 节创建所有控件,等等)。

例如,我目前在 C# 中有条件地创建一个 RadioButton,如下所示:

var radbtnEmployeeQ = new RadioButton
{
    CssClass = "dplatypus-webform-field-input"
};

现在如果我想给它添加 jQuery,我可以像这样给它添加一个 ID:

var radbtnEmployeeQ = new RadioButton
{
    CssClass = "dplatypus-webform-field-input",
    ID = "radbtnEmp"
};

...然后添加这种性质的jQuery(伪代码):

$('radbtnEmp').click {
    // visiblize/invisiblize other controls, assign certain vals to their properties
}

这对我来说似乎可行,但是如何将 jQuery 添加到 WebPart 中呢?是在与*.ascx.cs文件相同的目录级别添加一个.js文件,然后从*.ascx.cs文件中引用它,还是...???

更新

对于 POC 测试,我在我的项目中添加了一个名为“directpaydynamic.js”的文件,其中包含以下内容:

<script>
    $(document).ready(function () {
        $('input:radio[name=radbtnEmp]:checked').change(function () {
            if ($("input[name='radbtnEmp']:checked").val() == 'Employee?') {
                alert("radbtnEmp checked");
            }
            else {
                alert("radbtnEmp not checked");
            }
        });
    });
</script>

(源自示例here)

...并像这样在我的 *.ascx.cs 文件中引用 .js 文件:

SPWeb site = SPContext.Current.Web; 
site.CustomJavaScriptFileUrl = @"C:\Projects\DirectPaymentWebForm\DirectPaymentSectionsWebPart\DPSVisualWebPart\directpaydynamic.js";

(我把.js文件拖到代码上获取路径)

但是,运行解决方案并混合单选按钮不会导致 alert() 显示,所以我认为我选择了一条人迹罕至的路。

更新 2

意识到我不需要脚本业务(因为这是一个 .js 文件,而不是 html 文件),我删除了这些,并且还在 jQuery 中放置了一个“无论如何”alert():

$(document).ready(function () {
    alert("What's new, pussycat, whoa-oh-oh-oh-OH-oh?");
    $('input:radio[name=radbtnEmp]:checked').change(function () {
        if ($("input[name='radbtnEmp']:checked").val() == 'Employee?') {
            alert("radbtnEmp checked");
        }
        else {
            alert("radbtnEmp not checked");
        }
    });
});

...但我仍然忍受着警报的匮乏...

【问题讨论】:

    标签: c# jquery sharepoint-2010 web-parts


    【解决方案1】:

    在页面加载方法上使用以下代码

      string url = SPContext.Current.Site.ServerRelativeUrl;
    
            if (!url.EndsWith("/"))
            {
                url += "/";
            }
    HtmlGenericControl styleCss = new HtmlGenericControl("link");
            styleCss.Attributes.Add("rel", "stylesheet");
            styleCss.Attributes.Add("type", "text/css");
            styleCss.Attributes.Add("href", url + "Style Library/css/style.css");
    HtmlGenericControl JsLink = new HtmlGenericControl("script");
            JsLink.Attributes.Add("src", url + "Style    Library/js/jquery.min.js");`enter code here`
    this.Controls.Add(styleCss);
    this.Controls.Add(JsLink);
    

    【讨论】:

    • “在此处输入代码”是什么意思?举例说明?
    • 默认添加,您可以删除“输入代码”,剩下的就可以了。
    【解决方案2】:

    要做的事情(或者我应该写,“a”要做的事情)要让它工作是在 WebPart 的 *.ascx 文件的末尾添加如下代码:

    <script>
    $(document).ready(function () {
        alert("The ready function has been reached");
    });
    
    $(document).on("change", '[id$=ckbxEmp]', function () {
        alert('Function has been reached');
        if ($(this).is(":checked")) {
            alert("Checked");
        } else {
            alert("Not checked");
        }
    }); 
    </script>
    

    上面的工作就像一个冠军。

    【讨论】:

      猜你喜欢
      • 2010-11-12
      • 2015-08-15
      • 2013-01-29
      • 1970-01-01
      • 2021-08-27
      • 2012-02-14
      • 1970-01-01
      • 2015-12-12
      • 2015-05-05
      相关资源
      最近更新 更多