Only a single button stands between two choices

Currently, I am facing an issue where I have to click twice to perform a search with the correct text. Is there a way for me to track what I'm sending and update it accordingly? Are there any alternative solutions available?

Greetings!

I have two radio buttons that determine which search engine to use (Google or my own), along with a button to submit a text for searching on one of the two engines. When attempting a Google search, the first click opens a new window, while only on the second click does it open the Google window with the desired search term. If I try to perform my search, it ends up executing both searches. What am I doing wrong here? How can I get this functionality to work correctly?

<table><tr>
  <td>
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Button1_Click" UseSubmitBehavior="true"/>
  </td>
  </tr>
  <tr align="center">
  <td>
      <input id="Search1" value="one" type="radio" runat="server" name="sitesearch" />Site Name
      <input id="Search2" value="two" type="radio" runat="server" name="sitesearch" />           
      <img src="http://www.google.com/images/poweredby_transparent/poweredby_FFFFFF.gif" alt="Google" />
  </td>      
  </tr>
</table>

Additionally...

protected void Button1_Click(object sender, EventArgs e){

        if (Search1.Checked)
        {
            //My Search
            Response.Redirect(Request.UrlReferrer.AbsolutePath +...
        }
        else if (Search2.Checked)
        {
            //Google search
            btnSearch.Attributes.Add("OnClick", "window.open('http://www.google.com/search?q=" + Regex.Replace(TextB.Text, " ", "+") + "','_blank');");
        }
    }

...and then we have...

TextBox TextB;    
HtmlInputRadioButton radioBSite;
Button btnSearch;
HtmlInputRadioButton radioBGoogle;

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    TextB = (TextBox)FindControl("TextBox1");
    TextB.Text = Request["find"];
    radioBSite = (HtmlInputRadioButton)FindControl("Search1");
    radioBSite.Checked = true;
    radioBGoogle = (HtmlInputRadioButton)FindControl("Search2");

    btnSearch = (Button)FindControl("btnSearch");
    btnSearch.Click += new EventHandler(Button1_Click);
    .
    .
}

Answer №1

The issue lies within the following line of code:

