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>
Dmitry T
  • 385
  • 2
  • 4
  • 12

4 Answers4

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 }
Dmitry T
  • 385
  • 2
  • 4
  • 12
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");
Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Sunil
  • 1
-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

Pushpendra
  • 1,345
  • 9
  • 16
-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

Vladimir Oselsky
  • 201
  • 2
  • 11