Want to access literal value into javascript - c#

I have got a literal control on page (with some data on it). i want to access it in javascript and want to put some text on it. please tell me how can i access literal control in JavaScript. I was trying with following code-
<asp:Literal ID="lblPerInstanceListing" runat="server"></asp:Literal>
Javascript:
var value= document.getElementById('<%=lblPerInstanceListing.ClientID %>')
I am getting null value return by this.

An ASP.NET Literal control does not by itself insert any HTML into a webpage. Your Literal control is a placeholder for text or HTML you will set in your code behind.
You should wrap your literal in a DIV or SPAN and give that an ID and reference that DIV or SPAN in the JavaScript
WebForm:
<span id="yourId"><asp:Literal ID="lblPerInstanceListing" runat="server"></asp:Literal></span>
JavaScript:
Solved by this code.
var value= document.getElementById('yourId').innerText;

<span id="yourId" runat="server"></span>
js :
var value= document.getElementById('yourId').innerHTML;
It is not better then use this code??
You can put in span enableviewstate to false if You need.

It's a Literal. It renders literal HTML to the page. It doesn't render any container matching the ClientID that you can access on the client side.
Either put a div or span in the literal with an ID you can use, or use e.g. a Label or Panel control that does render a wrapping span or div you can use.

Related

how to execute dynamically created html code in string variable in asp.net

I have some HTML code in a String variable in C#.
That code generated and stored in String variable at run-time.
But now I want to execute this code at run time.
All I want to do is see how that code looks in BROWSER.
How can I do this?
Is there any control in asp.net that provides such functionality?
you can use any contol like panel literal.
Suppose you have a string
string str="<p><div>Some Text</div></p>";
Literal1.Text = str;
html
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
in your .aspx put html tag (div, paragraph, label etc) with property runat=server
<p id="dynamicstring" runat="server"></p>
in codebehind
dynamicstring.InnerText= yourstring;
You can use StringBuilder and then Simply use Response.Write for StringBuilder

ASP.NET set hiddenfield a value in Javascript

I don't know how to set the value of a hiddenField in Javascript. Can somebody show me how to do this?
Javascript:
document.getElementById('hdntxtbxTaksit').value = "";
HTML:
<asp:HiddenField ID="hdntxtbxTaksit" runat="server" Value="" Visible="false"> </asp:HiddenField>
error : "Unable to get value of the property \'value\': object is null or undefined"
Prior to ASP.Net 4.0
ClientID
Get the client id generated in the page that uses Master page. As Master page is UserControl type, It will have its own Id and it treats the page as Child control and generates a different id with prefix like ctrl_.
This can be resolved by using <%= ControlName.ClientID %> in a page and can be assigned to any string or a javascript variables that can be referred later.
var myHidden=document.getElementById('<%= hdntxtbxTaksit.ClientID %>');
Asp.net server control id will be vary if you use Master page.
ASP.Net 4.0 +
ClientIDMode Property
Use this property to control how you want to generate the ID for you. For your case setting ClientIDMode="static" in page level will resolve the problem. The same thing can be applied at control level as well.
asp:HiddenField as:
<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />
js code:
$("#hfProduct").val("test")
and the code behind:
hfProduct.Value.ToString();
First you need to create the Hidden Field properly
<asp:HiddenField ID="hdntxtbxTaksit" runat="server"></asp:HiddenField>
Then you need to set value to the hidden field
If you aren't using Jquery you should use it:
document.getElementById("<%= hdntxtbxTaksit.ClientID %>").value = "test";
If you are using Jquery, this is how it should be:
$("#<%= hdntxtbxTaksit.ClientID %>").val("test");
document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value
The id you set in server is the server id which is different from client id.
try this code:
$('hdntxtbxTaksit').val('test');
I suspect you need to use ClientID rather than the literal ID string in your JavaScript code, since you've marked the field as runat="server".
E.g., if your JavaScript code is in an aspx file (not a separate JavaScript file):
var val = document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value;
If it's in a separate JavaScript file that isn't rendered by the ASP.Net stuff, you'll have to find it another way, such as by class.
My understanding is if you set controls.Visible = false during initial page load, it doesn't get rendered in the client response. My suggestion to solve your problem is
Don't use placeholder, judging from the scenario, you don't really need a placeholder, unless you need to dynamically add controls on the server side. Use div, without runat=server. You can always controls the visiblity of that div using css.
If you need to add controls dynamically later, use placeholder, but don't set visible = false. Placeholder won't have any display anyway, Set the visibility of that placeholder using css. Here's how to do it programmactically :
placeholderId.Attributes["style"] = "display:none";
Anyway, as other have stated, your problems occurs because once you set control.visible = false, it doesn't get rendered in the client response.
I will suggest you to use ClientID of HiddenField. first Register its client Id in any Javascript Variable from codebehind, then use it in clientside script. as:
.cs file code:
ClientScript.RegisterStartupScript(this.GetType(), "clientids", "var hdntxtbxTaksit=" + hdntxtbxTaksit.ClientID, true);
and then use following code in JS:
document.getElementById(hdntxtbxTaksit).value= "";
Try setting Javascript value as in document.getElementByName('hdntxtbxTaksit').value = '0';

how to get the value from input control of html

