0

我想将文本框值“传输”到 html 代码中。例如,文本框的值是一个网站,如form1.aspx 中的http://www.google.com。我希望将值放入 form2.aspx 的 html 代码中。我可以问我应该如何解决这个问题吗?

<span class="logoposition">
<a href="http://www.google.com"><img src="Logo.jpg" height= "120"; width="280"; /></a>
</span>

就像我希望 textbox1 值替换当前的 www.google.com

4

1 回答 1

0

您可以这样做:

Webform1 标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SOTest1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>   
</head>
<body>
    <form id="form1" runat="server">
        <div>            
                <asp:Label ID="Label1" runat="server" Text="Your text: ">
                </asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" />

            </div>
    </form>
</body>
</html>

Webform1 代码:

using System;

namespace SOTest1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            string myString = Server.HtmlEncode(TextBox1.Text);
            Response.Redirect("Webform2.aspx?mystring=" + myString);
        }
    }
}

Webform2 标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="SOTest1.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <span class="logoposition">
        <a href="http://www.google.com" runat="server" id="myAnchor"><img src="Logo.jpg" height= "120"; width="280"; /></a>
    </span>
    </div>
    </form>
</body>
</html>

Webform2 代码:

using System;

namespace SOTest1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string mystring = Request.QueryString["mystring"];
            myAnchor.HRef = mystring;
            myAnchor.InnerText = mystring;
        }
    }
}
于 2013-09-13T06:31:07.660 回答