Assign converted html to a serverside control - javascript

I'm using markdown and I've got some input such as **test** which makes the word test appear in bold test and I've converted it with html like so"
var converter1 = Markdown.getSanitizingConverter();
var editor1 = new Markdown.Editor(converter1);
editor1.run();
var d = document.getElementById("wmd_input");
var html = converter1.makeHtml(d.value);
alert(html);
And this alerts <b>test</b> the issue is I need to take this value, namely <b>test</b> and access this via server side code (asp.net). I tried assigning it to a variable like so:
document.getElementById("Label1").value = html;
But it doesn't seem to work, when I go to the code behind it shows Label1 as being empty.
Is this possible?
Edit
I tried to change it to a hidden field same issue:
<script type="text/javascript">
(function () {
var converter1 = Markdown.getSanitizingConverter();
var editor1 = new Markdown.Editor(converter1);
editor1.run();
var d = document.getElementById("wmd_input");
var html = converter1.makeHtml(d.value);
alert(html);
document.getElementById('<%= h1.ClientID %>').value = html;
var h = document.getElementById('<%= h1.ClientID %>');
alert(h.value);
})();
</script>
The issue I have is I have an asp.net server side button that when clicked I try to do this:
Label1.Text = h1.Value;
That is to store the value from the hidden field to a label but this doesnt work. When I put a break point in it shows h1 is empty ""....So I'm not sure what event or how to do this so that when I make changes to my textarea wmd_input that i should be able to see these changes in my server side code...
Here's my entire asp.net form:
<html>
<head>
<title>PageDown Demo Page</title>
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<script type="text/javascript" src="js/Markdown.Converter.js"></script>
<script type="text/javascript" src="js/Markdown.Sanitizer.js"></script>
<script type="text/javascript" src="js/Markdown.Editor.js"></script>
</head>
<body>
<form id="myForm" runat="server">
<div class="wmd-panel">
<div id="wmd-button-bar"></div>
<textarea cols="5" rows="5" id="wmd_input" class="wmd-input" runat="server"></textarea>
<div id="wmd_preview" class="wmd-panel wmd-preview" runat="server"></div>
</div>
<asp:button id="Button1" runat="server" Text="Set" onclick="Button1_Click"></asp:button>
<asp:button id="Button2" runat="server" Text="Get" onclick="Button2_Click"></asp:button><asp:label id="Label1" runat="server">Label</asp:label>
<asp:HiddenField ID="h1" runat="server" EnableViewState="true" />
<script type="text/javascript">
(function () {
var converter1 = Markdown.getSanitizingConverter();
var editor1 = new Markdown.Editor(converter1);
editor1.run();
var d = document.getElementById("wmd_input");
var html = converter1.makeHtml(d.value);
alert(html);
document.getElementById('<%= h1.ClientID %>').value = html;
alert(document.getElementById('<%= h1.ClientID %>').value);
})();
</script>
</form>
</body>
</html>

The asp:Label tag isn't an input control, so it won't get posted back to the server. I'd suggest using asp:HiddenField or asp:TextBox instead. (And Adil's point is crucial too, you need to make sure the client ID is actually what you think it is.)
Here's a test that works for me. On the first page load, the label shows "initial value", but the alert shows "updated". After postback, the label also shows "updated".
Edit Added the client-side update logic inside a client-side event handler.
<%# Page Title="Test" Language="C#" AutoEventWireup="true" %>
<script runat="server">
void Page_Load()
{
l1.Text = h1.Value;
}
</script>
<html>
<body>
<form runat="server">
<asp:HiddenField runat="server" Value="initial value" ID="h1" />
<asp:Label runat="server" ID="l1" />
<asp:Button runat="server" Text="do postback" />
</form>
<script>
document.getElementById('<%= Button1.ClientID %>').onclick = function () {
document.getElementById('<%= h1.ClientID %>').value = 'updated';
alert(document.getElementById('<%= h1.ClientID %>').value);
};
</script>
</body>
</html>

Labels don't post. You will have to use an input or textarea element (<asp:TextBox>). If you don't want the user to see the markup source, you can also use an <asp:HiddenField>.

you should be able to do what you want to do by using knockoutjs http://knockoutjs.com/examples/helloWorld.html You can use element binding.

Related

How to set the value for textbox with javascript on an ASP.NET

