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 the following code for the javascript modal popup.

function ShowSearchBox() {
var options = {
    title: "Search Here",
    width: 760,
    height: 380,
    url: "/_layouts/CustomSearch/SearchPage/Searchpage.aspx"
};
SP.UI.ModalDialog.showModalDialog(options);

}

This is working fine. But now i want to send parameters to the url like, "/_layouts/CustomSearch/SearchPage/Searchpage.aspx?TableName=tblName &columnName=colName"

Here tblName and colName are not the static one. those are property values. We will set at the time of deploying the webpart. So these two values should be like dynamic parameters. we should insert the variables here instead of static values? How to pass here like this. I am calling ShowSearchBox() function in the '.js' file when ever a button is clicked like below. $("#btnShowDialog").click(function (e) { ShowSearchBox(); e.preventDefault(); });

How to achieve it? I searched but didn't find correct solution. This post is some what similar but i am not able to understand

share|improve this question
add comment

2 Answers 2

I think one solution for this is to do the following:

  1. Do not place the ShowSearchBox function in a separate .js file, but leave it in the aspx file.
  2. Add two parameters to the function, just to make it nicer: $("#btnShowDialog").click(function (e) { **ShowSearchBox(tableName,columnName)**; e.preventDefault(); });
  3. Add before the $(document).ready(...) part the following

    var tableName = '<%=this.TableName %>';
    var columnName = '<%=this.ColumnName %>';
    

    TableName and ColumnName are properties in the code behind of the aspx page. On their get {...} branch you can retrieve the values dynamically. Then the values are insterted into the generated HTML and if you do a view source of the page in a browser, you will see something like this

    var tableName = 'table1';
    var columnName = 'column1';
    

    You can use these global javascript variables in the rest of your javascript code.

I hope it helped.

share|improve this answer
    
Yeah I also tried with as this. –  Mihir Nov 30 '12 at 5:17
    
And, didn't it solve your problem? If not, please tell me why not, so maybe I can help further. –  Norbert Nov 30 '12 at 9:37
add comment

I'm confused. Why don't you do it exactly like you said?

function ShowSearchBox(var) {
    var options = {
        title: "Search Here",
        width: 760,
        height: 380,
        url: "/_layouts/CustomSearch/SearchPage/Searchpage.aspx?Foo=" + var
    }
    SP.UI.ModalDialog.showModalDialog(options);
}

$("#btnShowDialog").click(function (e) { ShowSearchBox('myValue'); e.preventDefault(); });

On searchpage.aspx, you can get the variable easily with Request.QueryString:

string foo = (string)Request.QueryString["Foo"]
share|improve this answer
    
In the example you given the parameter myvalue is static one. But This should not be static. It is dynamic value. Thats why I struck. I clearly mentioned that those are property values in C#. I know how to achieve this with static values brother. –  Mihir Nov 30 '12 at 5:16
    
But you can fill in those values from your webpart. When you are creating your button, you can set the OnClientClick property and reference your property there. If the button isn't your own button and you can't set the property, you can still pass any variable you want to your own javascript function. For example, in the rendercontents method: html.Write("<script>$(document).ready(function(){MyFunction(\"" + myValue + "\");});</script>"); Then you can attach the click event from inside your javascript file, using your own value. –  Spongeroberto Squarepantalones Nov 30 '12 at 7:53
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.