首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在新标签页中打开链接?

如何在新标签页中打开链接?
EN

Stack Overflow用户
提问于 2012-08-15 17:02:46
回答 4查看 4.9K关注 0票数 0

如何使用新选项卡/新窗口打开btnSMS.PostBackUrl?或者,有没有办法转到那个URL,然后自动转到另一个页面?

更多信息:该URL是一种使用服务提供商(Clickatell)发送SMS的方法。但是URL除了声明消息发送是否成功之外,什么也没有得到。我不希望使用我的ASP.NET的用户卡在那里。我想让他们在发完信息后回到我的网站上。

代码语言:javascript
复制
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
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-15 18:49:26

您的按钮应紧密反射,如下所示:

代码语言:javascript
复制
<asp:Button runat="server" ID="btnSMS"  UseSubmitBehavior="false"  OnClick="btnSMS_Click1"  Text="Send/> 

然后在代码隐藏中构建您的回发url,然后在提交之后添加以下内容:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-08-15 17:09:25

我认为实现这一点的更好方法是将表单发布到站点中的URL,您可以对此做出反应,然后从服务器将表单发布到您正在使用的服务,并将用户重定向到适当的URL。

更新

我假设您在这里使用的是web-forms。

问题是,您可以发布到您想要的URL,但您的用户在页面上看不到任何更改。另一个问题是,您将服务的密码和API_ID放在回发URL中,任何可以导航到您的页面并按下F12键的人都可以看到它。让你的API_ID向公众开放是一个很大的错误。它应该是只有你和远程服务才知道的东西。

这是我的解决方案。

当您第一次加载页面时,您将显示一个空表单,并允许用户输入您需要的数据。

然后,在单击事件处理程序中,您将获得所需的数据并手动将其发送到服务。HTTP post包含要发送到服务的键值对的列表。您必须手动构建post请求,并从远程服务获取响应。

下面是你的点击处理程序应该是什么样子的。

代码语言:javascript
复制
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方法(或任何您想调用它的方法),它将使用一个要post的RUL和一个与post一起发送的键值对列表,然后构建实际的post请求。

请查看此链接以了解如何执行此操作的教程。http://petewarden.typepad.com/searchbrowser/2008/09/how-to-post-an.html

我试图嵌入该方法和任何相关方法的代码,但是太长了。

因此,这样做的好处是,您永远不会将API密钥或密码发送到用户的浏览器,这样他们就永远不会知道。(否则他们可以使用密钥以您的身份访问服务,这是一个安全漏洞)

与您的解决方案相比,最糟糕的是您在服务器上发布内容,这会给服务器带来CPU和网络负载。但我想这是一个很好的交易,因为你得到了安全的好处。

希望这能有所帮助。有任何问题都可以提出来。

票数 1
EN

Stack Overflow用户

发布于 2012-08-15 17:16:07

是否可以将属性target="_blank"添加到btnSMS项中?如果它是一个link元素,它将如下所示:

代码语言:javascript
复制
id="btnSMS" href="" target="_blank"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11966591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档