2012-08-15 200 views
0

如何用新選項卡/新窗口打開btnSMS.PostBackUrl?或者無論如何去那個URL然後自動去另一個頁面?如何打開在新選項卡中打開鏈接?

更多信息:該URL是使用服務提供商(Clickatell)發送短信的方法。但是除了說明消息發送是否成功之外,URL沒有任何其他內容。我不希望使用我的ASP.NET的用戶卡在那裏。我想讓他們在發送消息後回到我的網站。

protected void btnSMS_Click1(object sender, EventArgs e) 
{ 

    String HP = txtHP.Text; 
    String URL = "http://api.clickatell.com/http/sendmsg?user=myuser&password=mypassword&api_id=myapi_id&to="; 
    String Text = "&text=" + txtSMS.Text; 
    btnSMS.PostBackUrl = URL + HP + Text; 

    string username; 
    //Sql Connection to access the database 
    SqlConnection conn6 = new SqlConnection("MY CONNECTION"); 
    //Opening the Connnection 
    conn6.Open(); 
    string mySQL; 
    username = HttpContext.Current.User.Identity.Name; 
    //Insert the information into the database table Login 
    mySQL = "INSERT INTO Table_Message(Username, Message, Title, SendTo) VALUES('" + username + "','" + txtSMS.Text.Trim() + "','" + txtTitle.Text.Trim() + "','" + txtHP.Text.Trim() + "')"; 

    SqlCommand cmdAdd = new SqlCommand(mySQL, conn6); 
    //Execute the sql command 
    cmdAdd.ExecuteNonQuery(); 
    //Close the Connection 
} 

回答

1

你的按鈕,如下應密切反映:

<asp:Button runat="server" ID="btnSMS" UseSubmitBehavior="false" OnClick="btnSMS_Click1" Text="Send/> 

然後在後臺代碼構建回傳網址,然後submition之後添加如下:

protected void btnSMS_Click1(object sender, EventArgs e) 
     { 


String HP = txtHP.Text; 
      String URL = "http://api.clickatell.com/http/sendmsg?user=myuser&password=mypassword&api_id=myapi_id&to="; 
      String Text = "&text=" + txtSMS.Text; 
      string UrlFinal = URL + HP + Text; 

      string username; 
      //Sql Connection to access the database  
      SqlConnection conn6 = new SqlConnection("Data Source=19-17\\sqlexpress;" + "Initial Catalog = Suite2; Integrated Security =SSPI"); 
      //Opening the Connnection  
      conn6.Open(); 
      string mySQL; 
      username = HttpContext.Current.User.Identity.Name; 
      //Insert the information into the database table Login  
      mySQL = "INSERT INTO Table_Message(Username, Message, Title, Startdate, Enddate, SendTo) VALUES('" + username + "','" + txtSMS.Text.Trim() + "','" + txtTitle.Text.Trim() + "','" + txtHP.Text.Trim() + "')"; 

      SqlCommand cmdAdd = new SqlCommand(mySQL, conn6); 
      //Execute the sql command  
      cmdAdd.ExecuteNonQuery(); 
      //Close the Connection 

      this.ClientScript.RegisterStartupScript(this.GetType(), 
             "navigate", 
             "window.open('" + UrlFinal + "');", 
             true); 

     } 

這應該會在新窗口中打開。或者,也可以使用window.navigate,這將在同一瀏覽器中打開另一個頁面。

1

我認爲實現這個更好的方法是使窗體發佈到你的網站,一個網址,你可以到,然後做職,你從你的服務器使用的服務反應,將用戶重定向到適當的URL。

UPDATE

我假設你正在使用網絡的形式在這裏。

問題是,您可以發佈到您想要的網址,但您的用戶在網頁上看不到任何更改。另一個問題是,您將密碼和服務的API_ID置於回髮網址中,任何可以導航到您的網頁並按F12鍵的人都可以看到該網址。讓您的API_ID可用於公開是一個很大的錯誤。它應該只是你和遠程服務應該知道的東西。

這是我的解決方案。

