Using the value from a login view control - asp.net

I am trying to get the name (value) from a login view status contraol when a user in logged into my asp.net website. I want to put this value or login name into a textbox when the page loads.
Any help would be greatly appreciated.
Marlon Gowdie

Assuming the user has been authenticated then you can get the current user from the User property of the current Page. More than likely this is what the LoginView control is using internally to get the current user information.

here is simple example how to get the login name using login status control...
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Login1_LoggedIn(object sender, EventArgs e)
{
string name = Login1.UserName;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" OnLoggedIn="Login1_LoggedIn">
</asp:Login>
</div>
</form>
</body>

Related

Preselecting Dropdown value in .Net

I have a webform whose fields change based on the value of a dropdown. Is there a way I could have a link to this page select one of these values when it loads.
For example, if the form was about traveling, the external "car" link would autoselect "car" in the dropdown.
You could use a QueryString parameter, such as ?preselect=car and then act on the value of that parameter in your code:
<%# Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
private void page_load(Object sender, EventArgs e)
{
if (Request.QueryString.AllKeys.Contains("preselect"))
{
MyDropDownList.Items.Cast<ListItem>()
.Where(li => li.Value == Request.QueryString["preselect"].ToString())
.First().Selected = true;
}
}
</script>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList runat="server" ID="MyDropDownList">
<asp:ListItem Value="asdf">Something</asp:ListItem>
<asp:ListItem Value="car">Car</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>

Compiler Error Message: CS0103: The name 'Test1' does not exist in the current context

I am completely new to programming in ASP.NET and do not understand why the error "Compiler Error Message: CS0103: The name 'Test1' does not exist in the current context" occurs on this basic piece of code.
Below is the code that I am trying to compile -
<%# Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Test1"/></asp>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Test1.Text = "Hello World; the time is now" + DateTime.Now.ToString();
}
</script>
</div>
</form>
</body>
</html>
Any assistance on why I'm receiving this error message would be greatly appreciated.
You need to include the runat="server" attribute on your asp control, otherwise your code-behind does not recognize it.
Note, you can add this attribute to plain html elements as well.
<label id="myLabel" runat="server">text</label>
myLabel.InnerText = "stuff";
In addition to including the runat tag, you didn't close the tag correctly
<asp:Label runat="server" ID="Test1"></asp:Label>
or
<asp:Label runat="server" ID="Test1"/>

how to make a asp.net page without any payload just a string in the sourcecode?

normaly a asp.net page looks like:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="my text"></asp:Label>
</div>
</form>
</body>
</html>
and then i get displayed "my text".
the problem is, now i need to make a page and just display the "my text" without the html stuff in the background. so when i look in the sourcecode it should just be my text
of course i need the asp.net in the background to change the label text.
is this possible?
Just delete everything except the page tag, and on Page_Load write my text in the response
In the aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
In the .cs:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("My text")
}
Or you can add a literal to the page (set it's value in the code behind) and remove the html stuff like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Foo.aspx.cs" Inherits="Foos" %>
<asp:Literal ID="literal1" runat="server" />

Removing READONLY attribute from textbox using client side code

I have a multiline textbox that by default, is set to ReadOnly. I would like a button on the page to change the control to allow it to be edited. I would like this code to run on the client side because the page renders slowly and I'd like to avoid the post back.
The problem I'm having is the javascript code that I wrote to remove the readonly attribute appears to have no effect. I posted a stripped down example that illustrates the problem for your review.
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script type="text/javascript" language="javascript">
function EnableEditing() {
var e = document.getElementById("<%=TextBox1.ClientID%>");
var result = e.removeAttribute("readonly");
alert(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div> </div>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" TextMode="MultiLine">test</asp:TextBox>
<input id="Button1" onclick="EnableEditing()" type="button" value="Remove RO" />
</form>
</body>
</html>
TextBox1 is the server side id,
try
var e = document.getElementById("<%=TextBox1.ClientID%>");
var result = e.removeAttribute("readonly",0);
or if you dont want the caseinsensitive search
var result = e.removeAttribute("readOnly");//note upercase Only
Use var e = document.getElementById("<%=TextBox1.ClientID%>");
Also, if you want to read the modified text on postback, you can't set the readonly attribute on the server control. You have to set it on the client only, as in: TextBox1.Attributes("readOnly") = "readOnly";

Is SearchString acting like a property?

Is SearchString acting as property of Page class?
Here is code,
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public string SearchString
{
get { return txtSearch.Text; }
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Button Search Typed</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblSearch"
Text="Search:"
Runat="server" />
<asp:TextBox
id="txtSearch"
Runat="server" />
<asp:Button
id="btnSearch"
Text="Go!"
PostBackUrl="ButtonSearchResultsTyped.aspx"
Runat="server" />
</div>
</form>
</body>
</html>
It is a property of your page class. This is not the page class. Your class inherits from the main page class.
Also, you need to be careful how you use thist kind of thing. Remember, ASP.Net pages are stateless, just like with other platforms. That means every time you do a postback, including just to handle simple server events like button clicks, you are working with a new instance of the class. Any previous SearchString value was lost.
It's a property of the class generated for your ASPX file, which inherits from System.Web.UI.Page. It's not added to that class itself, of course.

Resources