如何在文本框中输入数据并以编程方式单击某个网站上的按钮

时间:2015-02-23 12:58:04

标签: c# asp.net

我必须从网站上废弃数据,我可以使用Import.io来做到这一点,但我必须通过在C#编写简单程序来学习如何做。

假设该页面包含一个文本框和一个按钮,当单击按钮时,它会提供搜索结果。

Page1 : www.example.com
Search : <input type="text" value="abc" id="search" >
<input type="submit" id="submit">

Page2: www.example.com/result.aspx
<body>
<p>you have entered abc</p>
</body>

现在我只想运行我的localhost时的result.aspx数据:5585 / deafult.aspx。

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要以某种方式将第1页中输入的数据传递给第2页。最简单的方法是使用查询字符串参数。

更新您的Page 1标记以使用服务器控件:

<asp:TextBox ID="search" runat="server" />
<asp:Button ID="submit" runat="server" OnClick="btnSubmit_OnClick" />

为提交按钮创建点击事件:

protected void btnSubmit_OnClick(Object sender, EventArgs e)
{
    var redirectUrl = string.Format("/result.aspx?input={0}", search.Text);

    Response.Redirect(redirectUrl);
}

然后在第2页,在页面加载时,你想从查询字符串中获取它:

protected void Page_Load(Object sender, EventArgs e)
{
    var input = Request.QueryString["input"];

    if (!string.IsNullOrEmpty(input))
    {
        litResult.Text = input;
    }
}

您还需要稍微更新您的标记:

<body>
<p>you have entered <asp:Literal ID="litResult" runat="server" /></p>
</body>