當你第一次加載頁面時,你會顯示一個空的表單,並允許用戶輸入你想要的數據。

然後在單擊事件處理程序中,您將獲取所需的數據並手動發佈到該服務。 HTTP帖子包含要發送到服務的鍵值對列表。您必須手動構建發佈請求,並獲得遠程服務的響應。

所以這裏是你的點擊處理程序的樣子。現在

protected void btnSMS_Click1(object sender, EventArgs e) 
{ 
    Dictionary<string,string> postValues = new Dictionary<string,string>(); 

    // gather the key value pairs ou want from your controls and add them to the dictionary. 
    // and call postSynchronous method (which I'll explain below) 
    string result = postSynchronous(URLtoPOST,postValues); 

    if (result= yourSuccessValue){ 
     // redirect to a page that says.. 'Yay!! you successfully posted to the service ?' 
     Server.Transfer("successPage.aspx"); 
    }else 
    { 
     // what to do if it fails ? 
    } 
} 

postSynchronous方法(或者你想叫它什麼都)是你必須手動寫的東西,這將需要一個RUL,以張貼和鍵值對的列表與郵寄,然後構建實際的發佈請求。

看看這個鏈接的教程,瞭解如何做到這一點。 http://petewarden.typepad.com/searchbrowser/2008/09/how-to-post-an.html

我試圖嵌入代碼的方法和任何相關的方法,但它太長了。

因此,以這種方式做事情時最好不要將API密鑰或密碼發送給用戶的瀏覽器,這樣他們永遠不會知道它。 (否則,他們可以使用密鑰來訪問服務,因爲你是一個安全漏洞)

這有什麼不好,你的解決方案比較你是發佈在你的服務器上,這會給你帶來CPU和網絡負載服務器。但我想這對您獲得的安全性好處是很好的折衷。

希望這有助於。並隨時提出任何問題。

+0

對不起,我不明白你的意思。有沒有關於如何做到這一點的例子? – user1490374 2012-08-15 09:13:07

+0

我更新了答案 – Madushan 2012-08-16 03:54:34

0

是否可以將屬性target="_blank"添加到btnSMS項目? 如果它看起來會像這樣的鏈接元素:

id="btnSMS" href="" target="_blank" 
+0

嗯,我不明白你的意思。對不起,我仍然是一個學習ASP.NET的學生..任何想法如何打開新標籤中的鏈接? – user1490374 2012-08-15 09:19:44

+0

這個提示是爲了一個可能的HTML元素,它在點擊時執行你的代碼。 'target =「_ blank」'屬性可以將鏈接事件重定向到一個新的選項卡或窗口。 – sthag 2012-08-15 09:26:06

+0

我認爲你的代碼是由一個HTML元素的事件執行的。很抱歉,如果我誤導你回答我的問題。 – sthag 2012-08-15 09:33:55

0

一個重要的切線---你正在危險地打開SQL注入攻擊。

您絕對不應該永遠不要使用SQL查詢連接來自用戶的文本輸入。

在您的SQL查詢字符串中使用@Parameter值,然後使用SqlCommand參數替換這些值。

string mySQL = "INSERT INTO Table_Message(Username, Message, Title, SendTo) VALUES(@Username, @Message, @Title, @SendTo)"; 
SqlCommand cmdAdd = new SqlCommand(mySQL, conn6); 
cmdAdd.Parameters.AddWithValue("@Username", username); 
cmdAdd.Parameters.AddWithValue("@Message", txtSMS.Text.Trim()); 
cmdAdd.Parameters.AddWithValue("@Title", txtTitle.Text.Trim()); 
cmdAdd.Parameters.AddWithValue("@SendTo", txtHP.Text.Trim()); 
cmdAdd.ExecuteNonQuery(); 

它甚至會更好,明確指定類型:

cmdAdd.Parameters.Add("@Username", SqlDbType.NVarChar, 100).Value = username; 

見特洛伊亨特的真棒系列 - http://www.troyhunt.com/2010/05/owasp-top-10-for-net-developers-part-1.html

相關問題