Pass int from aspx.cs to aspx page - c#

I need to pass an int from aspx.cs page to aspx page and show it there
relevant part of example.aspx.cs page:
public partial class example : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected int Id(){
var Id = 318;
return Id;
}
}
Relevant part of aspx page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="example.aspx.cs" Inherits="blahblah.example" %>
.
.
.
<body>
<h1>example.Id()</h1>
.
.
.
How can I edit this? It should be straight foward, but I can't seem to figure it out.

Simply do:
<h1><%=Id()%></h1>
This will display the return value of the method.
You may see: Introduction to ASP.NET inline expressions in the .NET Framework

Injecting Code <%= //some code here %> into the HTML it is possible with ASP.NET. However I will recommend to use a literal control instead to always keep the separation between the controller and the UI. Better may be:
Html:
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
Code:
Literal1.Text = "My Value"

Related

Call code behind method from aspx

I have the following aspx code where I am calling a method from code behind. The result of the code behind method is not getting rendering in the page.
<%# Page Language="C#" AutoEventWireup="false" Src="LeftMenuSrce.aspx.cs" Inherits="LeftMenuSrce" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Home</title>
</head>
<body>
<asp:Table ID ="LeftMenuTable" runat="server">
<asp:TableRow>
<asp:TableCell ID="LeftMenuSrce" OnDataBinding="_getLeftMenuSrc"></asp:TableCell></asp:TableRow>
</asp:Table>
</body>
</html>
Below is my cs code:
public class LeftMenuSrce : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TableCell LeftMenuSrce;
protected System.Web.UI.WebControls.Table LeftMenuTable;
protected void Page_Load(object sender, EventArgs e)
{
LeftMenuSrce.DataBind();
}
protected string _getLeftMenuSrc()
{
string leftMenu;
leftMenu = "LeftMenuNew.aspx";
return leftMenu;
}
}
Even I have tried div instead of asp:tables, but nothing was working.
<div>
<%#_getLeftMenuSrce()%>
</div>
Any clues to overcome this issue? Thanks in advance.
Please note I have referred below links, but nothing helped me.
How to call a code-behind method from aspx page?
Call code behind method from aspx page
ASP.NET - Use variable in .aspx page
Use <%=_getLeftMenuSrc() %>
You must implement your method "_getLeftMenuSrc()" as an event handler.
Example:
protected void _getLeftMenuSrc(object sender, EventArgs e) {}
I guess you simply try to write some text into your TableCell LeftMenuSrce.
One way to do this is writing a OnPreRender event handler like this:
protected void TableCell1_OnPreRender(object sender, EventArgs e)
{
TableCell1.Text = "My text in a cell !!";
// Hint: 'sender' is your table cell ;-)
((TableCell) sender).Text = "My other text in that cell !!";
}
An other way is to fill all your tables cells within the Page_Load event. Like this:
protected void Page_Load(object sender, EventArgs e)
{
TableCell1.Text = "My text in a cell !!";
}
BTW: Don't use tables to build a navigation ;-). Try this link: http://www.w3schools.com/css/css_navbar.asp

How to access .ascx control from my aspx page

I have following code in my ascx page
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="WebUserControl" %>
<li id="firstry" runat="server"> first </li>
And aspx page contains :
<uc:Spinner id="Spinner"
runat="server"
MinValue="1"
MaxValue="10" />
This simply prints my li into my aspx page.. but I want to access the the control in ascx so that i can apply inline or a css class into that control ..Can any one guide me ?
In the Control's code aside add:
public Control FirstTryControl
{
get { return firsttry; }
}
Then you can access it normally from the page...
Spinner.FirstTryControl.Styles.Add(...)
That is a brute force approach, you might want to consider adding properties specific to what you need instead. In the code aside add something like:
private _spinnerClass = string.empty;
public string SpinnerClass
{
get { return _spinnerClass; }
set { _spinnerClass = value; }
}
protected void Page_Render(o,e)
{
Spinner.Attributes.Add('class', _spinnerClass);
}
Then in the page you can define these properties right from the markup:
<uc:Spinner id="Spinner"
runat="server"
MinValue="1"
MaxValue="10"
SpinnerClass="green" />

