2

I have a Knowledge Base template site that I'm trying to customize. I want to create a simple search site, with just two items on it:
- a dropdown menu which displays content of "Keywords" list (from the same site, using the list as its Data Source)
- a "Search" button, that will redirect me to the address:

http://server/site/_layouts/searchresults.aspx?k=[SelectedItem]

so that I can easily search by keywords.

I'm used to basic C#, so I was counting on something like "...search.aspx?k=" + dropdownList.SelectedItem.ToString(), but from what I found out it's not going to be that easy.

Somebody told me this can be done using "Content Editor" webpart, but I have no idea how to start.

Is there any way to do it, preferably without involving Visual Studio? I'm kind of used to Sharepoint Designer. :)

2 Answers 2

2

Using SharePoint designer you can create an HTML Button in the page and on the onClick event execute a JavaScript function that builds the URL by concatenating the site URL and the value of the dropdown menu (Get it using getElementById() and access it's value). Then you can use window.location.href = url to redirect to another page.

Example:

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
 function redirectToSearchPage()
  {
    var dropDown = document.getElementById('dropDownClientId');
    var itemText  = dropDown.options[dropDown.selectedIndex].text;
    window.location.href = "http://server/site/_layouts/searchresults.aspx?k=" + itemText;
  }
</script>
</asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">

    <asp:DropDownList runat="server" id="ddlKey" DataValueField="Title" DataTextField="Title" DataSourceID="spdatasource1" Width="165px"></asp:DropDownList>
    <input name="Button2" type="button" value="Search HTML" onclick="redirectToSearchPage()" />

</asp:Content>
13
  • Hi Moussa, thanks for the reply. Right now I'm trying to familiarize myself with basic JavaScript and get this selected value as string. It's not going well... Tried alert(document.getElementById("ddlKey")); and alert(document.getElementById('<%= ddlKey.ClientID %>'));, as suggested on some sites, but I keep getting a null object. Could you help me some more? :)
    – Piotr S.
    Dec 18, 2012 at 10:53
  • Ok. Can you post the code you used for creating the dropdown?
    – user3470
    Dec 18, 2012 at 10:57
  • <asp:DropDownList runat="server" id="ddlKey" DataValueField="Title" DataTextField="Title" DataSourceID="spdatasource1" Width="165px"></asp:DropDownList>
    – Piotr S.
    Dec 18, 2012 at 11:10
  • Try adding ClientIDMode="Static" to the dropdown and use document.getElementById("ddlKey").
    – user3470
    Dec 18, 2012 at 11:23
  • 1
    Ok the final solution is to browse to the SharePoint page and view the html source. locate the ddlKey dropdown and get its id. Use this id in JavaScript.
    – user3470
    Dec 18, 2012 at 12:53
0

Final solution, thanks to Moussa: onClick event:

var id = 'ctl00_m_g_f9ae17e4_a205_4c1c_88c1_300f89d385b3_ddlKey';
window.location = "http://server/site/_layouts/searchresults.aspx?k=" 
    + document.getElementById(id).options[document.getElementById(id).selectedIndex].text;`

All in one line. Element ID od ddlKey dropdown found in the webpage source.

2
  • I have added an example that can help you better organize your code.
    – user3470
    Dec 18, 2012 at 13:44
  • Nice, thanks. I thought I need to make it into one line. :)
    – Piotr S.
    Dec 18, 2012 at 13:46

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.