I am attempting to use a session variable to simply map a logged in location for a user. Please see the code below (I have edited out information that is superfluous to the problem as an attempt for clarity):
Index.aspx(default home page with a standard, MS Login. This works fine and throws errors if the entered credentials are wrong and does go to the correct destination once verified):
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
CreateUserSession()
End If
End Sub
Protected Sub CreateUserSession()
Dim strConn As String = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
Dim CurrentUser As MembershipUser = Membership.GetUser(HttpContext.Current.User.Identity.Name)
Dim CurrentUserID As Guid = DirectCast(CurrentUser.ProviderUserKey, Guid)
Dim sql As String = "Select * from aspnet_UserProfile where UserID ='" + CurrentUserID.ToString() + "'"
<-- This is a custom table that has the UserID and Path in it
Dim conn As New Data.SqlClient.SqlConnection(strConn)
Dim Cmd As New Data.SqlClient.SqlCommand(sql, conn)
conn.Open()
Dim objDR As SqlDataReader = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
While objDR.Read()
System.Web.HttpContext.Current.Session("Path") = objDR("BasePath").ToString()
End While
objDR.Close()
End Sub
End Class
All I need to do is replace HomeFolder="~\Data" in the user control call with: HomeFolder=<the contents of Session("Path") from the code behind>.
If I use HomeFolder='<%=Session("Path")%>' (or anything else), the user control errors; saying that HomeFolder is undefined as {0} and the label always writes <%=Session("Path")%>.
I have no sytax errors that .net Studio 2013 is highlighting and get no connection or session errors. If I execute the transactional SQL in SQL Studio 2014, I get the path.
Any suggestions would be appreciated to get the path to the user control (I am not bound to even using a session variable if there is another secure method of doing so). I know I am missing something very simple here. Thanks...
Using inline code doesn't work for setting attributes on user controls in the way you are trying to do it. This post explains how to hack it to make it work though. Basically if you want it to work like you have written change your syntax to
Line 52: If Not Page.IsPostBack Then
Line 53: If String.IsNullOrEmpty(Me.HomeFolder) OrElse Not Directory.Exists(GetFullyQualifiedFolderPath(Me.HomeFolder)) Then
Line 54: Throw New ArgumentException(String.Format("The HomeFolder setting '{0}' is not set or is invalid", Me.HomeFolder))
Line 55: End If
Line 56:
For example: <uc1:ViewDataVB1 ID="ViewDataVB1" runat="server" HomeFolder="~\Data" />
(statically assigned to "~\Data")
works perfectly as long as ~\Data is a valid path within the website. I statically set this as above. The user control is rendered perfectly and the
Thank you! I am looking at that solution now, however the label call should still render the contents of the "Path" variable since it is straight text though, correct?
In my eye's, Declarative or Early Loading is really meant for Databinding and shouldn't be used to set values on objects such as usercontrols that the those objects need to preform its tasks. If you want to do it that way then you will need to call Databind
on those objects so the values will be udpated. Then You will most likely have to call what ever loading task that needs to be done afterwards. Databind happens after Load.
si vis pacem para bellum
If this answered your question then please mark it as the answer.
I got sucked into another project but finally got back around to work on this. The DataBind works perfectly and with little effort. I agree with your later comment about sucking the information into the user control but since it is just one variable I
am not motivated to fix what is no longer broken. Thank you very much for your help! Your efforts are much appreciated!
Member
1 Points
4 Posts
Pass session variable to user control
Apr 17, 2015 09:54 AM|quickvfr|LINK
I am attempting to use a session variable to simply map a logged in location for a user. Please see the code below (I have edited out information that is superfluous to the problem as an attempt for clarity):
Index.aspx (default home page with a standard, MS Login. This works fine and throws errors if the entered credentials are wrong and does go to the correct destination once verified):
<asp:Login ID="Login" runat="server" Font-Names="Verdana" Font-Size="Smaller" RenderOuterTable="True" TextBoxStyle-Width="15" DestinationPageUrl="~/Default.aspx" DisplayRememberMe="False" Enabled="True">
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
<TextBoxStyle Font-Size="0.8em" Width="75px" />
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
</asp:Login>
Default.aspx (Destination after login. On the page there is a call to a custom user control that works perfectly if statically assigned as below):
At the top of the file:
<%@ Page language="VB" CodeFile="~/Default.aspx.vb" Inherits="_Default" AutoEventWireup="true"%>
<%@ Register src="UserControls/ViewDataVB1.ascx" tagname="ViewdataVB1" tagprefix="uc1" %>
<%@ Import Namespace="System.Web.Security" %>
Call to the User Control and a label used for testing:
<uc1:DisplayDataVB1 ID="DisplayDataVB1" runat="server" HomeFolder="~\Data" />
<asp:Label ID="Label1" runat="server" Text='<%=Session("Path")%>'></asp:Label>
Default.aspx.vb (code behind):
Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Security
Partial Class Default
Inherits System.Web.UI.Page
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
CreateUserSession()
End If
End Sub
Protected Sub CreateUserSession()
Dim strConn As String = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
Dim CurrentUser As MembershipUser = Membership.GetUser(HttpContext.Current.User.Identity.Name)
Dim CurrentUserID As Guid = DirectCast(CurrentUser.ProviderUserKey, Guid)
Dim sql As String = "Select * from aspnet_UserProfile where UserID ='" + CurrentUserID.ToString() + "'" <-- This is a custom table that has the UserID and Path in it
Dim conn As New Data.SqlClient.SqlConnection(strConn)
Dim Cmd As New Data.SqlClient.SqlCommand(sql, conn)
conn.Open()
Dim objDR As SqlDataReader = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
While objDR.Read()
System.Web.HttpContext.Current.Session("Path") = objDR("BasePath").ToString()
End While
objDR.Close()
End Sub
End Class
All I need to do is replace HomeFolder="~\Data" in the user control call with: HomeFolder=<the contents of Session("Path") from the code behind>.
If I use HomeFolder='<%=Session("Path")%>' (or anything else), the user control errors; saying that HomeFolder is undefined as {0} and the label always writes <%=Session("Path")%>.
I have no sytax errors that .net Studio 2013 is highlighting and get no connection or session errors. If I execute the transactional SQL in SQL Studio 2014, I get the path.
Any suggestions would be appreciated to get the path to the user control (I am not bound to even using a session variable if there is another secure method of doing so). I know I am missing something very simple here. Thanks...
Contributor
2403 Points
828 Posts
Re: Pass session variable to user control
Apr 17, 2015 10:32 AM|DeadTroll|LINK
Using inline code doesn't work for setting attributes on user controls in the way you are trying to do it. This post explains how to hack it to make it work though. Basically if you want it to work like you have written change your syntax to
and then from your code behind call
http://forums.asp.net/t/1351033.aspx?Setting+UserControl+properties+in+ASP+NET+markup
Personally I see this as a .Net Hack.
Normally the way I set user control properties is from the code behind. I don't see any benefit in doing declarativly.
I would do it like this
then in the user control define the property
If this answered your question then please mark it as the answer.
Member
1 Points
4 Posts
Re: Pass session variable to user control
Apr 17, 2015 10:55 AM|quickvfr|LINK
Thank you!
Same error:
Line 52: If Not Page.IsPostBack Then
Line 53: If String.IsNullOrEmpty(Me.HomeFolder) OrElse Not Directory.Exists(GetFullyQualifiedFolderPath(Me.HomeFolder)) Then
Line 54: Throw New ArgumentException(String.Format("The HomeFolder setting '{0}' is not set or is invalid", Me.HomeFolder))
Line 55: End If
Line 56:
For example: <uc1:ViewDataVB1 ID="ViewDataVB1" runat="server" HomeFolder="~\Data" /> (statically assigned to "~\Data")
works perfectly as long as ~\Data is a valid path within the website. I statically set this as above. The user control is rendered perfectly and the
<asp:Label ID="Label1" runat="server" Text='<%=Session("Path")%>'></asp:Label>
Line displays: <%=ResolveUrl(Session("Path"))%>
Member
1 Points
4 Posts
Re: Pass session variable to user control
Apr 17, 2015 11:48 AM|quickvfr|LINK
Thank you! I am looking at that solution now, however the label call should still render the contents of the "Path" variable since it is straight text though, correct?
All-Star
15372 Points
2074 Posts
Re: Pass session variable to user control
Apr 20, 2015 04:46 AM|Krunal Parekh|LINK
Hello quickvfr,
I believe it wont work and will throw error that says Server tags cannot contain <% ... %> constructs. Please see this
http://stackoverflow.com/questions/8738122/server-tags-cannot-contain-constructs
You could set it up so that the path variable is saved in JavaScript variable and then set label on document ready using JavaScript.
Hope this helps.
With Regards,
Krunal Parekh
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.
Contributor
2403 Points
828 Posts
Re: Pass session variable to user control
Apr 20, 2015 10:33 AM|DeadTroll|LINK
It will probably not work as you expect it to. I would write this whole thing as follows:
Page Markup
Page code
In my eye's, Declarative or Early Loading is really meant for Databinding and shouldn't be used to set values on objects such as usercontrols that the those objects need to preform its tasks. If you want to do it that way then you will need to call Databind on those objects so the values will be udpated. Then You will most likely have to call what ever loading task that needs to be done afterwards. Databind happens after Load.
If this answered your question then please mark it as the answer.
Member
1 Points
4 Posts
Re: Pass session variable to user control
Apr 23, 2015 04:49 PM|quickvfr|LINK
I got sucked into another project but finally got back around to work on this. The DataBind works perfectly and with little effort. I agree with your later comment about sucking the information into the user control but since it is just one variable I am not motivated to fix what is no longer broken. Thank you very much for your help! Your efforts are much appreciated!