Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryString to Textbox

Tags:

c#

asp.net

I have a table tblProfile, which contains fields with Id, Name, Address. I have 2 aspx page, which are Home.aspx and Here.aspx. On my Home.aspx I have used this code to pass the Id in my 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>

In code behind 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);

}

I don't have any problem with passing the Id to url in 2nd page. But what I want right now is, on my Here.aspx, I have 3 textboxes which supposed to be filled by the Id, Name and Address of the certain Id that passed from the Home.aspx. Tried many but had no luck at all. Any help would be appreciated. By the way, I'm using asp.net with c#.

like image 817
user2388316 Avatar asked Jul 17 '26 02:07

user2388316


1 Answers

We are having two solution for it

Solution 1 : User Previous page property in asp.net so that you no need to pass ID. i.e.

 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]";
    }

Solution 2 : Get ID from query string and get Data by ID in assign on text box text property.i.e.

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();

        }
like image 110
Yashwant Kumar Sahu Avatar answered Jul 19 '26 17:07

Yashwant Kumar Sahu