Opening a link in a modal dialog window

hello ,

I have created a share point webpart using c# code ( with no aspx file ). and i have a line in with i have added a link and i would like that the link should be opened in a modal dialog window like in sharePoint.

my code is :

 str2 = string.Concat(new object[] { obj2, "<p>", this.FirstWords(this.StripTagsCharArray(item[this.BodyField].ToString()), Convert.ToInt32(this.NumWords)), "... <br/><a href=\"",web.Url, "/Lists/", this.Text, "/DispForm.aspx?ID=", item.ID, "\" > read more</a></p></div></div></div>" });

I have referenced the function that open url in modal dialog window like this : 

string str = "<script type=\"text/javascript\">function OpenDialog(URL) {var options = SP.UI.$create_DialogOptions();"+" options.url = URL; options.width = 600; options.height = 400; SP.UI.ModalDialog.showModalDialog(options);}</script>"

I would like to call my function in the href attribute of my link "red more" but i get syntax error because of special characters.

How to call this function correctly ?

Thanks.

July 15th, 2013 4:02pm

Hi Lamine,

You can use the onclick event of the anchor tag with the SP.UI.ModalDialog.OpenPopUpPage without having to create options. It might be more straight forward. Return false so that the browser doesn't navigate to the url in the href property of the anchor tag:

<a onclick="javascript:SP.UI.ModalDialog.OpenPopUpPage(url,callbackFunction,width,height);return false;" href="">read more</a>
The MSDN documentation for OpenPopUpPage is here: http://msdn.microsoft.com/en-us/library/ff410825(v=office.14).aspx
Free Windows Admin Tool Kit Click here and download it now
July 15th, 2013 4:20pm

hi matthew ,


i have mentionned that i get syntax error when adding attritube href that call a javascript function and i get the same error when trying your solution.

my problem is syntax because  i have coded all in c# so we have to code all in a string variable.

how to write your code correctly using a string variable without forgotting the " " and the "/" ? 

thanks.


July 15th, 2013 4:51pm

One way is to add a label control to the webparts controls, then add the <a> tag to the labels text property. You can get the display from url from the list.

E.g.

private Label readMore;

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    readMore = new Label();
    Controls.Add(readMore);
}


protected override void OnPreRender(EventArgs e)
{
	base.OnPreRender(e);
	SPList list = SPContext.Current.Web.Lists.TryGetList("Mylist");
	if (list == null) return;            
	SPListItem item = list.Items[0];
	var url = String.Format("{0}?ID={1}", list.DefaultDisplayFormUrl, item.ID);
	readMore.Text = String.Format("<a onclick=\"javascript:SP.UI.ModalDialog.OpenPopUpPage('{0}',null,600,400);return false;\" href=\"#blank\">Read more...</a>",url);
}

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2013 5:40pm

Hi Matthew ,

it dont work for me.

I have first referenced the sharepoint javascript libraries sp.core.js , sp.js and SP.UI.Dialog.js in a string variable with my CSS styles sheet.

Then i have added a onclick event on my <a> tag and set the href attribute to # like you have mentioned in your code.

When i click on the my link nothing happen and i dont see my sharepoint popup.

Javascript is activated in my IE navigator and all thing seem to be Ok.

How to get rid of this error;

This is my code in one line :

                                   

string url = web.Url + "/Lists/" + this.Text + "/DispForm.aspx?ID=" + item.ID;

str2 = string.Concat(new object[] { obj2, "<p>", this.FirstWords(this.StripTagsCharArray(item[this.BodyField].ToString()),Convert.ToInt32(this.NumWords)), "... <br/><a onclick=\"javascript:SP.UI.ModalDialog.OpenPopUpPage(url,null,600,400);return false;\"  href=\"#\">Lire l'article</a></p></div></li>" });

Thanks.

July 18th, 2013 2:59pm

Hi Lamaine,

You don't need to reference sp.js and sp.ui.dialog.js in your webpart. These will already be loaded by the masterpage.

Have you used the Internet Explorer developer tools (F12) to see if there is a JavaScript error happening when you click on the link?

Free Windows Admin Tool Kit Click here and download it now
July 19th, 2013 3:58pm

hi matthew ,

after using F12 in IE , i have noticed a javascript error " URL is indefined " when clicking on my <a> tag.

my url seem to be ok as  i mentionned in last reply. a solution for this ?

Thank you.

July 20th, 2013 12:29pm

Hi Lamine,

The url needs to be in quotes, because it's a string being passed to the function. Try this:

string url = web.Url + "/Lists/" + this.Text + "/DispForm.aspx?ID=" + item.ID;

str2 = string.Concat(new object[] { obj2, "<p>", this.FirstWords(this.StripTagsCharArray(item[this.BodyField].ToString()),Convert.ToInt32(this.NumWords)), "... <br/><a onclick=\"javascript:SP.UI.ModalDialog.OpenPopUpPage('url',null,600,400);return false;\"  href=\"#\">Lire l'article</a></p></div></li>" });

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2013 4:24pm

Hi Matthew ,

Now , i have my popup dialog window but nothing in it ( error HTTP 404 : NOT FOUND). I don't see my page opening in the popup window.

as you see , my code is OK and the url is OK it's  the url of the displayform of my item.

Any help will be apprecieted.

Thank you matthew





July 22nd, 2013 4:56am

HTTP 404 is a file not found message. Can you try using the following code to set the URL of the items display form:

var url = String.Format("{0}{1}?ID={2}", list.ParentWeb.Url, list.DefaultDisplayFormUrl, item.ID);
Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2013 6:50am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics