View Full Version : Code behind variable for use in <asp: / > Tags ???

03-22-2007, 11:26 AM

I am have been trying to use a variable from a code behind page in my asp page in order to check for a row on a sql server.

I am getting the information to the codebehind page just fine and am able to display it on the page with no problems.

My problem is when I try to use the variable into a sql statement or into an asp parameter, I am not able to access the value of the variable.

here is my code:

Code Behind:

Partial Class index

Inherits System.Web.UI.Page

'I have hard Coded the values into the variables for testing purposes

Protected NAME As String = "Default"
Protected USERID As Integer = 9999

Protected REGION As String = "Default Region"

Protected CORPID As Integer = 9

Protected CORPNAME As String = "Dummy Corp"

Protected CORTITLE As String = "Schlep"

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

NAME = Request.ServerVariables("HTTP.NAME")

UserID = Request.ServerVariables("HTTP.UserID")

Region = Request.ServerVariables("HTTP.Region")

CorpID = Request.ServerVariables("HTTP.CorpID")

CorpName = Request.ServerVariables("HTTP.CorpName")

CorpTitle = Request.ServerVariables("HTTP.CorpTitle")


'Cheesy code (This is the only way I have been able to get it to work)

Me.TextBox1.Text = USERID

Me.TextBox2.Text = NAME

Me.TextBox1.Visible = False

Me.TextBox2.Visible = False

' End of Cheesy work around.


'Testing Variables to web page

Response.Write(NAME & USERID & CORPNAME)

'Variable values display fine to page


End Sub

End Class

Page Code:

'This page is nested into a Master Page

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="index.aspx.vb" Inherits="index" title="Untitled Page" %>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:InsertConnectionString2 %>"

InsertCommand="if not exists (select userID from dbo.UserStatistics where userID = @UserID)begin INSERT INTO dbo.UserStatistics(UserID,Name,corpID,corpName,visits,LastVisit)VALUES(@UserID ,@Name ,10,'Default Corp',0,getdate())end">


<asp:ControlParameter Name = "UserID" ControlID = "TextBox1" />

<asp:ControlParameter Name = "Name" ControlID = "TextBox2" />



Sample Text on page.

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

'More Testing of variables to the page

<%=NAME%> <%=UserID %> <%Response.Write(Corp)%>


'The above code works and does just what I want it to, it grabs a variable from the Code behind page and passes it into a Parameter, which is then used in an insertCommand

in order to make this work, I am having to add a text box to the page, fill it with the value from the code behind page, Hide it, then pass the value to the insertcommand via a ControlParameter.

This seems very cheesy to me, however when I attempt to use <%=Name%> to fill the parameter, I get casting errors, or I end up with "<%=Name%>" stored in my database.

I guess what I am looking for is some advice as to a better way of doing this, it just does not make sence to me to do it this way, creating controls on the page, filling them, then hiding them from the page users just does not feel right.

Is there a way to call the variables from within the <asp: /> tags ?

Thanks in advance for any assistance.


03-22-2007, 01:50 PM
You've got a control parameter there.
I think you wanted just a normal parameter. You can set the value on page load.