none
LinkButton OnClientClick Event calling a javaScript Function to open ModalDialog

    Question

  • I am using an ASP.NET repeater control in which i have added 2 controls (LinkButton and Label)

    I am binding the LinkButton and Label with Title and URL of a SharePoint ListItem respectively.

    OnClick of the LinkButton should open the Display Form(DispForm.aspx) of the ListItem in a ModalDialog with specific Height and Width.

    But when i click on the LinkButton, the ModalDialog flashes and disappears in about 5seconds.

    Here's the code i have written.

    (.ascx Code)

    <script type="text/javascript">

        function demoCallback(dialogResult, returnValue)

        {

            SP.UI.Notify.addNotification('Operation Successful!');

            SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);

        }

        function openDialog(title1, url1) {

            var options = {

                title: "Announcements - " + title1,    

                width: 600,

                height: 400,

                url: url1,

                dialogReturnValueCallback: demoCallback

            };

            SP.UI.ModalDialog.showModalDialog(options);

            //return false;

        }  

    </script>

    <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater_dataBound">

        <ItemTemplate>      

            <table>         

               <tr>

                   <td>

                        <li>

                        </li>

                   </td>  

                   <td>

                        <asp:LinkButton ID="lnkBtnTitle" runat="server" Style="position: relative" Text='<%# Eval("Title") %>' />            

                    </td>

                   <td>

                       <asp:Label ID="lblUrl" runat="server" Visible="False" Text='<%# Eval("Url") %>'></asp:Label>

                   </td>

               </tr>

            </table>   

        </ItemTemplate>

    </asp:Repeater>

    (.ascx.cs Code)

            protected void Page_Load(object sender, EventArgs e)

            {

                SPWeb web = SPContext.Current.Web;

                SPList list = web.Lists["Announcements"];

                DataTable dt1 = new DataTable();

                SPQuery query = new SPQuery();

                query.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>";

                query.RowLimit = 5;

               

                SPListItemCollection items = list.GetItems(query);

                try

                {          

                    DataColumn dc;

                    DataColumn dc1;

                    dc = new DataColumn("Title", Type.GetType("System.String"));

                    dc1 = new DataColumn("Url", Type.GetType("System.String"));

                    dt1.Columns.Add(dc);

                    dt1.Columns.Add(dc1);

                    string title = string.Empty;

                    string url = string.Empty;

                    DataRow dr;

                    foreach (SPListItem item in items)

                    {

                        title = item["Title"].ToString();

                        url = web.Url + "/Lists/" + list.Title + "/DispForm.aspx?ID=" + item.ID;

                      

                        dr = dt1.NewRow();

                        //dr["Title"] = "<a href=" + url + ">" + title + "</a>";

                        dr["Title"] = title;

                        dr["Url"] = url;

                        dt1.Rows.Add(dr);

                    }            

                    Repeater2.DataSource = dt1;

                    Repeater2.DataBind();            

                }

                catch (Exception Ex)

                {

                    throw Ex;

                }

            }

            protected void Repeater_dataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)

            {        

                LinkButton lnkBtn = (LinkButton)e.Item.FindControl("lnkBtnTitle");

                Label lbl = (Label)e.Item.FindControl("lblUrl");

                lnkBtn.OnClientClick = string.Format("openDialog('{0}','{1}');", Convert.ToString(lnkBtn.Text), Convert.ToString(lbl.Text));

            }

    When i check the LinkButton properties on my site, the Address URL is shown as

    javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl33$g_b3b02385_9300_490d_b08c_1793781f2cc4$Repeater2$ctl00$lnkBtnTitle", "", true, "", "", false, true))

    Please suggest a solution thru which i can open the Form in Modal Dialog

    Thursday, July 11, 2013 10:04 AM

Answers

  • Hi S.Narayan,

    As you described,the dialog showde and disappered.

    Does the founction  demoCallback cause it?

    You May change the founction

    function demoCallback(dialogResult, returnValue)
        {
            SP.UI.Notify.addNotification('Operation Successful!');
            SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
        }

    to this below

    function demoCallback()
        {
        SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 'Operation Successful!');
        }

    to test.

    Hope it can help you.


    IE show the page

    Thursday, July 11, 2013 2:55 PM