Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



Reply
How to display current logged in user in a textbox
Old 02-18-2010, 10:46 AM How to display current logged in user in a textbox
Skilled Talker

Posts: 51
Name: N
Trades: 0
Hi,

I am trying to make a Contact form exclusive for Members. So as my first field (ID=username) I would like some VB script so that when the page loads it can identify the user that is accessing the page and display it in a read-only text ready for submission by email (with the email address and text area, don't worry about these because I know how to add these).
Any help will be greatly appreciated.
(here is my Contact us page code, nothing special but I'll give you it just in case)

Code:
  1. <%@ Page Title="" Language="VB" MasterPageFile="~/Members/MasterMembers.master" AutoEventWireup="false" CodeFile="MembersCustomerSupport.aspx.vb" Inherits="Contact_Us_MembersCustomerSupport" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> </SPAN>
  3. <div class="alldivs"> </SPAN>
  4. <asp:LoginView ID="LoginView1" runat="server"> </SPAN>
  5. <AnonymousTemplate> </SPAN>
  6. <p>You must be a Member to benefit from our Member's customer services.</p> </SPAN>
  7. </AnonymousTemplate> </SPAN>
  8. <LoggedInTemplate> </SPAN>
  9. <h1>Members' Customer Support </h1> </SPAN>
  10. <p>Please fill in all the required fields:</p> </SPAN>
  11. <table border="0" style="width: 565px"> </SPAN>
  12. <tr> </SPAN>
  13. <td style="width: 95px"> </SPAN>
  14. <p> </SPAN>
  15. User Name</p> </SPAN>
  16. </td> </SPAN>
  17. <td width="455"> </SPAN>
  18. <p> </SPAN>
  19. <label> </SPAN>
  20. </label> </SPAN>
  21. <asp:TextBox ID="username" runat="server" ReadOnly="True" </SPAN>
  22. ToolTip="The field is uneditable"></asp:TextBox> </SPAN>
  23. <asp:LoginName ID="LoginName1" runat="server" /> </SPAN>
  24. </p> </SPAN>
  25. </td> </SPAN>
  26. </tr> </SPAN>
  27. <tr> </SPAN>
  28. <td style="width: 95px"> </SPAN>
  29. <p> </SPAN>
  30. Email</p> </SPAN>
  31. </td> </SPAN>
  32. <td> </SPAN>
  33. <p> </SPAN>
  34. <label> </SPAN>
  35. </label><asp:TextBox ID="email" runat="server"></asp:TextBox> </SPAN>
  36. <asp:RegularExpressionValidator </SPAN>
  37. ID="RegularExpressionValidator1" runat="server" </SPAN>
  38. ErrorMessage="Your Email is required" </SPAN>
  39. ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" </SPAN>
  40. ControlToValidate="email"></asp:RegularExpressionValidator> </SPAN>
  41. </p> </SPAN>
  42. </td> </SPAN>
  43. </tr> </SPAN>
  44. <tr> </SPAN>
  45. <td style="width: 95px"> </SPAN>
  46. <p> </SPAN>
  47. Message</p> </SPAN>
  48. </td> </SPAN>
  49. <td> </SPAN>
  50. <p> </SPAN>
  51. <label> </SPAN>
  52. </label><asp:TextBox ID="message" runat="server" TextMode="MultiLine"></asp:TextBox> </SPAN>
  53. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" </SPAN>
  54. ErrorMessage="A message is required" ControlToValidate="message" </SPAN>
  55. Display="Dynamic"></asp:RequiredFieldValidator> </SPAN>
  56. </p> </SPAN>
  57. </td> </SPAN>
  58. </tr> </SPAN>
  59. </table> </SPAN>
  60. <p> </SPAN>
  61. <label> </SPAN>
  62. <input ID="Submit" name="Submit" type="submit" value="Submit"> </input></label> </SPAN>
  63. </p> </SPAN>
  64. </LoggedInTemplate> </SPAN>
  65. </asp:LoginView> </SPAN>
  66. </div> </SPAN>
  67. </asp:Content> </SPAN>
I have tried to add the following into the VB script code so that the read-only text equals the current logged in user.

However when I load the page it tells me that, "username has not been declared" even though it is the ID of my texbox:

Code:

PartialClass Contact_Us_MembersCustomerSupport
Inherits System.Web.UI.Page
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
username.Text = User.Identity.Name
EndSub

EndClass

Thanks guys
njccbeach9 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-18-2010, 10:56 AM Re: How to display current logged in user in a textbox
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
So where exactly is User.Identity.Name declared?

Because that would be the name of the user that is logged on to THE SERVER not into your member system.
So for anonymous web access would not actually exist!
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-18-2010, 11:07 AM Re: How to display current logged in user in a textbox
Skilled Talker

Posts: 51
Name: N
Trades: 0
Chrishirst, please can you tell me how to solve this problem. If I don't use
User.Identity.Name then what should I use?

This is not the problem though that Visual Web developer alerts me. It says the 'username' needs to be declared.

Please could you give me some sample.

Thanks
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 02-18-2010, 11:16 AM Re: How to display current logged in user in a textbox
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
where is the object "username" declared? and does it have a property named text?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-19-2010, 04:05 AM Re: How to display current logged in user in a textbox
Skilled Talker

Posts: 51
Name: N
Trades: 0
I don't know, I guess the propery is Text because it is a textbox. What corrections need to be made in VB script and main page to make it run properly?
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 05:33 AM Re: How to display current logged in user in a textbox
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Aah! right I see.
You are thinking that "username" will relate to the HTML input element.

That does not exist until the HTML source is rendered by the browser, and as the ASP.net code is run on the server and completed BEFORE the HTML is sent to the browser, the two have NO interaction until the form is submitted back to the server, then "usename" and the value will exist as a POST (request.form) collection key/value pair

request.form("username") will contain the user entered value you have to process it from there onwards.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-19-2010, 09:28 AM Re: How to display current logged in user in a textbox
Skilled Talker

Posts: 51
Name: N
Trades: 0
I am connecting to a ASP.net textbox as below:

<
asp:TextBoxID="username"runat="server"ReadOnly="True">
</asp:TextBox>


which runs at the server hence runat="server" not an ordinary HTML textbox.

Anyway I did try what you suggested like below:

Code:
PartialClass Contact_Us_MembersCustomerSupport
Inherits System.Web.UI.Page
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
Request.Form("username")
username.Text = User.Identity.Name
EndSub

EndClass


I also tried moving the Request.Form around outside the Page_Load into the declarations and nothing. I changed Form to my form name on my MasterPage form1 and nothing.

The closest I have got to solving it is by using the Dim declaration: It takes the error off the fact username has not been declared and instead says after "Dim username As Text"- It puts a line under the word Text and says, "type expected".

Code:
PartialClass Contact_Us_MembersCustomerSupport
Inherits System.Web.UI.Page
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
Dim username As Text
username.Text = User.Identity.Name
EndSub

EndClass


I know I am very close into solving this one but please tell me is a Dim declaration a good idea or not? What do I need after text to complete teh declaration?

Thanks for your time
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Old 03-13-2010, 11:57 AM Re: How to display current logged in user in a textbox
Skilled Talker

Posts: 51
Name: N
Trades: 0
Don't worry guys I have solved this issue, please see my other thread on how to get the current logged on user's email address:

http://www.webmaster-talk.com/net-pr...nt-logged.html
njccbeach9 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to display current logged in user in a textbox
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31751 seconds with 12 queries