查询字符串到文本框

3

我有一个名为tblProfile的表,其中包含Id、Name和Address字段。我有两个aspx页面,分别是Home.aspx和Here.aspx。在Home.aspx中,我使用以下代码将Id传递给Here.aspx:

<asp:HyperLink ID="link" runat="server" NavigateUrl="Here.aspx?id={0}" 
            Font-Names="Times New Roman" Font-Size="Medium" >Click here</asp:HyperLink>

在 Home.aspx.cs 的代码后端:
protected void Page_Load(object sender, EventArgs e)
{
    string bID = Request.QueryString["Id"];
    if (string.IsNullOrEmpty(Id))
    {
        Response.Redirect("Here.aspx?", true);
    }
    ViewState["Id"] = Id;
    link.NavigateUrl = String.Format(link.NavigateUrl, Id);

}

我没有问题将ID传递到第二个页面的URL中。但是我现在想要的是,在我的Here.aspx页面上,有3个文本框应该填充从Home.aspx传递的特定ID的ID、姓名和地址。尝试了很多次,但完全没有运气。任何帮助将不胜感激。顺便说一下,我正在使用asp.net和c#。


你想如何在 Here.aspx 页面中加载姓名和地址?是从数据库检索还是从 Home.aspx 页面获取? - sarwar026
请告诉我们您尝试过的内容,编写代码并让我们知道您在尝试时遇到了什么问题... - Ram Singh
你想把姓名和地址发送到Here.aspx吗?如果是这样,请尝试以下代码:<asp:HyperLink ID="link" runat="server" NavigateUrl="Here.aspx?id={0}&Name={1}&Address={2}" Font-Names="Times New Roman" Font-Size="Medium">点击这里</asp:HyperLink> - MahaSwetha
@sarwar026 - 我正在从我的数据库中检索数据,而不是从Home.aspx中检索。现在Id可以工作了,因为我在Here.aspx的代码后台页面加载中使用了这个txtid.Text = Request.QueryString["id"]; 但是对于Name和Address,我完全没有头绪。再次感谢您的回复。 - user2388316
@MahaSwetha - 我正在从我的数据库中检索数据,而不是从Home.aspx中检索。现在ID可以工作了,因为我在Here.aspx的代码后台页面加载中使用了这个txtid.Text = Request.QueryString["id"]; 但是对于姓名和地址,我完全没有头绪。再次感谢您的回复。 - user2388316
3个回答

1

我们有两种解决方案

解决方案1:在asp.net中使用用户上一页属性,这样您就不需要传递ID。

 protected void Page_Load(object sender, EventArgs e)
    {
        // Find the name on the previous page
        TextBox txt = (TextBox)Page.PreviousPage.FindControl("txtNameText");

        if (txt != null)
            txtName.Text = Server.HtmlEncode(txt.Text);
        else
            txtName.Text = "[Name Not available]";
    }

解决方案2:从查询字符串中获取ID并通过ID在文本框文本属性上分配数据,即:
protected void Page_Load(object sender, EventArgs e)
        {
            // Get value from query string
            string profileID = Request.QueryString["Id"];

            // Fetch data from database by ID
            DataTable dt = FetchEmployeeDataByID(ProfileID);

            txtName.text = dt.rows[0]["Name"].tostring();

        }

0
你可以通过使用查询字符串中的 Id 作为键从数据库中检索配置文件数据来获取名称和地址。
因此,在你的 Here.aspx 的代码后端中:
protected void Page_Load(object sender, EventArgs e)
{
    string profileID = Request.QueryString["Id"];
    // Retrive profile in your db..
    var profile = Profiles.Get(p => p.ProfileID == profileID);

    // Populate the textboxes
    txtId.Text = profileID;
    txtName.Text = profile.Name;
    txtAddress.Text = profile.Address;
}

0

根据您在评论中的回复,我建议执行以下操作:

  1. 通过从查询字符串检索到的 Id,在 Here.aspx 后端 (.cs 文件) 中加载数据
  2. 将加载的数据显示在所需的文本框中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接