Getting a variable from javascript to a property of a webcontrol - javascript

I am wondering if i can get the session value and put as a parameter to a control i have.
This is the code I currently have:
<script type="text/javascript">
var workspacepath = '<%= Session["workspacepath"].ToString() %>';
var workspacename = '<%= Session["workspacename"].ToString() %>';
</script>
<iz:FileManager ID="FileManager1" runat="server" Height="550" Width="1016px" FileViewMode="Details"
ClientOpenItemFunction="funcinvisiblebutton1">
<RootDirectories>
<iz:RootDirectory DirectoryPath="~/" Text="Workspace Etteplan" />
</RootDirectories>
</iz:FileManager>
</div>
I want the session value in DirectoryPath="~/".

You can set attribute value :
document.getElementsByTagName("iz:RootDirectory")[0].setAttribute("DirectoryPath",workspacepath);

Related

getElementByID when script is external

I am trying to access a asp:panel in an external script to make it not visible,but it does not seem to be working. When the Script is in the .aspx file it works fine though.Any suggestions?
In the .aspx file
<script src="App_Themes/custom.js"></script>
<asp:Button ID="descriptionButton" Text="Description" runat="server" OnClientClick="descButton(); return false;" />
<asp:Panel ID="desciptionPanel" runat="server">
///random stuff
<asp:panel>
in the custom.js file
function descButton() {
var desc = document.getElementById('<%=desciptionPanel.ClientID%>');
desc.style.visibility = "visible";
desc.style.height = "800px";
}
Thanks in advance
Split it to two.
First, in the aspx at the server, leave the ID so that you can reuse it later
<script>
window.panelID = '<%= whatever.ClientID %>';
</script>
Then, in an external script, just use the ID
function externalJSfunction() {
var desc = document.getElementById(window.panelID);
}
Use a parameter to your function
OnClientClick="descButton('<%=desciptionPanel.ClientID%>');
then script will be
function descButton(id) {
var desc = document.getElementById(id);
desc.style.visibility = "visible";
desc.style.height = "800px";
}

I can't get the session value to print

