Saturday, August 10, 2019

How to implement google search box in asp.net

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Webpage and Design for Show Google Search Result.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.



Step-3: Sign up & Setup your Search Engine for Google Search.

Go to https://www.google.com/cse/all > Sign up for Google Search > Setup your Search Engine for Google Search.

Step-4: Place Google Search Code to the Search Result Page (Where you want to show your result).

Open Search Result Page and place Google Search Code in your page where you want to show google search result.
HTML Code 
  1.  
  2. <%-- Here I will Add code for show search result --%>
  3. <script>
  4. (function () {
  5. var cx = '002672153316751420378:ho6psrycb1e';
  6. var gcse = document.createElement('script');
  7. gcse.type = 'text/javascript';
  8. gcse.async = true;
  9. gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
  10. '//www.google.com/cse/cse.js?cx=' + cx;
  11. var s = document.getElementsByTagName('script')[0];
  12. s.parentNode.insertBefore(gcse, s);
  13. })();
  14. </script>
  15. <gcse:searchresults-only></gcse:searchresults-only>
  16.  
  17.  
  18.  

Step-5: Design & Write Code to your master page for Place Search Box & Implement Search.

Complete HTML Code for Master Page
  1.  
  2.  
  3. <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="ASPGoogleSearch.SiteMaster" %>
  4.  
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head runat="server">
  8. <meta charset="utf-8" />
  9. <title><%: Page.Title %> - My ASP.NET Application</title>
  10. <link href="~/Content/Site.css" rel="stylesheet" />
  11. <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
  12. <asp:PlaceHolder runat="server">
  13. <script src="<%: ResolveUrl("~/Scripts/modernizr-2.5.3.js") %>"></script>
  14. </asp:PlaceHolder>
  15. <meta name="viewport" content="width=device-width" />
  16. <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
  17. <script>
  18. function SubmitOnEnter(searchBox, event) {
  19. var keyCode;
  20. if (window.event) {
  21. keyCode = window.event.keyCode;
  22. }
  23. else if(event) {
  24. keyCode = event.which;
  25. }
  26. else {
  27. return true;
  28. }
  29. if (keyCode == 13) {
  30. // This is for Enter Key
  31. SiteSearch();
  32. return false;
  33. }
  34. else {
  35. return true;
  36. }
  37. }
  38.  
  39. function SiteSearch() {
  40. document.location.href = "/SearchResult.aspx?q=" +
  41. EncodeText(document.getElementById('q').value); // Here we should use url encode for the user input
  42. }
  43.  
  44. function EncodeText(value) {
  45. var returnValue = "";
  46. var x = 0;
  47. var regex = /(^[a-zA-Z0-9_.]*)/
  48. while (x < value.toString().length) {
  49. var match = regex.exec(value.substr(x));
  50. if (match != null && match.length > 1 && match[1] != '') {
  51. returnValue += match[1];
  52. x += match[1].length;
  53. }
  54. else {
  55. if (value[x] == ' ') {
  56. returnValue += '+';
  57.  
  58. }
  59. else {
  60. var charCode = value.charCodeAt(x);
  61. var haxValue = charCode.toString(16);
  62. returnValue += "%" + (haxValue.length < 2? '0':'') + haxValue.toUpperCase();
  63. }
  64. x++;
  65. }
  66. }
  67. return returnValue;
  68. }
  69. </script>
  70. </head>
  71. <body>
  72. <form runat="server">
  73. <asp:ScriptManager runat="server">
  74. <Scripts>
  75. <asp:ScriptReference Name="jquery" />
  76. <asp:ScriptReference Name="jquery.ui.combined" />
  77. </Scripts>
  78. </asp:ScriptManager>
  79. <header>
  80. <div class="content-wrapper">
  81. <div class="float-left">
  82. <p class="site-title"><a runat="server" href="~/">your logo here</a></p>
  83. </div>
  84. <div class="float-right">
  85. <%-- Here I am going to add Our search control in the Master Page --%>
  86. <div style="width:300px; text-align:right; margin-bottom:10px;">
  87. <input type="text" value="Search" style="width:200px;"
  88. id="q" name="q" onblur="if(this.value == '') this.value=this.defaultValue;"
  89. onfocus="if(this.value == this.defaultValue) this.value = '';"
  90. onkeypress="return SubmitOnEnter(this, event);" />
  91. <input type="button" value="Search" onclick="SiteSearch();" style="padding:4px; margin:0px;" />
  92. </div>
  93. <section id="login">
  94. <asp:LoginView runat="server" ViewStateMode="Disabled">
  95. <AnonymousTemplate>
  96. <ul>
  97. <li><a id="registerLink" runat="server" href="~/Account/Register.aspx">Register</a></li>
  98. <li><a id="loginLink" runat="server" href="~/Account/Login.aspx">Log in</a></li>
  99. </ul>
  100. </AnonymousTemplate>
  101. <LoggedInTemplate>
  102. <p>
  103. Hello, <a runat="server" class="username" href="~/Account/Manage.aspx" title="Manage your account">
  104. <asp:LoginName runat="server" CssClass="username" />
  105. </a>!
  106. <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" />
  107. </p>
  108. </LoggedInTemplate>
  109. </asp:LoginView>
  110. </section>
  111. <nav>
  112. <ul id="menu">
  113. <li><a runat="server" href="~/">Home</a></li>
  114. <li><a runat="server" href="~/About.aspx">About</a></li>
  115. <li><a runat="server" href="~/Contact.aspx">Contact</a></li>
  116. </ul>
  117. </nav>
  118. </div>
  119. </div>
  120. </header>
  121. <div id="body">
  122. <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
  123. <section class="content-wrapper main-content clear-fix">
  124. <asp:ContentPlaceHolder runat="server" ID="MainContent" />
  125. </section>
  126. </div>
  127. <footer>
  128. <div class="content-wrapper">
  129. <div class="float-left">
  130. <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
  131. </div>
  132. </div>
  133. </footer>
  134. </form>
  135. </body>
  136. </html>

Step-6: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...