2

I have a code on a visual web part user control (see below): Could you please advice, how to get value from the callback function back to the C# in code behind to process it?

<script type="text/javascript">
    function portal_BaseCallback(result, value) {
        if (result === SP.UI.DialogResult.OK) {
        // here I need to transfer value to C# code behind to process
        }
        if (result === SP.UI.DialogResult.cancel) {
        //user press Cancel, ignore it
        }
    }
    function GetBaseOptions(m_title, m_url, width, height) {
        var options = {
            url: m_url,
            width: width,
            height: height,
            title: m_title,
            dialogReturnValueCallback: Function.createDelegate(null,     portal_BaseCallback)
        };
        return options;
    }
    function openDialog(options) {
        SP.UI.ModalDialog.showModalDialog(options);
    }
</script>
<asp:UpdatePanel runat="server" ID="MyUpdatePanel">
<ContentTemplate>
 <asp:LinkButton runat="server" Id="Link01" Text="Write a letter" 
    OnClientClick="openDialog(GetBaseOptions('Write a    message','/_layouts/htmledit.aspx',1024,800));" 
    onclick="Link01_Click" />
</ContentTemplate>
</asp:UpdatePanel>
2

I have managed to do what I need using hidden field.

<script type="text/javascript">
    function portal_BaseCallback(result, value) {
        var hidden;
        if (result === SP.UI.DialogResult.OK) {
            // here I need to transfer value to C# code behind to process
            hidden = document.getElementById('<%= this.Hidden1.ClientID %>');
            if (hidden != null) {
                hidden.value = value;
                __doPostBack('<%= this.MyUpdatePanel.ClientID %>', "cmdSend");
            }
        }
        if (result === SP.UI.DialogResult.cancel) {
            hidden = document.getElementById('<%= this.Hidden1.ClientID %>');
            if (hidden != null) {
                hidden.value = '';
            }
            var notifyId = SP.UI.Notify.addNotification("Message cancelled", false);
        }
    }
</script>

<asp:UpdatePanel runat="server" ID="MyUpdatePanel" UpdateMode="Conditional">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Hidden1" />
</Triggers>
<ContentTemplate>
<asp:HiddenField runat="server" id="Hidden1" onvaluechanged="Unnamed1_ValueChanged"/>
<asp:HiddenField runat="server" id="Hidden2" />
 <asp:LinkButton runat="server" Id="Link01" 
        Text="Test me i'm a link" 
        OnClientClick="openDialog(GetBaseOptions('Write a message','/_layouts/htmledit.aspx',1024,800));" 
  />
</ContentTemplate>
</asp:UpdatePanel>

Code behind

protected void Unnamed1_ValueChanged(object sender, EventArgs e)
{ //use Hidden1.Value }
0

This will help you to load required JS.

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { alert("Hii") });
SP.SOD.executeOrDelayUntilScriptLoaded(function () { alert("Hello") },"sp.js");
-1

It should be as easy as to modify your function from

function openDialog(options) 
{
    SP.UI.ModalDialog.showModalDialog(options);
}

to

function openDialog(options) 
{
    var value = SP.UI.ModalDialog.showModalDialog(options);
}

http://msdn.microsoft.com/en-us/library/ff410058(v=office.14).aspx

  • Which has nothing to do with c#, the real problem here – Robert Lindgren Jul 16 '13 at 15:34
  • As I understend, var value will be in this case a object of type SP.UI.ModalDialog, and it doesn't help to get returned value from callback function to the c#. – Dmitry T Jul 17 '13 at 12:57
-1

To pass a value back to the parent page use SP.UI.ModalDialog.commonModalDialogClose.

It will close the dialog with the result like “OK”, “Cancel” and can pass aditional information.

Ex:

SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, "success");

function OnPopClosed(dialogResult, retValue) {

    if (dialogResult == SP.UI.DialogResult.OK) {
        if (retValue)
            alert(retValue);
        }
}

http://sptalks.wordpress.com/2013/03/12/sharepoint-modal-dialog-passing-values-back-and-forth/

http://chakkaradeep.com/index.php/using-the-sharepoint-2010-modal-dialog

SharePoint Modal Help - pass value back to parent when closing

  • Which has nothing to do with c#, the real problem here – Robert Lindgren Jul 16 '13 at 15:33
  • If you want in C#, then one hack is there. When the Dialog window get closed. Redirect it to the parent with your info imbedded as QueryString. Back to the parent page just check for the presence of QueryString. – Pushpendra Jul 16 '13 at 15:41
  • The problem is I need to get this value in code behind, not in the javascript. – Dmitry T Jul 17 '13 at 12:55
  • Yes..if you pass the values in QueryString, you can read it anywhere..js file or cs file. – Pushpendra Jul 17 '13 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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