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:Labeltag isn’t an input control, so it won’t get posted back to the server. I’d suggest usingasp:HiddenFieldorasp:TextBoxinstead. (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.