I was working on a project for a great client the other day, using the Data View Web Part (DVWP) and I was having problems with web part connections between the Query String (URL) Filter and two of the DVWPs. Really, the connections were working, but I needed to add a “custom” hyperlink for editing the displayed item. There were real, actual, business requirements that wouldn’t allow me to use the out-of-the-box functionality within the DVWP.
Because we were on a tight schedule, I decided to go looking for a different way to gather the query string values using JavaScript.
I wish I could provide a link to the original source for this JavaScript code, but I’m just not having luck in finding the original source. If this is your content, please comment on this blog entry and I’ll update immediately!
Here’s the JavaScript:
| <script language="javascript" type="text/javascript"> <!-- function PageQuery(q) { if (q.length > 1) this.q = q.substring(1, q.length); else this.q = null; this.keyValuePairs = new Array(); if(q) { for(var i=0; i < this.q.split("&").length; i++) { this.keyValuePairs[i] = this.q.split("&")[i]; } } this.getKeyValuePairs = function() { return this.keyValuePairs; } this.getValue = function(s) { for(var j=0; j < this.keyValuePairs.length; j++) { if(this.keyValuePairs[j].split("=")[0] == s) return this.keyValuePairs[j].split("=")[1]; } return false; } this.getParameters = function() { var a = new Array(this.getLength()); for(var j=0; j < this.keyValuePairs.length; j++) { a[j] = this.keyValuePairs[j].split("=")[0]; } return a; } this.getLength = function() { return this.keyValuePairs.length; } } function queryString(key) { var page = new PageQuery(window.location.search); return unescape(page.getValue(key)); } // --> </script> |
Next, by using a Content Editor Web Part, I included the following HTML and JavaScript:
| <hr size="1" style="height: 1px" class="style2" noshade="" /> <table border="0" width="100%"> <tr> <td class="ms-vb" align="right"> <script language="javascript" type="text/javascript"> <!-- document.write("<strong><a href=\"EditForm.aspx?ID="); document.write(queryString('ID')); document.write("&ProjectID="); document.write(queryString('ProjectID')); document.write("&Source="); document.write(escape(window.location)); document.write("\">Edit</a></strong>  "); // --> </script> </td> </tr> </table> |
Put it all together (or change for your particular business needs) and enjoy. I hope this helps.
Mike