I need to set up asp textbox with a value from javascript input.
i have here:
<td ><asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' ClientIDMode="Static" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="AddressTextBox" ErrorMessage="*"
ValidationGroup="InsertCustomer" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>
I use autocomplete address form from here
so i placed this to the javascript code:
var src = document.getElementById("autocomplete");
var dst = document.getElementById("AddressTextBox");
and this to function fillInAddress() funcion:
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
dst.value = src.value;
}
Im trying to get the full address from the autocomplete to the AddressTextBox field as soon as the address is selected.
But im getting an error that AddressTextBox "The name 'AddressTextBox' does not exist in the current context"
Any ideas? thanks.
AddressTextBox is server control so the ID might be changed.
Try as:
var dst = document.getElementById("<%=AddressTextBox.ClientID%>").value;
This may resolve your error.
Just run this working piece of code, this might help you:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function setValues()
{
document.getElementById('dest').value=document.getElementById('src').value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="src" runat="server"></asp:TextBox>
<asp:TextBox ID="dest" runat="server"></asp:TextBox>
<input type="button" value="Set Value" onclick="setValues()" />
</div>
</form>
</body>
</html>
Now you can customise the solution as per your requirement.
It looks that I made it work. I just added the following:
document.getElementById('ctl00_maincontent_FormView1_AddressTextBox').value = document.getElementById('autocomplete').value;
into function fillInAddress()

resetting the textbox through client side using javascript in asp.net

I want to reset the text box through client side using javascript. I used following ways to reset the textbox. But, they are not resetting.
cText = "";
document.getElementById("<%=txtText.ClientID %>").value = cText;
and
document.getElementById("<%=txtText.ClientID %>").value = "";
Please can anyone help me in this?
code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
function validate() {
var cText = document.getElementById('<%=txtText.ClientID %>').value;
if (cText != "") {
alert("Text is not null.");
document.forms['UserMaster'].elements['txtText'].focus();
$("#<%=txtText.ClientID %>").val("");
return false;
}
return true;
};
</script>
</head>
<body>
<form id="form" runat="server">
<div class="page">
<asp:TextBox ID="txtText" runat="server"></asp:TextBox>
<br/>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Bold="true" OnClientClick="return validate()"
ForeColor="#9370DB" Height="23px" Width="65px" OnClick="btnSubmit_Click"
/>
<br />
</div>
</form>
</body>
</html>
Here is the solution for your issues:
document.getElementById('<%=txtText.ClientID %>').value='';
you were using double quotes which was not working for you, whereas you need to use single quotes as show in the above code. This was your issues.
.value=" "; //is Wrong
Happy Coding.
I had the same problem and using this helped me solve the issue.
Here you can inspect the control from the browser and get the ID of that control.
// ControkClientID is the ID from Inspect element
document.getElementById(ControlClientID).value='';

ASP.NET: How to get JavaScript variable from popup window?

This is code for Firefox.
Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="pop.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Hidden1" type="hidden" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Popup" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
Button1.Attributes.Add("onClick", "javascript:InvokePop('" + TextBox1.ClientID + "');");
}
PopupWin.aspx:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="pop.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtValue" runat="server"></asp:TextBox><br />
<br />
<input type="button" id="btnReturnValue" value="Return value back" onclick="ReturnValue();" />
</div>
</form>
</body>
</html>
pop.js:
function InvokePop(fname)
{
val = document.getElementById(fname).value;
retVal = window.open("PopupWin.aspx?Control1=" + fname, 'Show Popup Window', 'height=90,width=250,resizable=yes,modal=yes');
retVal.focus();
}
function ReturnValue()
{
var newValue = document.getElementById("txtValue").value;
if ((window.opener != null) && (!window.opener.closed))
{
window.opener.document.getElementById("TextBox2").value = newValue;
}
window.close();
}
In this case I click button on Default.aspx page and open Popup.aspx as popup window. I enter some value to text box in Popup.aspx and press button. The new value appears in second text box on Default.aspx page.
That works, but how can I pass the new value entered in Popup.aspx page to some handler in Default.aspx page and use it further? For example, I can have another ASPX button on Default.aspx page and when I click it I can use the value entered in Popup.aspx page.
Well what you can do is the following:
Add a hiddenField on the first page. Im calling it "hfPopupValue".
In pop.js u are currently doing this:
window.opener.document.getElementById("TextBox2").value = newValue;
You can change this to:
window.opener.document.getElementById("hfPopupValue").value = newValue;
Afterwards you can just read the value from the hiddenField.
This should solve your problem.
Have you thought of using jQueryUI's Dialog, which lets you keep all controls on the same page, meaning cleaner code.
It also doesn't look as nasty as a JavaScript popup window.

javascript error in ASP.NET content page

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript">
function Test1(w) {
document.getElementById('<%=TextBox1.ClientID%>').style.width = w;
return false;
}
Test1(10); // This line arises an error. Why ?
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" type="button" value="button" onclick="Test1(10)" />
</asp:Content>
I get error when the function Test1(10) is called from the script. But when it called on the click of button, it works fine. How can I call the function from the script(OR how I can access the onload() function in an ASP.NET Content Page)?
When your call to "Test1()" inside the <script> block happens, that input field is not yet part of the DOM. Thus the call to getElementById() will return null and the first line of the function will fail.
If you move the <script> block to after the text box, it should work.
edit — also you may want to explicitly append "px" to your width.
try this
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript">
function Test1(w) {
document.getElementById('<%=TextBox1.ClientID%>').style.width = w;
return false;
}
window.onload = function() {
Test1(10); // This line arises an error. Why ?
}
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" type="button" value="button" onclick="Test1(10)" />
</asp:Content>
It's because the textbox doesn't exist when you execute Test1 in the script. You either have to place the call in a document.ready function or move it further down the page, after the textbox.
First of all you every time you want to execute javascript code, even directly from the tag ether from events you have to wait to load the dom level.
So can do something like :
<script type="text/javascript">
window.onload =function()
{ /* code */ }
</script>
Or execute your code in the "onload" event of the body tag:
like
<body onload="Test1(10);">
</body>
Or if you use JQuery you can use the above :
<script type="text/javascript">
$(document).ready(function(){
/* Code to execute */
});
</script>

Grabbing Textbox in javascript

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
function Incrementer()
{
debugger;
var txtBox = document.getElementById('ctl00_MainContent_TextBox1').value;
alert(txtBox);
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Up" OnClientClick="Incrementer();"/>
<asp:Button ID="Button2" Text="Down" runat="server"/>
</asp:Content>
I am unable to capture the textbox in JavaScript. What is the problem?
I've re tagged this ASP.net as well because it's quite specific to this problem.
When an ASP.net textbox control is rendered on a page it is assigned a dynamic ID that can change. There is a property for controls, ClientID that provides you with the controls assigned ID so you can use it in your scripts.
Use as follows:
<script type="text/javascript">
function Incrementer()
{
debugger;
var txtBox = document.getElementById('<%=TextBox1.ClientID%>').value;
alert(txtBox);
}
</script>
Try this
var txtBox = document.getElementById('<%=TextBox1.ClientID%>').value;
alert(txtBox);

Categories

Resources