locked
QueryString - Retrieve text of linklabel and pass to another form? RRS feed

  • Question

  • User1389508859 posted

    Hey there,

    I have a table populated with data in which a user is able to select one for editing. The user selects which record is to be edited by the primary key assigned to the record, I have made this primary key a linklabel that then redirects them to the update record page (...<a href='UpdateReport.aspx'>). When they are redirected, I want the primary key # to be passed to the UpdateReport page as well. Does that make sense? So let's say the user clicks on record 101, I want 101 passed to the new form so I can populate it's data.

    I have tried a few different methods, but I seem to fall short on all of them or cannot complete the thought/logic in my mind. Any help?

    Sunday, September 22, 2013 3:01 PM

Answers

  • User1508394307 posted

    I would still retrieve it like this, correct? ...

    Nope, the key must be same as you see in the query string.

    So, if you do

    Response.Write("<td><a href='updateReport.aspx?id=" + records["reportID"] + "'>Click me</a></td>");

    then your key is "id" as you see it in the address bar of the browser

    updateReport.aspx?id=xxx

    So, the same key you can get from the Request.QueryString collection:

    label.Text += Request.QueryString["id"];

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2013 3:38 AM

All replies

  • User1508394307 posted

    The link should have following format

    <a href='UpdateReport.aspx?id=xxx'>

    where xxx is your key value.

    If it is a public/protected (class level) variable

    protected string Key;

    then you can do

    <a href='UpdateReport.aspx?id=<%=Key%>'>

    if link is located in a data driven control, such as gridview, you can do

    <asp:TemplateField>
    <ItemTemplate>
    <asp:hyperlinkfield datanavigateurlfields="KeyColumn"
            datanavigateurlformatstring="UpdateReport.aspx?Id={0}" />
    </ItemTemplate>
    </asp:TemplateField>

    And finally in UpdateReport.aspx you could get the value using QueryString collection

    string key = Request.QueryString["id"];

    Sunday, September 22, 2013 3:19 PM
  • User1389508859 posted

    Smirnov,

    Thank you for the reply. I have tried the following, and both pass NULL values. (**Note that this is within a <table> control**)

    Response.Write("<td><a href='updateReport.aspx?id=records['reportID']>" + records["reportID"] + "</a></td>");

    I also tried creating a variable 'string key = null' and assigning the reportID to it in the loop that populates the table, again .. nothing is passed.

                    // Fill data
                    do
                    {
                        key = records["reportID"].ToString();
                        Response.Write("<tr>");
                        Response.Write("<td><a href='updateReport.aspx?id=key'>" + records["reportID"] + "</a></td>");
                        Response.Write("<td>" + records["reportDate"] + "</td>");
                        Response.Write("<td>" + records["shortDescription"] + "</td>");
                        Response.Write("</tr>");
                    } while (records.Read());

    And I apologize for my lack of thoroughness; I've been messing with this for about an hour trying numerous things and am slightly frusterated. I suppose the thing to do would be to walk away for a bit.

    Sunday, September 22, 2013 3:33 PM
  • User1508394307 posted

    Try

    // Fill data
    do
    {
    Response.Write("<tr>");
    Response.Write("<td><a href='updateReport.aspx?id=" + records["reportID"] + "'>Click me</a></td>");
    Response.Write("<td>" + records["reportDate"] + "</td>");
    Response.Write("<td>" + records["shortDescription"] + "</td>");
    Response.Write("</tr>");
    } while (records.Read());

    or instead of 

    Response.Write("<td><a href='updateReport.aspx?id=" + records["reportID"] + "'>Click me</a></td>");

    you can format string as

    Response.Write(String.Format("<td><a href='updateReport.aspx?id={0}'>Click me</a></td>",
        records["reportID"].ToString()));

    which might be more clear.

    Hope this helps.

    Sunday, September 22, 2013 3:41 PM
  • User1389508859 posted

    Smirnov,

    Great thoughts, but I still am not retrieving the data (reportID) I am looking for on the updateReport form. I would still retrieve it like this, correct? ...

    public partial class UpdateReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            label.Text += Request.QueryString["reportID"];
        }
    }


    I may have to reach out to my instructor if I don't get this, which sucks because that means that I wouldn't be able to make any more progress today.

    Sunday, September 22, 2013 3:51 PM
  • User-1716253493 posted
    Response.Write(string.Format("<td><a href='updateReport.aspx?repordID={0}'>{0}</a></td>",records["reportID"]));

    the value passed by this link : updateReport.aspx?reportID=123

    Sunday, September 22, 2013 8:59 PM
  • User1508394307 posted

    I would still retrieve it like this, correct? ...

    Nope, the key must be same as you see in the query string.

    So, if you do

    Response.Write("<td><a href='updateReport.aspx?id=" + records["reportID"] + "'>Click me</a></td>");

    then your key is "id" as you see it in the address bar of the browser

    updateReport.aspx?id=xxx

    So, the same key you can get from the Request.QueryString collection:

    label.Text += Request.QueryString["id"];

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2013 3:38 AM