By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

sending data from javascript to C#

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Nov 18 '05 #2

P: n/a
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

"Bin Song, MCP" <an*******@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
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 get
oDiv.innerHtml

Bin Song, MCP

Nov 18 '05 #3

P: n/a
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.
"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:eH**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #4

P: n/a
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" <sn***@wetsoftware.com> wrote in message
news:Sd******************@news.siol.net...
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.
"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:eH**************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #5

P: n/a
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.

"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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" <sn***@wetsoftware.com> wrote in message
news:Sd******************@news.siol.net...
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.
"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:eH**************@TK2MSFTNGP11.phx.gbl...
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



Nov 18 '05 #6

P: n/a
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" <sn***@wetsoftware.com> wrote in message
news:OP******************@news.siol.net...
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.

"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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" <sn***@wetsoftware.com> wrote in message
news:Sd******************@news.siol.net...
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.
"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:eH**************@TK2MSFTNGP11.phx.gbl...
> 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



Nov 18 '05 #7

P: n/a

<%@ 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" <sn***@wetsoftware.com> wrote in message
news:OP******************@news.siol.net...
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.

"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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" <sn***@wetsoftware.com> wrote in message
news:Sd******************@news.siol.net...
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.
"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:eH**************@TK2MSFTNGP11.phx.gbl...
> 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



Nov 18 '05 #8

P: n/a
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

"Jeffrey Palermo" <je************@yahoo.com> wrote in message
news:ea*************@TK2MSFTNGP10.phx.gbl...
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

Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Browse more ASP.NET Questions on Bytes