This is my default.aspx file
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Welcome to sample website:)
</div>
<p>
</p>
<p>
<asp:LoginName id="LoginName1" runat="server" FormatString="Welcome,{0}" />
</p>
<p>
</p>
</form>
</body>
</html>
I am able to display the doaminname\username but I need to display only the username . i am confused how do I go ahead with this.Can anyone help me with this?
Add the below code in your page_load
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
if (windowsIdentity == null)
throw new InvalidOperationException("WindowsIdentity is null");
string nameWithoutDomain = windowsIdentity.Name.Split('\\').Last();
LoginName1.Text = String.Format("Welcome,{0}", nameWithoutDomain)
And if you want a helper class you can refer to this Built-in helper to parse User.Identity.Name into Domain\Username
Related
I am trying to get get model in aspx page. How can i get it. is anything i have to do in config file? PLease help me.
<%# Page Language="C#" AutoEventWireup="false" CodeBehind="Report.aspx.cs"
Inherits="IBATechnologies.IBA.Main.Views.Report" %>
<%# page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
Model // here i cant find Model "the name Model is not in current
context"
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
i'm not sure but you can use DynamicViewPage instead ViewPage.
may be this link will help you.
https://learn.microsoft.com/en-us/aspnet/mvc/overview/views/dynamic-v-strongly-typed-views
how to set or get html elements inner text or html controls value in Razor syntax.
i know how we can do this in aspx file using Runat="server" attribute like this
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><%Response.Write(Page.Title); %></title>
<script runat="server">
protected void Button1_ServerClick(object sender, EventArgs e)
{
p1.InnerText = "Hello " + textbox1.Value; //get textbox value and set in html p tag
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="textbox1" runat="server" /><br />
<input type="button" id="btn" runat="server" value="Click Me" onserverclick="Button1_ServerClick" />
<p id="p1" runat="server">see result here</p>
</div>
</form>
</body>
</html>
Basically something like this...
View (SomeAction.cshtml):
...
<p>ViewBag.SomeValue</p>
...
Controller:
...
[HttpGet]
public ActionResult SomeAction()
{
ViewBag.SomeValue = "Hello";
return View();
}
...
However you should learn much about approach which is used to build applications using ASP.MVC. It's completely different than ASP.NET WebForms. WebForms technology uses events while ASP.MVC uses Model View Controller. You have to change your mindset.
I have a user control in my project (asp.net) that have an image. It has below code:
<%# Control Language="C#" ClassName="Header" %>
<asp:Panel ID="Panel1" runat="server">
<img alt="The Night Owl"
src="../Images/bookshelf.jpg"
width="800" height="110"/>
</asp:Panel>
<asp:Panel id="menuPanel" runat="server">
Home |
Titles |
Authors |
Publishers
</asp:Panel>
I can see image in user control but when i use it in my default.aspx page, it do not show image. my default.aspx code is:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register src="~/controls/Header.ascx" tagname="Header" tagprefix="uc1" %>
<!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>The Night Owl</title>
<link href="styles.css" rel="Stylesheet" type="text/css"/>
</head>
<body>
<form id="form1" runat="server">
<uc1:Header ID="Header1" runat="server" />
<h1><span lang="en-us">Home Page</span></h1>
<asp:Panel runat="server">
Welcome to The Night Owl, where all of
your technical needs are met!
</asp:Panel>
</form>
</body>
</html>
Because of src="../Images/bookshelf.jpg" in the default.aspx page it can not be load, user controls dont reference images well unless you specify the whole path or the page and user control are in the same folder,
Your UC is in a sub folder but Default page not,
Change the src something like ~/subf/subf1/img.jpg.
If that page and UC are in a same folder your src will work.
But if they are not in the same folder you should change src as I mentioned.
There is something about Path.
I'm trying to add some share this javascript in between the head tags of an asp.net page but only if the page is not secure (!Request.IsSecureConnection). How do I get the code in the head tags to check for secure connection and then write the javascript if not secure. I've tried using <% %> blocks and RegisterStartupScriptBlock and it's not working
UPDATE:
Was able to get it to work using this in the Page_Load
if(!Request.IsSecureConnection)
{
HtmlGenericControl Include = new HtmlGenericControl("script");
Include.Attributes.Add("type", "text/javascript");
Include.Attributes.Add("src", "http....");
this.Page.Header.Controls.Add(Include);
}
This works for me:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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">
<% if (!Request.IsSecureConnection)
{ %>
<script type="text/javascript">
onload = function() {
alert('Page is not secure') };
</script>
<% } %>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%if (!Request.IsSecureConnection)
{%>
<script ..........> </script>
<%}%>
This didn't work?
Update From your comments this didn't work. I'm guessing it has to do with something you are doing in your code behind. Did you try calling RegisterClientScriptBlock from your code behind? If you could post your aspx and code behind we might be able to help more.
I keep getting these requests for 'make me a tool to do xyz' for a web app we're putting up.
So after the third one, I realized it'd be easier to lump them all together and use a master page.
I've got a user control called MessageCenter I use for error, success, and informational messages, and so I dropped that on the master page.
<%# Master Language="VB" CodeFile="tfMasterPage.master.vb" Inherits="tfMasterPage" %>
<%# Register Src="MessageCenter/msgCenter.ascx" TagName="msgCenter" TagPrefix="uc1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>INSERT TITLE HERE</title>
<link href="Stylesheets/EogTool.css" rel="stylesheet" type="text/css" />
<link href="stylesheets/TF_Main_Styles.css" rel="stylesheet" type="text/css" />
<link href="stylesheets/TF_Print_Styles.css" rel="stylesheet" type="text/css" media="print" />
</head>
<body style="background-color: #eeeeee">
<form id="form1" runat="server">
<div class="page">
<div class="headerArea">
<div class="LogoImg">
<img alt="Transparency Florida" src="images/TF_Logo.jpg" /></div>
<div class="SealImg">
<img alt="Shining the Light on Florida's Budget" src="images/TF_Seal.jpg" /></div>
</div>
<div class="content">
<h1>
FIS - EOG Table Maintenance</h1>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="content">
<div>
<uc1:msgCenter ID="MsgCenter1" runat="server" />
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
...
Normally, when the msgcenter is on a regular aspx page, I call its method and stuff from the codebehind as in this sub:
...
rtn = dal.deleteRow(CInt(e.CommandArgument), currentTab())
If Not IsNumeric(rtn) Then
MsgCenter1.addMessage("An Error occured deletion" & rtn, , , , "E")
Else
MsgCenter1.addMessage("Delete Successful", , , , "S")
End If
bindGrid()
MsgCenter1.Visible = True
End Sub
But when I try to do that from the asp:content thing on the page using the masterpage, it tells me that msgCenter1 is not declared. It's some sort of scope issue.
I've read about using findcontrol like
ctype(master.findcontrol("tbWhatever"), textbox).text = "FOO"
But when I try to cast to my user control, it complains because it once again, isn't declared.
I feel as though I'm just missing one piece of the puzzle, but it's been eluding me since around 4PM yesterday.
Any advice, pointers, or links would be most appreciated.
Thanks.
First add this directive to the content page you want to access the master page
<%# MasterType VirtualPath="~/NameOfMasterPage.master"%>
Second, On the master page setup a public propery that returns the control you want to access
public Label MasterLabel
{
get
{
return lblMaster;
}
private set
{
//do nothing
}
}
Lastly just access the control in the content page like so
Master.MasterLabel.Text = "Hello from the content page!";
I know your question has been answered and this doesn't apply to it, but I noticed you're passing in 1 length characters for your "MessageCenter" control. I would use an Enum instead of a string to make your code a little less brittle. As it stands now you can pass "fart" in as a parameter and it will compile just fine. An Enum will give you some compile time checking and avoid any issues at runtime.
Examples:
Message.Success
Message.Error
Message.Warning
this is what i'd been used.
Master.FindControl("ControlID").Visible = false;