0

I have a grid where you can select items, a drop down and a button. You can select an action from the dropdown and the click the button to open the modal dialog. I need to put the id of the selected items in the Session first (IMPORTANT!), and then I need to open a Modal Dialog from code behind. I have tried a few solutions but the client code always get executed before the server code.

Thanks in advance,

UPDATE

This is the code in the aspx page

<script language="javascript" type="text/javascript">
function ShowLightBox(pageUrl, lightBoxTitle, moreOptions) {
    options = {
        url: pageUrl,
        autoSize: true,
        title: lightBoxTitle
    }
    SP.UI.ModalDialog.showModalDialog(options);
    yPos = $(window.parent.document).scrollTop() + 20;
    $(".ms-dlgContent", window.parent.document).css({ position: 'fixed', top: yPos });
}
</script>


<telerik:RadComboBox ID="rcActions" runat="server" 
            DataTextField="Key" DataValueField="Value">
        </telerik:RadComboBox>
        <asp:Button ID="btnGo" runat="server" Text="GO" OnClick="btnGo_OnClick" />

    <telerik:RadGrid ID="radGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnNeedDataSource="radGrid1_OnNeedDataSource"    OnItemCommand="radGrid1_ItemCommand" OnColumnCreated="radGrid1_OnColumnCreated"
        OnItemDataBound="radGrid1_OnItemDataBound">
        <MasterTableView AutoGenerateColumns="false" NoMasterRecordsText="<%$Resources:GlobalResource, noRecords%>">
            <Columns>
                <telerik:GridBoundColumn DataField="Id" UniqueName="Id" Visible="false">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="UnitNumber" UniqueName="UnitNumber" Visible="false">
                </telerik:GridBoundColumn>
                <telerik:GridClientSelectColumn UniqueName="Selected">
                </telerik:GridClientSelectColumn>
            </Columns>
            <NestedViewSettings>
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="Id" MasterKeyField="Id" />
                </ParentTableRelation>
            </NestedViewSettings>
            <NestedViewTemplate>
                <asp:Panel runat="server" Visible="false" ID="InnerContainer">
                   <!-- Some code -->
                </asp:Panel>
            </NestedViewTemplate>
        </MasterTableView>
    </telerik:RadGrid>

This is the code behind

    protected void btnGo_OnClick(object sender, EventArgs e)
        {
            try
            {
                var unitNumbers = new List<string>();
                foreach (GridItem unit in radGrid1.SelectedItems)
                {
                     var gridInfo = new GridInfo(unit);
                     unitNumbers.Add(gridInfo.UnitNumberCell.Text);
                }
                if (Session["UnitNumbers"] != null)
                     Session.Add("UnitNumbers", unitNumbers);
                 else
                    Session["UnitNumbers"] = unitNumbers;

                  // Here is where I want to put the code to open the lightbox, instead of       redirecting.
                  Response.Redirect("../Pages/MaintenanceDetails.aspx");
            }
            catch (Exception ex)
            {

            }
        }

I have tried using ScriptManager.RegisterStartupScript(GetType(),"hwa","ShowLightBox(param);",true); and it does not find the function.

Any other suggestion?

4
  • can you show us some code to see where abouts your going wrong? could be the event that your putting your code into!
    – Ali Jafer
    Commented Oct 12, 2012 at 14:24
  • I have updated my post based on our suggestion. Please read it and let me know.
    – L C
    Commented Oct 15, 2012 at 13:23
  • What is your functional requirement? Cause I've got a suspicion that you're not solving it in a correct way. Mixing up server side codebehind and client side scripting is more often a recipe for trouble than a good solution. Do you need the unit numbers session var in your popup by any chance?
    – Jasper
    Commented Oct 15, 2012 at 13:55
  • Yes, absolutely. Per requirements, I need to have the unit numbers in session before showing the pop up window.
    – L C
    Commented Oct 15, 2012 at 15:57

3 Answers 3

0

I had a similar issue and resolved it by moving the showModalDialog function call inside of ExecuteOrDelayUntilScriptLoaded:

ExecuteOrDelayUntilScriptLoaded(function () {
    SP.UI.ModalDialog.showModalDialog(options);
    ...
}, "sp.js")
0

If you are hooking up the modal using jquery in $(document).ready it will fire before the postback occurs. Try to not-hook that specific set of buttons, and call the dialog opener function from the code behind using ScriptManager.RegisterClientScript.

For example: https://stackoverflow.com/questions/11425887/open-showmodaldialog-of-javascript-from-asp-net-code-behind (note the use of the deprecated ClientScript, you should now use ScriptManager).

https://stackoverflow.com/questions/9538600/how-to-display-jquery-dialog-from-codebehind

If this doesn't work, please give more details on what kind of dialog are you using.

1
  • Thanks for your answer. I have tried ScriptManager.RegisterStartupScript and didn't work. I have updated my post with more details.
    – L C
    Commented Oct 15, 2012 at 13:24
0

try the following:

        //make modal dialog script.  
        string script =  
            @"  
var modalDialog;  
function ShowDialogTest() {  
    var options = {  
        url: '/Pages/MaintenanceDetails.aspx',  
        tite: 'Random Letter Details',  
        allowMaximize: false,  
        showClose: false,  
        width: 800,  
        height: 600 };  
    modalDialog = SP.UI.ModalDialog.showModalDialog(options);  
}";  
        //register the script.  
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), ClientID, script, true);  

so it should look somthing like this:

    protected void btnGo_OnClick(object sender, EventArgs e)        
    {        
        try        
        {        
            var unitNumbers = new List<string>();        
            foreach (GridItem unit in radGrid1.SelectedItems)        
            {        
                 var gridInfo = new GridInfo(unit);        
                 unitNumbers.Add(gridInfo.UnitNumberCell.Text);        
            }        
            if (Session["UnitNumbers"] != null)        
                 Session.Add("UnitNumbers", unitNumbers);        
             else        
                Session["UnitNumbers"] = unitNumbers; 

             ClientScript.RegisterStartupScript(this.Page, Page.GetType(), "ShowDialogTest()",true); 

    }        
    catch (Exception ex)        
    {        

    }        
}   

    protected void Page_Load(object sender, EventArgs e)   
    { 
             //make modal dialog script.  
             string script =  
              @"  
              var modalDialog;  
              function ShowDialogTest() {  
              var options = {  
              url: '/Pages/MaintenanceDetails.aspx',  
              tite: 'Random Letter Details',  
              allowMaximize: false,  
              showClose: false,  
              width: 800,  
              height: 600 };  
              modalDialog = SP.UI.ModalDialog.showModalDialog(options);  
              }";  

        //register the script.  
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), ClientID, script, true);
  }

You need to register the script first on the onload event and then call the function with the button onclick event :)

also have a look at this, http://sharepointdevelopertopics.blogspot.co.uk/2011/04/using-sharepoint-2010-modal-dialog-in.html

hope it helps!

3
  • It didn't work either. I'm getting 'Object expected' error.
    – L C
    Commented Oct 15, 2012 at 17:55
  • where is the error comming from? im presuming javascript error on webbrowser and not when you debug your code? if its javascript then you might need to tweek it abit to get it working but it indicates that its being called and you only need to change it slightly to get it working :)
    – Ali Jafer
    Commented Oct 16, 2012 at 8:07
  • also take a look at this its probably better than mine as that was off the top of me head without testing. If its calling the code fine than the example below should work for you :) sharepoint.stackexchange.com/questions/33086/…
    – Ali Jafer
    Commented Oct 16, 2012 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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