Is there a way to use a session variable in CSS style such as to set width % dynamically.
width:<%Convert.ToString(Session["DaysAvailable"]);%>%;
Yes, as #JB11 confirmed. Here's a working example.
The div's width is set to 25% on PageLoad() only.
It's set to 75% on button Click() event.
SessionCSS.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="SessionCSS.aspx.cs" Inherits="SessionCSS" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.my-class{
background-color: green;
color: white;
width: <%= Session["DaysAvailable"].ToString() %>%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="my-class">
Days available
</div>
<br/>
<asp:Button ID="btnUpdateDays" runat="server" Text="Update days" OnClick="btnUpdateDays_Click" />
</form>
</body>
</html>
SessionCSS.cs (code behind)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SessionCSS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if( !Page.IsPostBack)
{
Session["DaysAvailable"] = 25;
}
}
protected void btnUpdateDays_Click(object sender, EventArgs e)
{
Session["DaysAvailable"] = 75;
}
}
Result:
Note:This work only when CSS is directly inside the Page Markup and not in external Stylesheet.
Related
In my project there are one video tag and two buttons. I am trying to do that when I click button1 then video starts playing and when I will click another button then another video starts playing. These two videos are in video folder inside my project.
here is the test.aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="css/test.css" media="all" />
</head>
<body>
<form id="form1" runat="server">
<div>
<video id="videoplr" width="500" controls="controls" runat="server"></video>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
Here is the code behind c# file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
videoplr.Attributes["src"] = "C:\\Users\\Punam\\Desktop\\Project\\videos\\personal_talk.MP4";
}
protected void Button2_Click(object sender, EventArgs e)
{
videoplr.Attributes["src"] = "C:\\Users\\Punam\\Desktop\\Project\\videos\\Tags.MP4";
}
}
When I am clicking buttons then video is not playing. I am new bie in asp.net.
I am developing a website using ASP.NET and used jQuery to implement the tabs in the page.
I got the design perfectly. but problem is I have a treeview on each tab. When user click a node the page should redirect to another page. Currently I am using response.redirect method which worked perfectly. But after I put the treeview control inside this jQuery tab post back occurs but instead of redirecting again it loads the same page with the tabs. I put a break point on SelectedNodeChanged event of the treeview but compiler even didn't reach to the breakpoint. What went wrong?
Here is the code
<link rel="stylesheet" href="Content/jquery-ui.css">
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/jquery-ui.js"></script>
<script>
$(function () {
$("#tabs").tabs();
});
</script>
<div id="tabs">
<ul>
<li>Test1</li>
</ul>
<div id="tabs-1">
<asp:TreeView ID="tvTest" runat="server" OnSelectedNodeChanged="tvTest_SelectedNodeChanged">
<NodeStyle CssClass="tree" />
</asp:TreeView>
</div>
</div>
Here is the selectednodechanged event
protected void tvTest_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Redirect("~/Display.aspx",true);
}
These are my Test pages. When the node changes, I.E. I click on a different node, it redirects me to the said page.
TreeView.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TreeTest.aspx.cs" Inherits="WebApplication.TreeTest" %>
<!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>
<link rel="stylesheet" href="Scripts/jquery-ui.css" />
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
$("#tabs").tabs();
});
</script>
</head>
<body>
<form runat="server">
<div id="tabs">
<ul>
<li>Test1</li>
</ul>
<div id="tabs-1">
<asp:TreeView ID="tvTest" runat="server" OnSelectedNodeChanged="tvTest_SelectedNodeChanged">
<NodeStyle CssClass="tree" />
<Nodes>
<asp:TreeNode Text="Test1"></asp:TreeNode>
<asp:TreeNode Text="Test2"></asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>
</div>
</form>
</body>
</html>
TreeView.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication
{
public partial class TreeTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void tvTest_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Redirect("~/Display.aspx", true);
}
}
}
I have an ASP.net WebForm. The markup is like:
<div>
<input type="text" id="input" runat="server" value=" " />
<asp:Button Text="send" OnClick="btnsend_Click" ID="btnsend" runat="server" />
</div>
This HTML is generated at runtime . The events are defined in the code behind file.
I need to add these controls at runtime. I tried to use the Literal-Control but the controls are working just like HTML Controls and not like ASP.net Controls.
EDIT:
Note: The Project type should be Website, not a web Application. Web application won't support on demand compilation where website yes, it is.
If I understood currectly, you want to take the Markup from User which contains even asp.net controls and scriplets too.
if this is the case, Follow below steps:
Create a dummy .ascx control file, like DynamicMarkup.ascx with empty content
Add this user control to the page (xxxx.aspx) where you want to show this control statically so it registered to the page
<%# Register src="~/DynamicMarkup.ascx"
tagname="DynamicMarkup" tagprefix="MyASP" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server"
ID="DynamicMarkupContainer" ></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Write user input markup (may be get from database based on your criteria) to the DynamicMarkup.ascx file in page OnInit of the page (xxxx.aspx) And the create object of this DynamicMarkup
DynamicMarkup dynamicMarkup = LoadControl("~/DynamicMarkup.ascx") as
DynamicMarkup;
DynamicMarkupContainer.Controls.Add(ucSimpleControl);
I have not tested this approach, Just give a thought, With this you may get some session overwriting issue which you need handle.
Hope this will help!!
OLD:
is this the one that you are expecting? TextBox, and Button controls are available in System.Web.UI.WebControls namespace.
void Page_Load(Object sender, EventArgs e)
{
TextBox input = new TextBox();
input.Id ="input";
this.PlaceHolder.Controls.Add(input);
Button btnSend=new Button();
btnSend.Id ="btnSend";
btnSend.Text="Send";
btnSend.Click += new EventHandler(btnSend_Click);
this.PlaceHolder.Controls.Add(btnSend);
}
void btnSend_Click(object sender, EventArgs e)
{
// throw new NotImplementedException();
}
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:PlaceHolder ID="phHolder" runat="server"></asp:PlaceHolder>
</form>
</body>
</html>
code behind :
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Init()
{
GenerateContorls();
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void GenerateContorls()
{
TextBox newTxt = new TextBox() { ID = "txtsend" };
Button newBtn = new Button() { Text = "Send", ID = "btnsend" };
newBtn.Click += btnsend_Click;
phHolder.Controls.Add(newTxt);
phHolder.Controls.Add(newBtn);
}
protected void btnsend_Click(object sender, EventArgs e)
{
TextBox txt = (TextBox)this.FindControl("txtsend");
//your code
}
}
hope it helps
<%=Eval(src) %> is not working.
<img id="Img1" src="<%=Eval( src) %>" runat="server" alt="" />
Code Behind
public partial class image_from_cb : System.Web.UI.Page
{
public string src="";
protected void Page_Load(object sender, EventArgs e)
{
src = "~/final.jpg";
}
}
I know i can do this with Attribute.Add or with ASPIMAGE , but i was just wondering why <% %> is not working
As You are using runat="server", You can use
protected void Page_Load(object sender, EventArgs e)
{
src = "~/final.jpg";
Img1.src = src;
}
Or if you want to use your src variable
<img id="Img1" src='<%= src >%' alt="" />
Try it
You can do this way
html
<img id="Img1" runat="server" alt="" />
C#
protected void Page_Load(object sender, EventArgs e)
{
Img1.src= "~/final.jpg";
}
And Another way is
try this Page.DataBind(); in Page load event
Look at my sample code html
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img id="Img1" src="<%# src.ToString() %>" runat="server" alt="" />
<asp:Image runat="server" ID="myImage" ImageUrl="<%# src %>" />
</div>
</form>
</body>
</html>
and code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public string src = "";
protected void Page_Load(object sender, EventArgs e)
{
src = "~/1455094_265560366930522_537876922_n.jpg";
//Add this
Page.DataBind();
}
}
}
Easy coding !!The both ways working in my side
I need a value from javascript before the main page load and then want to use that value in code. The code which I am using for that purpose is:
I have make a test.aspx page. The code for which is as following:-
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!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>Untitled Page</title> <script type="text/javascript" language="javascript">
function GetScreenHeightAndWidth()
{
var width = screen.width;
var height = screen.height;
var object = 'Label1';
document.getElementById(object ).innerHTML=height ;
//alert(height);
'<%Session["Screensize"] = "' + height +'"; %>' ;
}
</script>
</head>
<body onload="GetScreenHeightAndWidth();" >
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
The code for test.aspx.cs page is as following:-
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Windows.Forms;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["Screensize"] = Label1.Text;
TextBox1.Text=Session["Screensize"].ToString();
}
}
The result is as following:-
768 test
while result what I need is 768 and 768.
Is there any way to solve the problem?
What you are asking is impossible to do. Why do you need to know the screen size on the server -side in the first place? Whatever you need to accommodate on the html produced by the server can be either adjusted via proper CSS rules or Javascript (JQuery if you prefer that framework)
If you set the width/height values to a hidden field from script, you can than access them from codebehind after a postback. Labels are not posted with the form so you cant use that.
First of all thanks to all of you for responding to my question. After much search I have found what I was exactly wanting. For this I have to use one more page and add this code in my original code:
if (!IsPostBack)
{
if (Session["ScreenResolution"] == null)
{
Response.Redirect("detectscreen.aspx");
}
else
{
Session["Screensize"] = Session["ScreenResolution"].ToString();
}
TextBox1.Text = Session["Screensize"].ToString();
}
And the code for new page is:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["action"] != null) {
Session["ScreenResolution"] = Request.QueryString["res"].ToString();
Response.Redirect("Catalogue.aspx");
}
}
and the script in the new page will be like this:
<script language="javascript" type="text/javascript">
res = "&res="+screen.height
top.location.href="detectscreen.aspx?action=set"+res
</script>