views:

207

answers:

3

My problem now is that i need to click 2 time to do the search with right text to search. is there a way to cacth what i'm sending and update it? Is there another solution?

Hello!!

I have 2 radiobuttons that decides what search engine to use (google or mine), and i have 1 button that will submit a text that will be used to be searched in one of the 2 search engines. When i try to do the google search, the first time it just opens the new window when i click the 2nd time, it opens the google window with word that i want to search and if i try to do my search it do the both searchs. What i'm doing wrong ? how i put this working in the right way?

  <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>

and then i have...

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 i have this...

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);
    .
    .
}
A: 

instead of input controls use asp:RadioButton with autopost property as true and create and assign their events at design time

scorpio
But then i wont need the button right? i want to search only when i click the button not when i select the radiobutton. isnt what would happen if i do that?
SlimBoy
A: 

EDIT: Updated the code to open the search window.

The following may help you. It's not performing a search, but you should be able to get the general 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);
        }

Swap the Response.Write's for a call to a method that does your search.

Chuck
any reason why the line ClientScript.RegisterStartupScript(GetType(), "popup", clientScript, true);is giving me a error "no overload for method 'RegisterStartupScript' takes '4' arguments"when i try to search for 'RegisterStartupScript' in ClientScript there's nothing... what should i do?
SlimBoy
If i do "Page.ClientScript.RegisterStartupScript(..."is the same thing? If is the same it won't work in my project
SlimBoy
it runs the code but it doesnt open a new window
SlimBoy
Page. is the same thing. I have a using statement that negates the need to prefix the call with Page. As to not opening the new window - are you using a pop-up blocker? It worked fine when I ran the test in Chrome.
Chuck
No, i'm not using a pop-up blocker. May __dopostback help in this case? Since none of this is doing what i want to?
SlimBoy
I just retested the code above in Chrome, IE, and FF and in all three the google pop-up opened with the search results (after I allowed pop-ups). There is no other code in the test file I created besides what I have pasted above. Have you tried a brand new, blank aspx page?
Chuck
Yes in a brand new, blank aspx page it works, but in my project it dont work, i have tried other solutions that work in a new project but not in mine.
SlimBoy
Like this example:<html><head><script type= "text/javascript" language="javascript">function abreJanela() {var object = document.getElementById('pg');var tb = document.getElementById('tb');if (tb.value == "") {alert("Nothing to search");}else {if (object.checked) {window.open("http://www.google.com/search?q=" + tb.value.replace(" ", "+") + "", '_newtab');} else {alert(tb.value);}}return;}</script></head>
SlimBoy
<FORM name="myForm" action="#"><table><tr><td> <input ID="tb" name="TextBox1" type="text" Columns="25" MaxLength="255" /> <input type="submit" value="btnSearch" onClick="abreJanela()"/></td></tr><tr align="center"><td><input id="pi" type="radio" name="PesqInterna" value="um" />Nome do Site<input id="pg" type="radio" name="PesqGoogle" value="dois" /><img src="http://www.google.com/images/poweredby_transparent/poweredby_FFFFFF.gif" alt="Google" /></td></tr></form></table></body></html>
SlimBoy
May __dopostback help in this case? Since none of this solutions are doing what i want to? Someone have a example that can be apply to this problem for me to see?
SlimBoy
+1  A: 

The problem is with this line of code:

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

You are adding a client side OnClick, that will open the search in a new window (the _blank target).

If you do a:

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

Things will work as you expect.

Oded
This works! But just one more detail.... and if i want to open in a new window what i have to do?
SlimBoy
You say you want to open in a new page - both options, or just one of them?
Oded
On button click open a new browser window with google search
SlimBoy
One option would be to output javascript to the page:Response.Write("<script language='Javascript'>window.open('http://www.google.com/search?q=' + " + TextB.Text +"','_blank');</script>");
Oded
with this it does work
SlimBoy