0

I am creating a visual webpart with a linkbutton, the linkbutton is supposed to open a modal popup.

I already have the function defined, but I am not sure how to make the link button to fire a javascript function.

Pls see the CreateChildControls method

    <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);
            }           
        }
    }
1
  • you can use LnkButton.OnClientClick =
    – Pete
    Commented May 16, 2013 at 9:24

3 Answers 3

2

Just add one line to your code:

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

YOu can set the onclick attribute of your linkbutton to be javascript:

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

                /*************************************************************/
                /* you can replace "alert('test');" with any javascript
                i.e a function call */

                linkButton..Attributes.Add("onclick", "alert('test'); ");
                /*************************************************************/

                Controls.Add(linkButton);
            }           
        }
1

You could define a LinkOnClientClick property, similar to your LinkText property, and use it to set the LinkButton's OnClientClick property.

public string LinkOnClientClick
{
    get; set;
}

protected override void CreateChildControls()
{
   LinkButton linkButton = Page.LoadControl(_ascxPath) as LinkButton;
   if (linkButton != null)
   {
         linkButton.Title = LinkText;
         linkButton.TitleUrl = Link.ToString();
         linkButton.OnClientClick = LinkOnClientClick;
         Controls.Add(linkButton);
   }           
}
2
  • And don't forget to end your js onclientclick method by a return false; or else your LinkButton will do a PostBack
    – Trajan
    Commented May 16, 2013 at 9:50
  • @Trajan yes, you're correct. I'd assumed a certain knowledge of JS and Web Forms technology, but it's definitely worth pointing that out.
    – Netricity
    Commented May 16, 2013 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.