2009-11-27 81 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