How to access HTML variable in code-behind

Last post 04-29-2008 1:01 PM by GregSQL. 5 replies.

Sort Posts:

  • How to access HTML variable in code-behind

    04-29-2008, 12:03 PM
    • Loading...
    • GregSQL
    • Joined on 07-02-2007, 7:27 PM
    • Posts 58

     How can I access the ItemNo variable from my code-behind? I've tried assigning it to the text of a textbox but I get an error that <% %> is not allowed for server controls. 

    <%

    Dim ItemNo As String

    ItemNo = Request.QueryString("itemno")

    Response.Write(ItemNo)

    Session.Add(
    "ItemNo", ItemNo)

     

    %>

     

    <asp:TextBox ID="ItemNo" runat="server"></asp:TextBox>

    <input type="text" id="itemno1" name="textfield" value="<%=Request.QueryString("itemno")%>" />

  • Re: How to access HTML variable in code-behind

    04-29-2008, 12:11 PM
    • Loading...
    • docluv
    • Joined on 06-29-2002, 11:16 PM
    • Willow Spring NC
    • Posts 1,410
    • ASPInsiders
      TrustedFriends-MVPs

    ItemNo.Text

     

    This is a Web Control, it would not be accessed the same way as just a form element. :)

  • Re: How to access HTML variable in code-behind

    04-29-2008, 12:18 PM
    • Loading...
    • GregSQL
    • Joined on 07-02-2007, 7:27 PM
    • Posts 58

    I'm trying to access the string in the HTML page (Dim ItemNo As String) from the code-behind. It says it isn't declared.

    I know I can access the <asp:TextBox>  from the code-behind, but what about the string or the <input> field?

  • Re: How to access HTML variable in code-behind

    04-29-2008, 12:25 PM
    • Loading...
    • GridViewBoy
    • Joined on 04-23-2008, 8:09 PM
    • Posts 8

    This might not be the exact answer, but you can use a ViewState and set the QueryString to it, so you can retrieve it in the code behind. Or you could use a label instead of a string.
     

     

  • Re: How to access HTML variable in code-behind

    04-29-2008, 12:32 PM
    • Loading...
    • GregSQL
    • Joined on 07-02-2007, 7:27 PM
    • Posts 58

    GridViewBoy:

    This might not be the exact answer, but you can use a ViewState and set the QueryString to it, so you can retrieve it in the code behind. Or you could use a label instead of a string.
     

     

     

    Here is what I tried:

     

    <%

    Dim ItemNo2 As String

    ItemNo2 = Request.QueryString("itemno")

    Response.Write(ItemNo2)

    Session.Add(
    "ItemNo", ItemNo2)

     

    ViewState.Add(
    "ItemNo", ItemNo2)

     

     

    %>

    When I try to access the viewstate or session variable in the code-behind, I get an error as if the variable doesn't exist.

     I was able to set a label in the HTML page using Label1.Text = ItemNo but when I test it, the value doesn't change. Same thing happens when I try to set a textbox. The value doesn't change.

     

    <asp:Label ID="Label1" runat="server" Text="Item"></asp:Label>

    &nbsp;

     

     

    <asp:TextBox ID="ItemNo" runat="server"></asp:TextBox>

     

    <%

    Dim ItemNo2 As String

    ItemNo2 = Request.QueryString("itemno")

    Response.Write(ItemNo2)

    Session.Add(
    "ItemNo", ItemNo2)

     

    ViewState.Add(
    "ItemNo", ItemNo2)

    ItemNo.Text = ItemNo2

    Label1.Text = ItemNo2

     

    %>

  • Re: How to access HTML variable in code-behind

    04-29-2008, 1:01 PM
    • Loading...
    • GregSQL
    • Joined on 07-02-2007, 7:27 PM
    • Posts 58

    The answer was to simply do this in the code-behind instead of the HTML

     

    Dim ItemNo As String

    ItemNo = Request.QueryString("itemno")

     

    Sorry, I'm a doofus sometimes.

Page 1 of 1 (6 items)