sending data from javascript to C#

  • Thread starter Thread starter siol
  • Start date Start date
S

siol

Hy!

I have htmleditor (from the microsoft's samples page) embeded into my
asp.net page.
Editor generates some html code inside <div id="oDiv"></div> tags. And what
I would like to do is return oDiv.InnerHtml to my "codebehind" (C#) where I
would do additional tasks on it.
If I do postback the oDiv content is lost.

Can anyone help me how to send those data back to codebehind?

Thanks in advance.
- Samo K.
 
Hi

In order to access from server, you need to add runat="Server"
<div id="oDiv" runat="Server"></div
Then in server code you can ge
oDiv.innerHtm

Bin Song, MCP
 
yes, that's true, but if script modifies the contents of the <div> on the
client, that content will not get posted back to the server on a postback.
You must look at how ASP.NET works. The server cannot receive any
information unless it receives it in the Querystring or Form collections. A
div is not a normal form field, so it doesn't post anything back, as does
<input type="text" ... .>. You are going to have to do some custom
programming to get it to work. If it were me, I would register a hidden
form field, and call a script function right before the page submits back to
the server. In that script function, copy the InnerHTML property of the div
to the <input type=hidden. . .> and then you can get the data in server
code.

By the way, have you checked out FreeTextBox? It's a .Net WYSIWYG Html
editor. It's used in .Text, nGallery, and I use it in my EZWeb project:
http://workspaces.gotdotnet.com/ezweb.

Jeffrey Palermo
 
Thanks for your reply...
I've created the hidden field like this:

<input type="hidden" id="oHidden" runat="server">

The problem with this is that the server changes the IDs a bit and when I
look at the generated HTML the id is _ctl0_oHidden.

I fill this input with data like this: form1._ctl0_oHidden.value =
oDiv.InnerHTML;
(if I do form1.oHidden.... i get object Error exception).

And when I do postback, all I get is big nothing (value == "").

I also tried with input type=text, and the script gets the right data to
oHidden (or _ctl0_oHidden).

I'm a bit noob (as you can see) to all this stuff.


- Samo K.
 
For your script, you can do:
document.all["<%=oHidden.ClientID%>"].value = oDiv.InnherHTML;
Make sure the hidden field is inside your server-side form.
Then (with trace turned on) look at your form collection, and you should see
that the hidden field posted some data. You're on the right track. It'll
work for you.

In your code-behind, you do have a declared field like
protected HtmlInputHidden oHidden; right?

Jeffrey Palermo
 
I've tried document.all["<%=oHidden.ClientID%>"]

but the value is undefined, thus obj.value (obj=document.all["<%....) threw
object Error exception.

As for trace... I could output something like this (despite object Error
exception) on post back:

__VIEWSTATE
_ctl0:Title
_ctl0:Customer
_ctl0:Link
_ctl0:oHidden

The code was:

foreach(string s in Request.Form.Keys)
System.Diagnostics.Trace.WriteLine(s);

But the oHidden.Value in my server code remains "".

Am I annoying? :) This annoys me for the past few days.

Samo K.



For your script, you can do:
document.all["<%=oHidden.ClientID%>"].value = oDiv.InnherHTML;
Make sure the hidden field is inside your server-side form.
Then (with trace turned on) look at your form collection, and you should see
that the hidden field posted some data. You're on the right track. It'll
work for you.

In your code-behind, you do have a declared field like
protected HtmlInputHidden oHidden; right?

Jeffrey Palermo

SamoK said:
Thanks for your reply...
I've created the hidden field like this:

<input type="hidden" id="oHidden" runat="server">

The problem with this is that the server changes the IDs a bit and when I
look at the generated HTML the id is _ctl0_oHidden.

I fill this input with data like this: form1._ctl0_oHidden.value =
oDiv.InnerHTML;
(if I do form1.oHidden.... i get object Error exception).

And when I do postback, all I get is big nothing (value == "").

I also tried with input type=text, and the script gets the right data to
oHidden (or _ctl0_oHidden).

I'm a bit noob (as you can see) to all this stuff.


- Samo K.


collections. back
the
 
Ok, I worked out an example. copy this source into a blank .aspx file and
run it. It works.\



Initial value.

Initial value.
copy div contents to hidden field.



Jeffrey Palermo


SamoK said:
I've tried document.all["<%=oHidden.ClientID%>"]

but the value is undefined, thus obj.value (obj=document.all["<%....) threw
object Error exception.

As for trace... I could output something like this (despite object Error
exception) on post back:

__VIEWSTATE
_ctl0:Title
_ctl0:Customer
_ctl0:Link
_ctl0:oHidden

The code was:

foreach(string s in Request.Form.Keys)
System.Diagnostics.Trace.WriteLine(s);

But the oHidden.Value in my server code remains "".

Am I annoying? :) This annoys me for the past few days.

Samo K.



For your script, you can do:
document.all["<%=oHidden.ClientID%>"].value = oDiv.InnherHTML;
Make sure the hidden field is inside your server-side form.
Then (with trace turned on) look at your form collection, and you should see
that the hidden field posted some data. You're on the right track. It'll
work for you.

In your code-behind, you do have a declared field like
protected HtmlInputHidden oHidden; right?

Jeffrey Palermo

SamoK said:
Thanks for your reply...
I've created the hidden field like this:

<input type="hidden" id="oHidden" runat="server">

The problem with this is that the server changes the IDs a bit and
when
I on
the the
 
<%@ Page language="c#" Inherits="System.Web.UI.Page" %>
<html>
<head>
<title>junk</title>
<script language="javascript">
function foo(){
document.all['<%=oHidden.ClientID%>'].value =
document.all['<%=oDiv.ClientID%>'].innerHTML;
}
function bar(textarea){
document.all['<%=oDiv.ClientID%>'].innerHTML = textarea.innerHTML;
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<input type=hidden id=oHidden runat=server>
<div id="oDiv" runat="server">
Initial value.
</div><br>
<textarea onkeypress="bar(this);">Initial value.</textarea><br>
<button onclick="foo();">copy div contents to hidden field.</button>
<asp:Button ID="btn" Runat="server" Text="Post Back"></asp:Button>
<br>
<asp:Literal ID="lit" Runat="server"></asp:Literal>
</form>
</body>
</html>
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
if(IsPostBack)
{
lit.Text = oHidden.Value;
}
}
</script>



SamoK said:
I've tried document.all["<%=oHidden.ClientID%>"]

but the value is undefined, thus obj.value (obj=document.all["<%....) threw
object Error exception.

As for trace... I could output something like this (despite object Error
exception) on post back:

__VIEWSTATE
_ctl0:Title
_ctl0:Customer
_ctl0:Link
_ctl0:oHidden

The code was:

foreach(string s in Request.Form.Keys)
System.Diagnostics.Trace.WriteLine(s);

But the oHidden.Value in my server code remains "".

Am I annoying? :) This annoys me for the past few days.

Samo K.



For your script, you can do:
document.all["<%=oHidden.ClientID%>"].value = oDiv.InnherHTML;
Make sure the hidden field is inside your server-side form.
Then (with trace turned on) look at your form collection, and you should see
that the hidden field posted some data. You're on the right track. It'll
work for you.

In your code-behind, you do have a declared field like
protected HtmlInputHidden oHidden; right?

Jeffrey Palermo

SamoK said:
Thanks for your reply...
I've created the hidden field like this:

<input type="hidden" id="oHidden" runat="server">

The problem with this is that the server changes the IDs a bit and
when
I on
the the
 
Hey, thanks for your time and example (I never doubted to what you say, it
just didn't work for me;)).
I found out what the problem was. It has something to do with fact that I
had all this stuff (Div, Hidden fields,...) in ascx control that was loaded
onto aspx control. And that's the reason why scripts wasn't functioning the
way I/we wanted them :)

I solved the problem now, and thanks again for taking time to clear some
things to me.

Regards,
Samo Kralj
 
Back
Top