c# - 如何通过服务器代码 C# 将 javascript 代码添加到 LinkBut​​ton?

标签 c# javascript jquery asp.net sharepoint

我正在创建一个带有链接按钮的可视化 Web 部件,该链接按钮应该打开一个模式弹出窗口。

我已经定义了该函数,但我不确定如何使链接按钮触发 JavaScript 函数。

请参阅CreateChildControls方法

    <asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
        <script type="text/javascript">
            function dialogfunction(pageUrl) {
                var options = { url: pageUrl, width: 900, height: 300 };
                SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
            }
        </script>
    </asp:Content>
    <asp:LinkButton ID="LnkButton" runat="server"></asp:LinkButton>



  public partial class LinkButton : WebPart
    {
        // Visual Studio might automatically update this path when you change the
        // Visual Web Part project item.
        private const string _ascxPath =@"~/_CONTROLTEMPLATES/VisualWebParts/LinkButton/LinkButton.ascx";

        private string _LinkText;
        private Uri _Link;


        [WebBrowsable(true), WebDisplayName("LinkText"), WebDescription("Text for the link"),
        Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
        System.ComponentModel.DefaultValue("")]
        public string LinkText
        {
            get { return _LinkText; }
            set { _LinkText = value; }
        }

        [WebBrowsable(true), WebDisplayName("Link"), WebDescription("Link"),
        Personalizable(PersonalizationScope.Shared), Category("xx- xx"),
        System.ComponentModel.DefaultValue("")]
        public Uri Link
        {
            get { return _Link; }
            set { _Link = value; }
        }

        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public LinkButton()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void CreateChildControls()
        {
            LinkButton linkButton = Page.LoadControl(_ascxPath) as LinkButton;
            if (linkButton != null)
            {
                linkButton.Title = LinkText;
                linkButton.TitleUrl = Link.ToString();
                Controls.Add(linkButton);
            }           
        }
    }

最佳答案

只需在代码中添加一行:

linkButton.Attributes.Add("onclick", "dialogfunction('" + Link.ToString() + "');");

关于c# - 如何通过服务器代码 C# 将 javascript 代码添加到 LinkBut​​ton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16583486/

相关文章:

c# - WPF - 为基类实现 System.ComponentModel.INotifyPropertyChanged

javascript - 如何解决 jquery+flash 插件和 require.js 之间的范围问题

javascript - 将 twitter api1.0 迁移到 1.1 json 数据访问问题

javascript - 如何为单个元素的 jQuery 组合框设置 minLength

javascript - 如何防止 jQuery 结果以两种不同的形式显示?

javascript - CSS 响应式图像 slider

c# - 为什么 Request.QueryString ["path"] 将所有 + 号转换为空格?

c# - 具有泛型参数的 Asp.net 方法

javascript - 根据输入的日期获取所有 javascript 时区

c# - 获取异步方法的结果