I have a table which is generated by a 3rd party control. This table has only one row and one column. Within this column is html text as follows :
<p>
this is a test</p>
<p>
<input name="amount" type="text" value="this is for amount" /></p>
<p>
this is a test</p>
<p>
<input name="test" type="text" value="this is for test" /></p>
<p>
this is a test</p>
the problem is how to get the value saved inside the html input control ?
I tried the following code but it fails:
t.Rows[0].Cells[0].FindControl("amount");
thanks in advance...
You can use the below snippet to retrieve the required values inside the javascript function:
function getValue()
{
//First method
var val= document.getElementsByName("amount");
alert("Val by Element Name:-" +val(0).value);
//Second method
val= document.getElementById("amount");
alert("Val by element Id:-" +val.value);
//third method
val= document.getElementsByTagName("input");
alert("Val by Tag:-" +val(0).value);
alert("Val by Tag:-" +val(1).value);
}
Assumption: You're aware of the names of the input controls inside the table obtained by the 3rd party tool.
Do you mean when the form is posted back? In that case, just use
Request["amount"]
to get the value.
Since it is a 3rd party control, it should give you access to the values of the controls throught the properties of the controls. Check the properties and documentation.
Use a HiddenControl and jQuery.
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(documet).ready(function(){
var inputVals = '';
$('#yourThirdPartyTableID').find('input[type=text]').each(function() {
inputVals = inputVals + $(this).val() + ', ';
});
$('#<%= YourHiddenControlID.ClientID %>').val(inputVals);
});
</script>
The above code will insert values of all text inputs from your third party table into the hidden control separated by a comma. You can change the comma into anything else to make it easier to split on server side code.
If you're looking for the value of the input with "name" of "amount", do the following:
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(documet).ready(function(){
$('#<%= YourHiddenControlID.ClientID %>').val($('#yourTableID').find('[name="amount"]').val());
});
</script>
There is no straight forward method to retrieve the value of an input control unless you say runat="server".
My suggestion is to use the runat="server" tag and try getting the value using VALUE property.
Otherwise you can use Page.FindControl('') and see whether the control is returning other than null...then i feels that will work.
if you are using ajax or master-content page then the controls id will be not the one you given....so make sure you are giving the correct id itself (using view source of the page)

Is there such a control in ASP.NET that can render HTML during runtime?

Like the preview box below that take input from the textarea we are typing now.
The requirement is to retrieve HTML saved in database and preview it on the screen. Should I use label or is there any better control around?
I would use the literal control unless you need to do anything "extra" with the html.
I often use a DIV with a runat="server" attribute declared. That way, I have a container to apply CSS classes to, and I know and can control the markup that is being created.
A Literal will work fine if you don't need a container (or you have a container already on the page).
<div class="css-class">
<asp:Literal runat="server" />
</div>
OR
<div runat="server" class="css-class" />
And as Oded said, watch out for XSS by sanitizing your HTML.
If you simply need to output HTML, use a LiteralControl - you simply set its Text property to the HTML you need.
You may want to think about cleaning up the HTML, if it something that is user input, just in case of XSS attacks hiding in your HTML data.

How to set value of <input type="hidden"> in asp .net page_load without using runat="server"

i need to do the following two things...
i want to set value of in asp .net page_load. the problem is that i dont want to use runat="server". i have tried this the following but it does not work:
HtmlInputHidden hiddenControl = (HtmlInputHidden) FindControl("a");
is there a way to access in asp .net page_load without using runat="server"? ? ?
i can do this if i use but in this case i cannot access it in master page's javascript function. i have tried this but it does not work...
var hdnField = document.getElementById('<%= hdnIdentity.ClientId%>');
var hdnField = document.getElementById("hdnIdentity").getAttribute("value");
var hdnField = document.getElementById("hdnIdentity").value
what i need... i want to access content page's hidden field value in javascript in master page. is there a way ? ? ? thnx in advance regards Haroon haroon426#yahoo.com
I sometimes do the following, especially when I want control over my ids (especially when using jquery).
<asp:literal id="literal1" runat="server"><input type="hidden" id="someid" value="{0}"/></asp:literal>
Then, in codebehind you can set the value with the following:
literal1.Text = string.Format(literal1.Text, "somevalue");
This doesn't really get around using runat="server", but you haven't specified why you don't want to do that. Also, you'd have to get the value with a request.form
Update
In .net 4.0 you have much more control over your IDs. See this for more information:
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
IIRC, you need to look in the HttpRequest.Forms, somewhere in there.
If the value is part of a POST form then you want to check Request.Forms or Request.QueryString if it's a GET form.
ad 1) in aspx file just write <input type="hidden" value="<%=GetHiddenValue%>" />. And in your code behind define protected property
public class MyPage : Page {
protected GetHiddenValue { get { /*...*/ } }
You can use it in your master page javascript how ever the control name is not what you expect it to be you'd need to use ClientID to get that. If you do not apply runat=server you can only get a hold of the control as text by either traversing the .aspx file or as some one mentioned embedding it in a named tag and then doing string manipulation on the inner HTML. That is for setting it. If you need to get the value use Request[tagName] or similar
ad 2) You can use simple html code in your content page with specified id <input type="hidden" id="myHiddenField" />. Then in master page javascript use document.getElementById('myHiddenField').

Categories

Resources