Set or get html tags value or innerhtml in razor view - asp.net

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.

Related

Dsiplay Login Name without the Domain name (No Java Script)

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

asp.net using jQuery Ajax - Parent page dissapears onClick of a Button in Child page loaded inside the Parent page dynamically

We are loading a child page in the parent page parent.aspx as follows -
$("#result").load("Child.aspx");
The Child.aspx consists of an asp:Button.
Problem -
OnClick of asp:Button is causing Child.aspx to be opened instead of being embedded in Parent.aspx as before the click event.
Expected -
Child.aspx should remain embedded in Parent.aspx onClick of the Asp:Button
What we tried: Added an UpdatePanel & ContentTemplate but the Child.aspx page is still getting refreshed.
[Snap1] Parent.aspx:
Note: onClick of Button will load another page i.e Child.aspx page within the <div id="result"></div> of the Parent page itself using
$("#result").load("Child.aspx");
[Snap2] Parent.aspx:
Note: The Child.aspx page gets loaded inside the Parent.aspx page.
The Child.aspx page consists of an Asp.net button named "ChildPageButton".
onClick of ChildPageButton reloads the page and causes the Parent.aspx page to dissapear.
[Snap3] Child.aspx:
[Snap3]
Code -
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Parent.aspx.cs" Inherits="NewLab.Parent" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/themes/redmond/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js" type="text/javascript"></script>
<script src="/js/jquery.mousewheel.js" type="text/javascript"></script>
<script src="/js/jquery.colorbox.js"></script>
<title></title>
<script type="text/javascript">
function openSection() {
alert("openSection");
//Loads the Child.aspx page inside the Parent.aspx result div.
$("#result").load("Child.aspx");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager
runat="server">
</asp:ScriptManager>
<h1>Parent Page</h1>
<asp:Button ID="lnkbtn" runat="server" Text="Button" OnClick="Button1_Click" Height="51px" Width="218px" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="result"></div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NewLab
{
public partial class Parent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//This invokes the javascript function openSection
if (!ClientScript.IsStartupScriptRegistered("openSection"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"openSection", "openSection();", true);
}
}
}
}
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="NewLab.Child" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
function calljs() {
alert("click");
event.preventDefault();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="background-color:blue; height:400px">
<h1>CHILD PAGE LOADED!</h1>
<input type="button" value="htmlbutton" />
<asp:Button ID="Button1" runat="server" Text="AspPageButton"/>
</div>
</form>
</body>
</html>**/
The code above helps to reproduce the problem.
It can be observed that onClick of ChildPageButton reloads the page and causes the Parent.aspx page to dissapear.
The Child.aspx page appears as an individual page, rather it should remain embedded inside the Parent.aspx page as depicted in Snap2.
Thanks in advance.
The problem is that when your child button causes a postback, it's a postback of the child page, so the child page is what gets reloaded. I recommend you consider ASP.NET User Controls instead of JQuery to achieve what you're aiming for here.

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" />

UserControl Raise Events

I add an onfocus event to a web user control (.ascx) expection it to raise event when it gets focus but it doesn't. Is it not intended to work this way and how can I get it to raise the event? Here is sample below, but it doesn't work.
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb"
Inherits="RaiseEventUserControl.WebUserControl1" %>
<div style="padding: 5px; background-color: #C0C0C0">
TB1:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
TB2:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
<%# Register Src="WebUserControl1.ascx" TagName="WebUserControl1" 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></title>
<script type="text/javascript">
function RaiseEvent(obj) {
alert("Event was raised for: " + obj.id)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" onfocus="RaiseEvent(this)" />
<br />
<uc1:WebUserControl1 ID="WebUserControl12" runat="server" onfocus="RaiseEvent(this)" />
</div>
</form>
</body>
</html>
This can be achieved using jquery. In your head element, add the following:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[name^="WebUserControl"]').focusin(function () {
$(this).val("i have focus");
});
});
</script>
What this does is, grabs the input elements whose name starts with "WebUserControl" and adds a "got focus" event. The handler for that event places "i have focus" as the value for that input element.
The way that this would be rendered to the page is that a "onFocus" attribute would be added to the div that represents the control. This is not something that supports focus. You might add the "onFocus" item to say your first text field in the control or something similar to be able to raise the event if needed.

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