This question most likely has an easy solution, but I cannot figure it out. I have a .aspx page with the following code:
<%# Page Title="Update ASV Information" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UpdateASV.aspx.cs" Inherits="COAF_Process_to_ASV_Relation_Tool.UpdateASV" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" >
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<asp:Button runat="server" ID="asv_update" OnClick="asvUpdate_Click" Text="Update ASV" />
</asp:Content>
I want to be able to write text from the C# code behind this page (UpdateASV.aspx.cs). Whenever I try:
Response.Write("some text");
It puts the code behind the content2 placeholder. I want it inside. Is there an easy way to do this?
Response.Write directly modifies the response. You don't want to do that in your web forms application. You should be modifying the contents of a Label, Literal, or PlaceHolder control from the code-behind.
Use Label to put text into the page
Use Literal to put raw html into the page
Use PlaceHolder to add new controls to the page dynamically.
Either way, the placement of the control(Label, Literal, or PlaceHolder) on your page determines where on the page your output will be rendered.
If you are determined to stick to server side controls:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
Literal myText = new Literal()
{
Text = "some text"
};
Content2.Controls.Add(myText);
}
}
Otherwise, just put the code inline
<%=Response.Write("some text") %>
Related
First of all, I'm very new to asp.net (Few days worth of experience).
I am using the default content provided by Visual Studio 2013 to work on a Web Forms Application.
Scenario: I would like to change the text of a Histamine(h2) after clicking a button.
Problem: I am using the default master page provided by Visual Studio but it is not loaded correctly on postBack. From what I can tell the resources it uses for it's layout can't be accessed? I'm not entirely sure what I'm doing wrong. If someone could enlighten me I would very much appreciate it.
My code so far:
Default2.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content runat="server" ContentPlaceHolderID="MainContent">
<h2 id="h1" runat="server">Change me please.</h2>
<asp:Button ID="b1" Text="Submit" runat="server" OnClick="dothis" />
</asp:Content>
Default2.aspx.cs:
public partial class Default2 : Page
{
protected void dothis(object sender, EventArgs e)
{
Header.InnerHtml = "Hello world.";
}
}
See change html tags text , on server side (C#, ASP.NET) for the code you need to implement. In your case:
h1.InnerHtml = "Hello";
As an alternative, instead of using an <h2> server control, you can wrap something like a label or literal control within the header tag, like this:
<h2><asp:LiteralControl runat="server" id="HeaderLiteral">Change me please</asp:LiteralControl></h2>
Then in your code-behind you can do this:
HeaderLiteral.Text = "Hello World!";
Personal preference but I generally shy away from Server Controls. No particular reason, but offering this suggestion for completeness sake.
Ok this an odd way of doing this, I know I should have done it on Page_Load in every pages when using Masterpage, but is there a way around this issue? I don't want to rebuild the pages if I can help it,if I can only insert a title on <asp:content></asp:content> it would be easier.
If anyone has run into this or maybe have a suggestion of a good way to do it , I know jquery can do it document.title ='' but I heard it's not SEO friendly.
Thanks!
You can still set the title in each page that's using a MasterPage. In markup: -
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
Or in code: -
protected override void OnLoad(EventArgs e)
{
Page.Title = "Your Title";
base.OnLoad(e);
}
you can place a contentplaceholer in your master page as..
<asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder>
After that In content page add its reference as..
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title> Your title goes here. </title>
</asp:Content>
more simple solution in Master page
<%: Page.Title %> - the main title goes here
in content page first line of it
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
I know this is an older question but what definitely works for me is to set a Literal control in the title tag of the MasterPage like this:
<title><asp:Literal runat="server" id="pagetitle" Text="MyTitle"></asp:Literal></title>
Then, in the content page, put this in the Page_Load method:
Literal myTitleLiteral = (Literal)Master.FindControl("pagetitle");
if (myTitleLiteral != null)
{
myTitleLiteral.Text = "Test Title";
}
So I've noticed that PreviousPage and Request.Form don't work if my SOURCE page has the TextBox's and such within an asp:Content block (master pages)
Is there a workaround or am I not understanding something?
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" MasterPageFile="Menu.master" %>
<asp:Content ID="pageHead" ContentPlaceHolderID="pageHead" Runat="Server">
<title>Whatever</title>
</asp:Content>
<asp:Content ID="pageContent" ContentPlaceHolderID="pageContent" Runat="Server" >
<asp:TextBox runat="server" ID="txtTester" Text="YES"></asp:TextBox>
<asp:Button runat=server ID="btnTest" PostBackUrl="~/Process.aspx" />
</asp:Content>
Target Codebehind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ltlDebug.Text = Request.Form["txtTester"];
//Doesn't get the var, only if I remove it from master page model in source page
}
}
This is similar to How to read input value from the Request.Form collection by input name.
A control's Form key is the unique id of the control. When you nest your control inside of a containing control (such as Content) that implements INamingContainer, ASP.NET will use the nesting control's name to qualify the nested control's id (and name).
So the Form key will be a concatenation of the Content and the TextBox control's IDs - something like "pageContent$txtTester". You can look at the page's generated markup and use whatever the text box's id value.
You can use response.redirect("yoursite.aspx?parameter1=value"); for that.
This is my first SO post, so please forgive any faux pas. I feel as though I've tried everything on every post related to this error (disable viewstate, noCache, etc) and I'm at my wits end.
I have a blank project with a single master page, a single page, and a single control.
The page (aspx) loads using the master page. This works fine. There is a button on the page which loads the control (the ascx) onto the aspx in the section called divRightMainView. This also works fine (which is where my problem seems to differ from all others I've found...). There is a button on the ascx which is supposed to call the code-behind of the ascx - this is where I get the "state information" error.
This is the aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Outer.aspx.cs" Inherits="TestProject.Outer" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
function loadUserView(viewToLoad) {
PageMethods.RenderControl(viewToLoad, onSuccess, onError);
}
function onSuccess(results) {
alert(results);
var command = results.split('##')[0];
if (command == 'loadView') {
var htmlToLoad = results.split('##')[1];
$get('divRightMainContentView').innerHTML = htmlToLoad;
}
}
function onError(error) {
alert('error ' + error);
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div id="leftSideContent">
<input type='button' id="clickToLoadASCX" value="Click Me" onclick="loadUserView('/inner.ascx');"/>
This is the "Outer.aspx" page which will hold the ascx after the button is clicked
</div>
<div id="divRightMainContentView">
</div>
</asp:Content>
Aspx code behind (which renders the ascx)
[WebMethod]
public static string RenderControl(String controlName)
{
Page page = new Page();
Control control = page.LoadControl(controlName);
HtmlForm form = new HtmlForm();
form.Controls.Add(control);
page.Controls.Add(form);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return "loadView##" + writer.ToString();
}
This is the ascx called inner.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="inner.ascx.cs" Inherits="TestProject.inner" %>
<div>
This is the inner text. When I click the button below I should proceed to inner.ascx.cs to the "PageBehindCall_Submit".
<asp:Button ID="innerButton" runat="server" onclick="PageBehindCall_Submit" Text="Submit"/>
</div>
And finally, the ascx code-behind
protected void PageBehindCall_Submit(object sender, EventArgs e)
{
string str = "This call does not work!";
}
I'm hoping to be able to use the "PageBehindCall_Submit" to process data, grab inputs from the ascx, etc. Let me know if there is a way to make this happen, or another possible work-around? Any help would be appreciated!
Why not load the ascx on page load of the ascx but turn the visibility off rather than manually rendering user control html. This is very weird that it does not hook up with the event in ascx as the control belongs to ascx. But I think you should add it in aspx then try to turn on visibility whenever you need it.
I am trying to pass text to a Literal, the parent.master has the ContentTemplateID's and the Child.master has the contentID's, one of them is the literal of form
<asp:Literal runat="server" ID="myLiteral"></Literal>
I am trying to pass it from a UserControl.cs file like this
gooleTrackin = track.GetTrack(track.OrderType.YAHOO);
Literal mpLiteral = (Literal)this.Parent.Page.Master.FindControl("myLiteral");
mpLiteral.Text = gooleTrackin.ToString(); //nullReference here
But it is giving me NulLReference in the last line.
By the way I do not have access to the .cs files of the master pages, it has to be done through the UserControl.
Thank you
ADDITIONAL CODE (THIS IS LOCATED IN THE CHILD.MASTER)
<%# Master Language="C#" AutoEventWireup="true" MasterPageFile="../common/child.master" %>
<%# Register Src="UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="inner"><uc1:UserControl ID="theRceipt" runat="server" Visible="true"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BottomContentTemplate" Runat="Server">
<div style="margin-bottom:30px;">
<asp:Literal ID="myLiteral" runat="server"></asp:Literal>
Back to Home Page
</div>
</asp:Content>
When master page is used, controls from master page are merged into the control hierarchy of page so this can be an issue while finding controls. I will suggest that you try following and see if it works:
Literal mpLiteral = (Literal)this.Page.FindControl("myLiteral");
OR
Literal mpLiteral = (Literal)this.Parent.FindControl("myLiteral");
Otherwise, you may have to try recursive find - see http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl
However, I will rather recommend a alternate way - assuming that you have defined your literal control in Child.Master and Child is the name of code behind class for master, you can add an helper method in it such as
public void UpdateMyLiteral(string text)
{
myLiteral.Text = text;
}
And in user control code, invoke the helper method such as
((Child)this.Page.Master).UpdateMyLiteral("xyz");
Note that we are casting master to its code-behind class.