locked
Modify Parameters in a Navigation or DataNavigation URL using Javascript RRS feed

  • Question

  • User417723577 posted

    I am attempting to pass two parameters to another page with either a navigation or datanavigation url of fields from two separate datasources. The one url is relatively easy (see PID) below as it is attached to Detailview associated with Hyperlink or Hyperlinkfield. The second one is harder &DId=MDAir. After some difficulty I am able to get dynamic data from second datasource via getelementbyId but....

    I thought now I could replace string in navigation url with correct data to pass along in url paramenter to another page - trying not to use sessions/cookies.

    What I have found is you cannot call a Function while trying to modify navigation Url or datanavigation url string. Here is code which works with exception always get same DId=MDAir. Will not allow a function call where page/paramenters is presented - I have tried to use Function for both full or partial string with no luck.

    <asp:TemplateField HeaderText="Choose Now">
        <ItemTemplate>
         <asp:HyperLink id="HyperLink1" runat="server" NavigateUrl='<%# Eval("ProductId", "ShopGatewayNoDiv.aspx?PId={0}&Did=MDAir") %>' Text='Select Pet'></asp:HyperLink>
        </ItemTemplate>
       </asp:TemplateField>

    Note: I get First one (PId) from ProductId in Datasource but 2nd parameter is from different datasource and plan was to use function to placve appropriate design in DId (e.g. MDAir)

    Does anyone know how to get around this glitch.

    Thanking you in advance for assistance.

     

    Wednesday, August 17, 2011 7:05 PM

Answers

  • User2131854062 posted

    A basic implementation which you'll need to adapt would look something like this:

    function insertParam(key, value)
    {
        key
    = escape(key); value = escape(value);

       
    var kvp = document.location.search.substr(1).split('&');

       
    var i=kvp.length; var x; while(i--)
       
    {
            x
    = kvp[i].split('=');

           
    if (x[0]==key)
           
    {
                    x
    [1] = value;
                    kvp
    [i] = x.join('=');
                   
    break;
           
    }
       
    }

       
    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

       
    //this will reload the page, it's likely better to store this until finished
        document
    .location.search = kvp.join('&');
    }

    This is approximately twice as fast as a regex or search based solution, but that depends completely on the length of the querystring and the index of any match


    the slow regex method I benchmarked against for completions sake (approx +150% slower)

    function insertParam2(key,value)
    {
        key
    = escape(key); value = escape(value);

       
    var s = document.location.search;
       
    var kvp = key+"="+value;

       
    var r = new RegExp("(&|\\?)"+key+"=[^\&]*");

        s
    = s.replace(r,"$1"+kvp);

       
    if(!RegExp.$1) {s += (s.length>0 ? '&' : '?') + kvp;};

       
    //again, do what you will here
        document
    .location.search = s;
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 18, 2011 1:53 AM
  • User3866881 posted

    Hello again, try this plz——

      <a href='<%# string.Format("ShopGatewayNoDiv.aspx?PId={0}&Did={1}",Eval("ProductId"),"XXXX") %>'>Select Path Here</a>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 8:52 PM

All replies

  • User2131854062 posted

    A basic implementation which you'll need to adapt would look something like this:

    function insertParam(key, value)
    {
        key
    = escape(key); value = escape(value);

       
    var kvp = document.location.search.substr(1).split('&');

       
    var i=kvp.length; var x; while(i--)
       
    {
            x
    = kvp[i].split('=');

           
    if (x[0]==key)
           
    {
                    x
    [1] = value;
                    kvp
    [i] = x.join('=');
                   
    break;
           
    }
       
    }

       
    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

       
    //this will reload the page, it's likely better to store this until finished
        document
    .location.search = kvp.join('&');
    }

    This is approximately twice as fast as a regex or search based solution, but that depends completely on the length of the querystring and the index of any match


    the slow regex method I benchmarked against for completions sake (approx +150% slower)

    function insertParam2(key,value)
    {
        key
    = escape(key); value = escape(value);

       
    var s = document.location.search;
       
    var kvp = key+"="+value;

       
    var r = new RegExp("(&|\\?)"+key+"=[^\&]*");

        s
    = s.replace(r,"$1"+kvp);

       
    if(!RegExp.$1) {s += (s.length>0 ? '&' : '?') + kvp;};

       
    //again, do what you will here
        document
    .location.search = s;
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 18, 2011 1:53 AM
  • User3866881 posted

    Hello Macpack:)

    have a try with the common HTML tag instead of HyperLink, this will do the work.

    <asp:TemplateField HeaderText="Choose Now">
        <ItemTemplate>
         <a href='<%# Eval("ProductId", "ShopGatewayNoDiv.aspx?PId={0}&Did=MDAir") %>'>Select Path Here</a>
        </ItemTemplate>
       </asp:TemplateField>

    Friday, August 19, 2011 9:12 PM
  • User417723577 posted

    Thanks for help.

    This works same as Hyperlink. Compiler just does not like you messing around with Parameter list in URL from field in second datasource. I am attempting to have data that shows up as second parameter created by a Function. Initial record just happens to be "MDAir" but goal is to  dynamically create the Design Id (DId) at run time with a user selected design.(one of 300).

    Monday, August 22, 2011 7:06 PM
  • User417723577 posted

    Thanks for help.

    Compiler does not want to have a function attempting to Modify URL parameters. e.g. DId=MDAir is second parameter with 'MDAir' just being one of 300 possible values that is selected on a previous page by user.

    It would seem that calling this function from within URL string will get same basic compilation error problem???

    Monday, August 22, 2011 7:19 PM
  • User3866881 posted

    Hello again, try this plz——

      <a href='<%# string.Format("ShopGatewayNoDiv.aspx?PId={0}&Did={1}",Eval("ProductId"),"XXXX") %>'>Select Path Here</a>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 8:52 PM