Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created custom list web part and using that web part in home page. when i click on item its not opening in dialog. I used below code.

Can anyone suggest me what is the problem ?

     SPQuery query = new SPQuery();
                                     query.Query = "<OrderBy><FieldRef Name='ID'     Ascending='False' /></OrderBy>";
                            query.RowLimit = Convert.ToUInt16(RowLimit);
                            SPList list = web.Lists[ListName];
                            SPListItemCollection listItemsCollection = list.GetItems(query);
                            foreach (SPListItem listItem in listItemsCollection)
                            {
                                TableRow tableRow = new TableRow();
                                TableCell tableCell = new TableCell();
                                HyperLink link = new HyperLink();

                                string docName = listItem.Title;
                                link.Text = docName;
                                try
                                {
                                   link.NavigateUrl = web.ParentWeb.Url  + list.DefaultDisplayFormUrl + "?id=" + listItem.ID + "&IsDlg=1";

                                }
                                catch (Exception ex)
                                {
                                    link.NavigateUrl = web.Url + list.DefaultDisplayFormUrl + "?id=" + listItem.ID ;
                                }

                                //link.Target = "_parent";
                                tableCell.Controls.Add(link);
                                tableRow.Cells.Add(tableCell);
                                table.Rows.Add(tableRow);
                            }
                            this.Controls.Add(table);
share|improve this question
    
Here is a similar question: stackoverflow.com/questions/10069102/… –  Chloraphil Mar 14 '13 at 12:02
add comment

1 Answer

When looking at the code I can't see anything that opens it in a dialog. Just ordinary links. Add "javascript:OpenDialog(here you puit that url) to link. Add this javascript as well:

The script can be found here: http://msdn.microsoft.com/en-us/library/ff410058(v=office.14).aspx

or here

function OpenDialog(url)
{

   //Using a generic object.
   var options = {
   title: "My Dialog Title",
   width: 400,
   height: 600,
   url: url };

    SP.UI.ModalDialog.showModalDialog(options);
}
share|improve this answer
    
Addition: showModalDialog appends IsDlg=1 to the link provided, so no need to include that in the url parameter. –  Jussi Palo Mar 14 '13 at 12:26
    
Thanks For ur reply. Can you tell me how to add and call javascript function in .ascx.cs file in .net 2012 –  janakiakula Mar 14 '13 at 12:28
    
You save the javascript in a js file which you deploy to either the hive or library. Then you reference it through the ClientScriptManager.RegisterClientScriptInclude. Info can be found at: msdn.microsoft.com/en-us/library/… –  Magnus Hansson Mar 14 '13 at 13:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.