2009-11-27 92 views
1

我现在的问题是,我需要点击2次以搜索正确的文本进行搜索。有没有办法检视我发送的内容并更新它?还有其他解决方案吗?两个决定只是一个按钮

你好!

我有2个单选按钮,决​​定使用什么搜索引擎(谷歌或我的),我有1个按钮,将提交一个文本,将用于搜索在2搜索引擎之一。当我尝试执行谷歌搜索时,第一次只是在第二次点击时打开新窗口,它会打开带有我想要搜索的词的谷歌窗口,如果我尝试执行搜索,则执行两次搜索。我做错了什么?我如何以正确的方式工作?

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

,然后我有...

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');"); 
     } 
    } 

...我有这个...

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

回答

0

,而不是输入控件用asp:单选按钮与自动过帐财产真实并在设计时创建并分配其事件

+0

但后来我不需要按钮吗?我只想搜索当我点击按钮不当我选择单选按钮。如果我这样做会怎么样? – SlimBoy 2009-11-27 16:04:32

0

编辑:更新了代码以打开搜索窗口。

以下内容可能对您有所帮助。这不是执行搜索,但你应该能够得到大致的想法。 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> 

代码隐藏:

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); 
     } 

交换的Response.Write对到执行搜索的方法的调用。

+0

任何理由为什么行 ClientScript.RegisterStartupScript(GetType(),“popup”,clientScript,true); 给我一个错误“方法无重载”RegisterStartupScript'取'4'参数“ 当我尝试在ClientScript中搜索'RegisterStartupScript'时,什么也没有......我该怎么办? – SlimBoy 2009-11-30 13:23:07

+0

如果我做 “Page.ClientScript.RegisterStartupScript(...” 是一回事吗?如果是相同的它不会在我的项目工作 – SlimBoy 2009-11-30 13:44:07

+0

它运行的代码,但它不打开一个新窗口 – SlimBoy 2009-11-30 13:44:58

1

的问题是这行代码:

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

您的OnClick添加一个客户端,将打开一个新的窗口中搜索(在_blank目标)。

如果你做一个:

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

像您期望的事情会解决的。

+0

这个工程!但只是一个更详细的信息....如果我想在新窗口中打开我需要做什么? – SlimBoy 2009-11-30 12:49:19

+0

你说你想打开一个新的页面 - 两个选项,或只是其中之一? – Oded 2009-11-30 13:45:26

+0

在点击链接打开一个新的浏览器窗口,谷歌搜索 – SlimBoy 2009-11-30 15:13:57