Formview textbox populated with request.querystring

Last post 05-09-2008 9:58 AM by gbogea. 6 replies.

Sort Posts:

  • Formview textbox populated with request.querystring

    05-08-2008, 6:22 PM
    • Loading...
    • juicejug
    • Joined on 05-08-2008, 4:04 PM
    • Posts 5

    I have a textbox with the ID TextBox1 in a formview.  On page load I want the request.querystring("text") to be passed to the formview textbox.  I am using the following code in the .cs file:

     

    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["text"] != null)
            {
                string requestText = Request.QueryString["text"];
                TextBox1.Text = requestText;
            }
        }
    }
    The problem is, when I go to run the project, I get the following error 'The name 'TextBox1' does not exist in the current context'.
    How can I get the value in the querystring passed to the textbox in the formview if it exists?
  • Re: Formview textbox populated with request.querystring

    05-08-2008, 10:11 PM
    Answer
    • Loading...
    • gbogea
    • Joined on 04-14-2008, 11:17 PM
    • Brazil
    • Posts 198

     It depends on which Templates (Insert, Edit, Item) your TextBox is in. You first need to test if you're in the correct mode then call FindControl to get it and then you can manipulate it's value:

            if (FormView1.CurrentMode == FormViewMode.Insert)
            {
                TextBox tb = (TextBox)FormView1.FindControl("TextBox1");

                tb.Text = Request.QueryString["text"];

            }

    Hope it helps. 

    Gabriel Bogéa (http://www.gbogea.com)
    -----------------
    Please 'Mark as Answer' the post(s) that helped you
  • Re: Formview textbox populated with request.querystring

    05-08-2008, 10:18 PM
    Answer
    • Loading...
    • vinz
    • Joined on 10-05-2007, 3:47 PM
    • Cebu Philippines
    • Posts 4,230

    As suggested above, You need to extract the Control using the FindControl Method first before assigning it a value, since the control resides within the FormView and be sure to check the FormView mode before doing anything else..

    if (FormView1.CurrentMode == FormViewMode.ReadOnly) {
           
        TextBox tb = (TextBox)FormView1.Row.Cells(0).FindControl("TextBox1"); //Just change the cells index to where the control was reside
        if (Request.QueryString["text"] != null)

        { 

        string requestText = Request.QueryString["text"];  

        tb.Text = requestText ;

       }
           

    Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post fixed your problem.


  • Re: Formview textbox populated with request.querystring

    05-08-2008, 10:21 PM
    • Loading...
    • juicejug
    • Joined on 05-08-2008, 4:04 PM
    • Posts 5

    If you already know which template is located in does that affect how you would code it?  Or is it programatically better to use FindControl to locate it each time?

  • Re: Formview textbox populated with request.querystring

    05-08-2008, 10:23 PM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 3:47 PM
    • Cebu Philippines
    • Posts 4,230

    :

    If you already know which template is located in does that affect how you would code it?  Or is it programatically better to use FindControl to locate it each time?

    You need to use FindControl method when extracting controls thats inside the NamingContainers such as FormView ,Grids or something... 

    Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post fixed your problem.


  • Re: Formview textbox populated with request.querystring

    05-09-2008, 9:44 AM
    • Loading...
    • juicejug
    • Joined on 05-08-2008, 4:04 PM
    • Posts 5

    Thanks.  Both posts showed me how and work just the way I wanted.  I guess the question I should have asked is if you must know which template the textbox is located in or if you can search each template for the control.

  • Re: Formview textbox populated with request.querystring

    05-09-2008, 9:58 AM
    • Loading...
    • gbogea
    • Joined on 04-14-2008, 11:17 PM
    • Brazil
    • Posts 198

     You should know which template your control should be in, that's why I added the if test in my code.

    If you are currently in ReadOnly template then your control doesn't exist on the page, hence if you use FindControl("MyControl") it will return null.

    One way to do it ignoring the current mode would be like this:

                TextBox tb = (TextBox)FormView1.FindControl("TextBox1");

                if (tb != null) {

                  //this means that the Control was found
                  tb.Text = Request.QueryString["text"];

                } 

    In this approach you don't have to check the CurrentMode of the FormView. Maybe this is a better solution indeed.
     

    Gabriel Bogéa (http://www.gbogea.com)
    -----------------
    Please 'Mark as Answer' the post(s) that helped you
Page 1 of 1 (7 items)