<script language="javascript" >
function addSelection(){
var tst="hello";
sessionStorage.setItem("test", tst);
<%
String pastst=request.getParameter("tst");
session.setAttribute("tst1",pastst);
%>
addSelection1();
}
function addSelection1(){
var outtst = '<%= session.getAttribute("tst1") %>';
<%
out.print("session"+session.getAttribute("tst1"));
%>
var tstout= session.getAttribute("tstout");
alert(tstout);
}
</script>
alert(tstout); works properly, but the code inside jsp prints blank.
I don't know what is the purpose behind this. Anyway few issues found in your code.
First. You cannot get the javascript variable value like this,
var tst="hello"; //Setting the value in javascript
sessionStorage.setItem("test", tst);
<%
String pastst=request.getParameter("tst"); //Expecting the value will be returned by java request
session.setAttribute("tst1",pastst);
%>
Second. Following line will provide you JS error, You cannot assign like this var tstout= session.getAttribute("tstout");
You need to change into, var tstout= '<% session.getAttribute("tstout"); %>'
Its wrong assumption javascript will print the session value in the following line,
<%
out.print("session"+session.getAttribute("tst1"));
%>
Finally, you need to start learning JSTL and EL avoid using scriptlets.
This is the way some where it is working.
<script type="text/javascript" >
function addSelection(){
var tst="hello";
sessionStorage.setItem("test", tst);
<%
String pastst="hello";
session.setAttribute("tst1",pastst);
%>
addSelection1();
}
function addSelection1(){
var outtst = '<%= session.getAttribute("tst1") %>';
alert('<%
out.print("session : "+session.getAttribute("tst1"));
%>');
alert(outtst);
}
</script>
Hope it helps you.

How to send xml string from action class to jsp in struts2

I am very much new to struts 2. I am creating a sample example. In my example i have a login.jsp. Currently i have not implemented any logic in my jsp page, i am just checking if the user has inputed value in username and password textBox. From the login.jsp, the control gets redirected to callWebservice.java action class. Here i am calling a webservice which is giving me result in form of xml. On success i am redirecting from this action class to showResult.jsp. Here i want to capture my result xml in a variable using javascript.
Here is my code on showResult.jsp :
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Show Result</title>
<script type="text/javascript">
function showResult() {
alert("Here it comes ");
var result = <s:property value="key1" />
alert("Before Display");
}
</script>
</head>
<body onload="showResult()">
<form action="#" method="POST" >
<div id="headerbg">
<h1 id="headerTag">Webs service Result</h1>
</div>
Result obtained by invoking webservice is : <s:property value="key1" />
</form>
</body>
</html>
I am getting the result xml from this property tag i.e <s:property value="key1" />.
I just want to know how can i store this result in a variable using
JAVASCRIPT.
Change the var result as
var result = "<s:property value='key1'/>";
i.e.
<script type="text/javascript">
function showResult() {
alert("Here it comes ");
var result = "<s:property value='key1'/>";
alert("Before Display");
}
</script>
replace
var result = <s:property value="key1" />
by
var result = "<s:property value=\"key1\" />"

Create a correct string in JavaScript from result of C# inline code

I have to use some inline code in my asp.net application.
F.e. i have following inline code:
<% FlashRenderer.Render(); %>
That will return me following markup:
<div id="flashGame" style="color:red"></div>
<script type="text/javascript" language="javascript">
var flash = document.getElementById('flashGame');
...
</script>
</div>
I need to assign a result of FlashRenderer.Render() to javascript variable, then using jquery
append that markup to some parent div. I try follwing:
<script type="text/javascript">
var swfString = '<%= FlashRenderer.Render() %>';
$("swf").append(swfString);
</script>
And it fails, because result string from inline inclusion has both single and double quotes, so when i use single quotes to wrap '<%= FlashRenderer.Render() %>' i always have a syntax error in browser console.
I try to replace "'" with "\'" but error happens before i got string created.
Here is screenshot what i have after try to use JavaScriptSerializer().Serialize
You can use a JSON serializer, which will give you a Javascript-safe string, including the quotes, and it will handle the line breaks as well:
--JSON.NET--:
var x = <%= JsonConvert.SerializeObject(FlashRenderer.Render()) %>;
or
--System.Web.Script.Serialization--:
var x = <%= new JavaScriptSerializer().Serialize(FlashRenderer.Render()) %>;
This will render something like:
var x = "<div id=\"flashGame\" style=\"color:red\"></div>\r\n <script type=\"text/javascript\" ...etc... </div>";
Instead of trying to place it in a string, just have it render hidden on the page and move it:
<div style="display:none">
<%= FlashRenderer.Render() %>
</div>
<script type="text/javascript">
$("swf").append($("#flashGame"));
</script>
Hacky solution if you can't modify the C# code. Output the function into a textarea then get the value of the textarea with jQuery.
<textarea id="swfref" style="display:none;">
<% FlashRenderer.Render(); %>
</textarea>
<script type="text/javascript">
var swfString = $("#swfref").val();
$("swf").append(swfString);
</script>

how can I assign the text of programatically created label to the other label in html body?

RadioButton rb;
Label lb;
while (reader.Read())
{
rb = new RadioButton(); **//radio buttuns created**
lb = new Label(); **// labels created**
lb.Text = reader[0].ToString(); *// each label's text assigned*
rb.Attributes.Add("OnClick", "getSelectedAuthor('"+lb.Text.ToString()+"')");
PlaceHolder1.Controls.Add(rb); *//when the radio clicked,function called*
PlaceHolder1.Controls.Add(lb); *// to set use the text of label*
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
conAuthorTbl.Close();
//innerthtml does not work for the purpose,//becuse I need to send//Labe2's text to other page, //when I use Response.Redirect(..), //there are no string coming //from Label2
<script language="javascript" type="text/javascript">
function getSelectedAuthor(text) {
document.getElementById("Label2").innerText = text;
}
</script>
//the text where I want to set the text
<div>
<asp:Label ID="Label2" runat="server" Text="" ></asp:Label>
</div>
Might I suggest you use something like knockout.js ? Its client side MVVM, you can use declarative bindings to achieve the task. I hope this helps.
http://knockoutjs.com/
try this:
<script language="javascript" type="text/javascript">
function getSelectedAuthor(text) {
document.getElementById("<%=Label2.ClientID%>").innerText = text;
}
</script>
This will work as long as the javascript is on the same aspx page.
If you need to wirk with it in external JS file, then you can create a mapping for your js file.
so you'll put something like this in your aspx page:
<script language="javascript" type="text/javascript">
var myElement_Label2 = getElementById("<%=Label2.ClientID%>");
etc...
</script>
and then in our external js file you can work just with those references like:
myElement_Label2.innerText = "something";

Categories