Pass a variable from Site.Master.cs to Site.Master - c#

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
<%
}
%>

Related

Get variable value from code behind and use in aspx page

Variable in .aspx.cs file
public string apmt_val { get; set; }
I want to use this in .aspx page as
if(<%#apmt_val%> == "Some Value"){
// Do something
}
else{
// Do something
}
field must be declared public or protected
Then, to call it on the ASPX side:
<%=apmt_val %>
Note that this won't work if you place it on a server tag attribute. For example:
<asp:Label runat="server" Text="<%=apmt_val %>" />
Your Solution
<%if (this.apmt_val == "Some Value") { %> //logic <%}%>
<%if (this.yourvar == "value") { %> //set the logic here <%}%>

Use model on SharePoint application page .aspx

I am working on SharePoint to create a Feedback questionnaire form using an application page that is basically a aspx page.
I wish to do this by emulating MVC as far as possible. I've set up my model in the code-behind:
public List<QuestionViewModel> modelQuestions = new List<QuestionViewModel>();
Next, I need to display each question and an appropriate input depending on the question type (e.g. single line, multi line, single selection, multiple selection).
I've got it displaying the questions correctly:
<fieldset class="what-went-wrong">
<% for (int i = 0; i < modelQuestions.Count; i++) { %>
<p>
<label for="QuestionText">
<% if (modelQuestions[i].Required) { %>
<span class="req-field indicator">*</span>
<% } %>
<%= modelQuestions[i].QuestionText %>
<% if (modelQuestions[i].Required) { %>
<span class="req-field right">* Required field</span>
<% } %>
</label>
</p>
<% } %>
</fieldset>
This give's me the question text. I'm now trying to construct the appropriate input, but this <% %> tags is not working for this:
<% if(modelQuestions[i].QuestionTypeId == QuestionType.SingleLine) { %>
<input id="modelQuestions_<% i %>" name="modelQuestions[<% i %>]" type="text" placeholder="<% modelQuestions[i].Placeholder %>" />
<% } %>
I can't seem to get it to construct the html element using details from the model (in the value for id, name, placeholder etc.)
Also, I've no idea how to go about posting this back to the server when I get to that point.
Is there any merit in continuing? Are there other controls/methods more appropriate to use in this case with aspx?
You cannot generate HTML markup like this. Even data-binding expressions will not help, because they bind ASP.NET controls' attributes values, not the plain output HTML in the page.
You should generate the markup in the "code behind", like this:
Page markup:
<div id='AnswersPanel'>
<div>
Page code behind:
protected void PageLoad(...)
{
AnswersPanel.InnerHtml = "";
AnswersPanel.InnerHtml += string.Format("<input id='modelQuestions_{0}' name='modelQuestions[{0}]' type='text' placeholder='{1}' />",
i.ToString(),
modelQuestions[i].Placeholder);
}

Pass int from aspx.cs to aspx page

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"

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

How to create a runat server DIV in the behind code ? ASP.NET & C#

I'm creating multiple DIVs according to database records at my C# behind code. How could I make them runat server ?
Thanks in advance ..
Create and add ASP.NET Panels.
The code
<asp:Panel id="abc" runat="server">
is exactly the same as if you do:
<div id="abc" runat="server">
They render the same, but it's the functionality with other WebControls that the Panel is most used, and the Panel web control gives you more control under code-behind as it exposes more properties.
If you want to access a DIV on serverside, you could also add runat="server". It will be created as HtmlGenericControl.
That's not necessary, just create a HtmlGenericControl and add it to the controls collection:
HtmlGenericControl div = HtmlGenericControl("div")
div.Id = "myid";
this.Controls.Add(div);
Use a custom control that pulls the data and renders it how you would like. Kind of like this:
public class MyDivControl : System.Web.UI.Control
{
private System.Data.DataTable tblMyResults;
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
// Get your Data (or do it on Page_Load if you'll need it more than once
if (tblMyResults != null && tblMyResults.Rows.Count > 0)
{
int iIndex = 0;
foreach (System.Data.DataRow rItem in tblMyResults.Rows)
{
writer.WriteLine("<div id=\"{0}_{1}\">", this.ClientID, iIndex++);
//Whatever content you want here using your rows.
writer.WriteLine("</div>");
}
}
}
}
Then just drop the control on the page where you want it to render.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Solution.Web.Presentation.pub._default" MasterPageFile="~/ui/master/main.master" %>
<%# Register TagPrefix="custom" Namespace="MyNameSpace" Assembly="MyProjectAssembly" %>
<asp:Content runat="server" ContentPlaceHolderID="cntMain">
<custom:MyDivControl runat="server" />
</asp:Content>
You can use Repeater Control
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div id="box<%# Eval("ID")%>" runat="server"></div>
</ItemTemplate>
</asp:Repeater>
and bind data from codebehind

Categories