btnSearch.Attributes.Add("OnClick", "window.open('http://www.google.com/search?q=" + Regex.Replace(TextB.Text, " ", "+") + "','_blank');

You are inserting a client-side OnClick action that will open the search in a new window (using the _blank target).

If you were to use:

Response.Redirect("http://www.google.com/search?q=" + TextB.Text);

Everything would function as anticipated.

Answer №2

For a more user-friendly experience, opt for asp:RadioButtons instead of input controls. Set the autopost property to true and set up their events during design time.

Answer №3

UPDATE: Made some changes to the code in order to open the search window.

Below is a snippet that could guide you. It doesn't actually conduct a search, but it should give you a good idea. ASPX:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<div>
    <asp:TextBox ID="SearchFor" runat="server"></asp:TextBox><br />
    <asp:RadioButton ID="RadioGoogle" runat="server" GroupName="SearchSelect" Text="Google" />
    <asp:RadioButton ID="RadioCustom" runat="server" GroupName="SearchSelect" Text="Custom" /><br />
    <asp:Button ID="Search" runat="server" onclick="Search_Click" Text="Search" />
</div>    
</form>

Codebehind:

protected void Search_Click(object sender, EventArgs e)
{
if (RadioGoogle.Checked)
GoogleSearch(SearchFor.Text);
if (RadioCustom.Checked)
Response.Write("Search Custom for " + SearchFor.Text);
}
private void GoogleSearch(string searchFor)
{
string targetURL = "http://www.google.com/search?q=" + Regex.Replace(searchFor, " ", "+");
string clientScript = "window.open('" + targetURL + "');";
ClientScript.RegisterStartupScript(GetType(), "popup", clientScript, true);
}

Replace the Response.Write's with a call to a function that executes your search.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to observe a function and provide a simulated result from within a different function using jasmine?

Query: How can I access the reference of getWindowSize within getBreakpoint() to perform spying on it? Additionally, how can I use callFake to return mock data? media-query.ts export const widthBasedBreakpoints: Array<number> = [ 576, 768, 99 ...

Bootstrap modal assistance does not function properly when used together with ajax requests

I'm experiencing an issue with a small class project where the data is not being inserted into the database. I collect the data using a bootstrap modal and pass it to the php file using ajax. The problem seems to be occurring when executing the script ...

Bootstrap Tags Input is unable to function properly with data stored locally

I am currently working on developing a Project Manager tool that allows for the addition of multiple individuals to a single project. To accomplish this, I decided to incorporate the use of Bootstrap Tags Input by following the examples provided for Typeah ...

The ajax keypress event is malfunctioning and the ActionResult parameter is failing to capture any data

I am facing an issue where I want to update the value of a textbox using AJAX on keypress event, but the controller is not receiving any value to perform the calculation (it's receiving null). <script> $('#TotDiscnt').keypress(fu ...

Is there a way to update protractor.conf.js configurations using the command line?

Currently, I have set up Protractor to run on our integration server. In the protractor.conf.js file, I have configured the multiCapabilities as follows: multiCapabilities: [{ 'browserName': 'firefox', 'platform': &a ...

Problem with jQuery expanding the container when clicking "Read More"

I'm working on a script to collapse and expand a div based on click events. The code below is functioning correctly, but the container always starts off expanded when the page loads. I want to reverse this behavior so that the div is collapsed when th ...

Is the Three.JS camera update causing issues with the shader?

Attempting to replicate the Bubble shader from https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Bubble.html, but encountering issues due to outdated code. The example should refract whatever is behind it, like this: https://i.sstatic ...

Unable to retrieve any data from the input column

Utilizing C# and Selenium, I am attempting to extract the contents of rows, columns, and cells from a table. The table consists of two columns: Name and Favorite Color. While I can successfully retrieve the content under the Name column, I am encountering ...

Utilizing the request module in Node.js with ExpressJS

Currently, I am in the process of creating a helper method within my code that will handle user authorization. This function, named "usePermissions", is being developed following the React approach but for backend functionality. Upon implementa ...

Enhancing Page Content Dynamism: Making Elements Static

I have a div element that I want to align on the right side of the screen, but the problem is that it doesn't adjust its position as the content dynamically expands and exceeds the width of the page. Despite conducting extensive research, I haven&apos ...

Stop Chrome from automatically scrolling to the top of the page when making changes to the DOM

Currently utilizing Action Cable to create a discussion room where users can exchange questions. Upon posting a question, all participants within the room are supposed to see it. Nonetheless, I've encountered an odd issue specifically on Chrome: whene ...

Is it possible to bypass the fleet menu when it is displayed on the website while reading from a text.txt file?

After creating a text file for my automated C# scripts, I encountered an issue with the data format. The first row of the file has an empty field in the 3rd column which causes errors during script execution. Is there a way to handle this error and continu ...

Upgrade to V2.0: Substituting empty values with a specified string within a JSON formatted array

After submitting a form and receiving null values through JSON, I wanted to replace them with String values like 'n/a' or 'not specified'. Even though I followed @Winter Soldier's suggestion and added code to check for null values ...

Discovering the most recently selected element in jQuery

Currently reading a jQuery tutorial from the Head First series and practicing with an example where I need to identify the last selected element. In Chapter 2, there is a task involving four images on a page. When a user clicks on any of these images, the ...

Tips for extracting HTML entities from a string without altering the HTML tags

I need assistance with removing HTML tags from a string while preserving html entities like &nbps; & é < etc.. Currently, I am using the following method: stringWithTag = "<i> I want to keep my ->&nbsp;<- element space, bu ...

Guide to creating a clickable inline svg in HTML/CSS for use with onclick event in JavaScript

I have a unique inline svg code snippet in my html: <svg id="nav-search-icon" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32.2 32.2"><path d="M19 0C11.8 0 6 5.8 6 13 ...

How can we resolve a FOREIGN KEY constraint conflict in an UPDATE statement?

https://i.sstatic.net/66a0R.jpgDuring my code debugging process, I encountered an issue related to a SQL Server stored procedure. "The UPDATE statement conflicted with the FOREIGN KEY constraint \"FK_PATIENTS_DropdownCounty\". The conflict occ ...

Array filtering functions similarly to marketplace filtering tools

In order to make the filter function like a marketplace filter, I want to only see items related to the selected brand and status. For example: partners = [ 0:{ year: "2022" badge_status: "badge-success" sale_date: "01/07/2022&quo ...

Allowing only certain URLs to be opened in a new tab using regular expressions

I am currently developing a website that requires sanitizing the output from the database while allowing certain HTML tags. To achieve this, Regex is being used for data sanitization. Currently, the system permits standard Google (normal href with no ...

Having trouble with the "next" pagination button not loading cards, even though the numbered buttons are working perfectly

My pagination numbers are functioning correctly, but I'm having trouble with my "next" or "previous" buttons. I am using Bootstrap 5 and jQuery to load cards from an array. The issue seems to be with the currentPage variable in my displayList function ...