如何打开在新选项卡中打开链接?
如何用新选项卡/新窗口打开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
}
你的按钮,如下应密切反映:
<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,这将在同一浏览器中打开另一个页面。
我认为实现这个更好的方法是使窗体发布到你的网站,一个网址,你可以到,然后做职,你从你的服务器使用的服务反应,将用户重定向到适当的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和网络负载服务器。但我想这对您获得的安全性好处是很好的折衷。
希望这有助于。并随时提出任何问题。
是否可以将属性target="_blank"
添加到btnSMS项目? 如果它看起来会像这样的链接元素:
id="btnSMS" href="" target="_blank"
嗯,我不明白你的意思。对不起,我仍然是一个学习ASP.NET的学生..任何想法如何打开新标签中的链接? – user1490374 2012-08-15 09:19:44
这个提示是为了一个可能的HTML元素,它在点击时执行你的代码。 'target =“_ blank”'属性可以将链接事件重定向到一个新的选项卡或窗口。 – sthag 2012-08-15 09:26:06
我认为你的代码是由一个HTML元素的事件执行的。很抱歉,如果我误导你回答我的问题。 – sthag 2012-08-15 09:33:55
一个重要的切线---你正在危险地打开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
对不起,我不明白你的意思。有没有关于如何做到这一点的例子? – user1490374 2012-08-15 09:13:07
我更新了答案 – Madushan 2012-08-16 03:54:34