用文本框值在HTML中构建URL
本文关键字:构建 URL HTML 文本 | 更新日期: 2023-09-27 18:04:15
我正在为我的网站添加一个按钮,将用户带到一个网站,并从我的网站上的文本框填写各种变量。
下面是我的HTML页面的标记代码:
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="FirstNameLabel" runat="server" Text="First Name:" Width="100px"></asp:Label>
<asp:TextBox ID="FirstNameTxtBox" runat="server" Width="150px"></asp:TextBox>
<br />
<asp:Label ID="LastNameLabel" runat="server" Text="Last Name:" Width="100px"></asp:Label>
<asp:TextBox ID="LastNameTextBox" runat="server" Width="150px"></asp:TextBox>
<br />
<asp:Label ID="EmailLabel" runat="server" Text="E-mail Address:" Width="100px"></asp:Label>
<asp:TextBox ID="EMailTextBox" runat="server" Width="150px"></asp:TextBox>
<br />
<asp:Label ID="DescLabel" runat="server" Text="Description:" Width="100px"></asp:Label>
<asp:TextBox ID="DescTextBox" runat="server" Height="100px" Width="150px"></asp:TextBox>
</div>
<button class="button" style="height:20px;width:120px" onclick="<%# String.Format("location.href='https://biznetsoftware.fastsupport.com/?first_name={0}&last_name={1}&email={2}&question= {3}",FirstNameTxtBox.Text,LastNameTextBox.Text,EMailTextBox.Text,DescTextBox. Text) %>" id="JoinChatButton">Join Chat</button>
</form>
</body>
页面没有转到目标URL。我做错了什么?
您可以使用以下代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
void JoinChat(Object sender, EventArgs e)
{
string url = String.Format("https://biznetsoftware.fastsupport.com/?first_name={0}&last_name={1}&email={2}&question={3}",
FirstNameTxtBox.Text,
LastNameTextBox.Text,
EMailTextBox.Text,
DescTextBox.Text);
Response.Redirect(url);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="FirstNameLabel" runat="server" Text="First Name:" Width="100px"></asp:Label>
<asp:TextBox ID="FirstNameTxtBox" runat="server" Width="150px"></asp:TextBox>
<br />
<asp:Label ID="LastNameLabel" runat="server" Text="Last Name:" Width="100px"></asp:Label>
<asp:TextBox ID="LastNameTextBox" runat="server" Width="150px"></asp:TextBox>
<br />
<asp:Label ID="EmailLabel" runat="server" Text="E-mail Address:" Width="100px"></asp:Label>
<asp:TextBox ID="EMailTextBox" runat="server" Width="150px"></asp:TextBox>
<br />
<asp:Label ID="DescLabel" runat="server" Text="Description:" Width="100px"></asp:Label>
<asp:TextBox ID="DescTextBox" runat="server" Height="100px" Width="150px"></asp:TextBox>
</div>
<asp:Button class="button" style="height:20px;width:120px" id="JoinChatButton" Text="Join Chat" runat="server"OnClick="JoinChat" />
</form>
</body>
</html>
首先您忘记了location.href='...'
末尾的'字符
同样,由于位置原因,页面不会去任何地方。Href不是onclick的一部分,因为您需要转义onclick
中的所有双引号字符(")这个应该可以工作:
<button class="button" style="height:20px;width:120px" onclick="<%# String.Format('"location.href='https://biznetsoftware.fastsupport.com/?first_name={0}&last_name={1}&email={2}&question={3}''", FirstNameTxtBox.Text,LastNameTextBox.Text,EMailTextBox.Text,DescTextBox.Text) %>" id="JoinChatButton">Join Chat</button>