Not picking up id of control in code behind

I have a basic webform(Default.aspx) that I created using and master page and bizarrely I am not able to access the id of a control in the code behind.
<ul id="topicsList" runat="server">
<asp:Label ID="labUserName" runat="server"/>
</ul>
IDomainObjectRepository rep;
protected void Page_Load(object sender, EventArgs e)
{
rep = new DomainObjectRepository();
rep.GetDomainObjectsByType("Visuals Product Label");
/*labUserName not picked up by intellisense*/
}
any pointers as to why this could be the case
The problem is likely due to your Code-front not being referenced by code behind, or vice versa. Make sure you have the following (double check the CodeBehind and code-behind class marry up):
public class _Default : Page
{
// Your load event
}
<%# Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %>

Simple jQuery Hello World in ASPX

I really dont understand why this simple example doesnt work :S
I have a WebApplication in which I have a script :
function myAlert() {
$("#Button1").click(function () {
alert("Hello world!");
});
}
In my asp page, I have this simple code
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Graph.aspx.cs" Inherits="WebApplication.Graph" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" Width="100px"/>
</asp:Content>
And finally I register the script in the cs :
protected override void OnPreLoad(EventArgs e)
{
Page.ClientScript.RegisterClientScriptInclude("jQuery",
ResolveUrl(#"Scripts\jquery-1.4.1.js"));
Page.ClientScript.RegisterClientScriptInclude("jMyAlert",
ResolveUrl(#"Scripts\MyAlert.js"));
// check if the start up script is already registered with a key
if(!Master.Page.ClientScript.IsStartupScriptRegistered("jMyAlert"))
{
// since it is not registered, register the script
Master.Page.ClientScript.RegisterStartupScript
(this.GetType(), "jMyAlert", "myAlert();", true);
}
}
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "jMyAlert", "myAlert()", true);
}
I dont see what's wrong with this. I tried to include the scrit directly inside the aspx but nothing. I then try onto a simple html page and it works fine.
I want to use a plotting library using jQuery in my page so I'm very far to succeed if such a simple example causes me such a lot of problem...lol
Try checking the debug console within whatever browser you are using to see if "$" is undefined. It sounds like you are missing jquery when using the full ASP.NET approach.
The Id of that button is not going to be #Button1 because of the use of the master page. Try viewing the source to see what I mean.
To solve this, you will need to be able to see the actual Id in the JavaScript.
Something like this in your Page_Load method:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"Button1Id", string.Format("var Button1Id = '{0}';", Button1.ClientID), true);
Will create the following in your page:
<script type="text/javascript">
//<![CDATA[
var Button1Id = 'Button1';//]]>
</script>
Which then means that your myAlert method will need to look like this:
function myAlert() {
$("#" + Button1Id).click(function () {
alert("Hello world!");
});
}

Pass a variable from Site.Master.cs to Site.Master

I'm new to C# web development. Please bear with me.
I'm trying to reference a param from Site.Master.cs in Site.Master, so that in Site.Master page, I can do:
<%
if (someParam == true) {
%>
some HTML code
<%
}
%>
The param someParam is the one I want to pass from Site.Master.cs.
Thanks!
Use a class-level field (or better: property) with visibility at least "protected" (not private).
You can use a LiteralControl.
<asp:Literal ID="myControl" runat="server">
// Some html here.
</asp:Literal>
Then in the codebehind - set the Visible property appropriately.
Follow the below code example.
Site.Master.cs
protected bool someParam
{
get;
set;
}
Site.Master page
<%
if (someParam) {
%>
some HTML code
<%
}
%>

Categories