This project is read-only.

ID variable

Topics: Developer Forum, Localization Forum, Project Management Forum, User Forum
May 17, 2011 at 6:06 AM
Edited May 17, 2011 at 6:14 AM

on the contact form I would like to capture one variable in a field that are passed from another page...

e.g. http://www.sitename.com/order.aspx?id=123456789

in the contact form I have the input field:

<asp:TextBox runat="server" ID="FieldID" Width="200px"></asp:TextBox>

have tried with:

<

asp:TextBox runat="server" ID="FieldID" Width="200px" Request.QueryString="id"> 

 

what's the correct syntax ?  



May 17, 2011 at 8:31 AM

There are 2 things todo here.

  1. In the first child node after the @Page Deceleration you must import your namespace
  2. You need to add the correct formatting and also to what parameter your adding the value to (text). It would be something like this 
    <asp:TextBox runat="server" ID="FieldID" Width="200px" Text='<%# Request.QueryString["id"]%>'>
    

Alternatively (Recommended) you can just add this in the code behind file during Page Load and if its not a post-back like this.

Would not bother with the old markup version above as its just a waist of time and can cause many headaches even though your doing something simple here I would suggest to you to not make it practice :-)

Try this

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        FieldID.Text = Request.QueryString["id"]; // <!-- much better and in a single line plus you also get Intellisense and if anything changes visual studio will let you know before you make it a live bug!
}

Might also pay for you to check the query exists as this may cause an error.

May 17, 2011 at 11:55 AM

Hi SpiderMaster

Thanks ! - done it the recommended way  - and it works perfect... ;0)

How can I make it read only (after the ID are written in there)?

 

 

May 17, 2011 at 12:19 PM

If you mean you want the text box to be readonly!! then in that page load method you could do this

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FieldID.Text = Request.QueryString["id"];
        
        // Select either of the options below to suite your need. 
        // FieldID.Enabled = false; // is visable but user can not edit
        // FieldID.Visible = false // is not visible or editable.
    }
}

Also note that if you never want to change this value then you could just add it to the textbox tag in the markup like this

<asp:TextBox runat="server" ID="FieldID" Visible="false"/>

In my personal opinion assuming you only want to see this when testing I would do this

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FieldID.Text = Request.QueryString["id"];
        
#if DEBUG // visible when debugging but not editable.
        FieldID.Enabled = false;
#else // not visible when not debugging
        FieldID.Visible = false
#endif
    }
}

May 31, 2011 at 10:24 AM

Hi Wiper, Was just thinking if you do not need to display this value at any time you may want to use ViewState (better practice) for holding the value rather than using a text box in the page.

http://msdn.microsoft.com/en-us/library/ms972976.aspx

Here is an example.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string urlid = Request.QueryString["id"];

                if (urlid != null || urlid != string.Empty)
                    MyID = Request.QueryString["id"];
            }
        }

        public string MyID
        {
            get
            {
                string storedid = (string)ViewState["id"];

                if (storedid != null || storedid != string.Empty)
                    return storedid;
                else
                    return string.Empty;
            }
            set { ViewState["id"] = value; }
        }

        protected void My_Click(object sender, EventArgs e)
        {
            if (MyID != string.Empty)
                SendEmail(MyID);
            else throw new Exception("My ID was not found!");
        }

        private static void SendEmail(string UseID)
        {
            // Fill with your method info!
        }