get the value of DIV in code behind

I am getting Facebook User ID by following script code. I am binding it to the Div.
Now I wanted to save it in string and store in database. When I create a div as a server control then I didn't get the user ID.
How can I get the div value in string in code behind page.

 var id = document.getElementById('id')
                     id.innerHTML =

<div id="id" ></div>
LVL 19
Manoj PatilSr. Software EngineerAsked:
Who is Participating?
Manoj PatilConnect With a Mentor Sr. Software EngineerAuthor Commented:
I have change the Business Logic. Now I am creating a session through JavaScript. And this works for me.  Because from the previous way I didn't find the solution.
it must have runat = "server" and in js, use ClientID to get the clientside ID of the div.
so your code will be like this:

 var id = document.getElementById('<%=Div1.ClientID%>').innerHTML =;
<div id="Div1" runat="server" ></div>

Open in new window

this way in code behind you can use:

Div1.innetHTML = "HTML CODE"

Open in new window

Manoj PatilSr. Software EngineerAuthor Commented:
Thanks jkofte,

I have done the way you explained. I have created div as server control.
But in code behind how can I get the value in string..???

You have written Div1.innerHTML="HTML CODE"  ........I didn't understand.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

make your DIV server control first, by putting property runat="server" .
for example:  <div id="myContentDiv" runat="server" />

than you can access it in code-behind (cs/vb file) just like another controls.

like in C#:
string userId = myContentDiv.InnerHTML;

What are you using ? A web application or a web site project of ASP.NET?
Manoj PatilSr. Software EngineerAuthor Commented:
I have tried following code. I am using website Project of ASP.NET
string getID = id.InnerHtml;
string getID1=id.InnerText;

still i got null value
On which event you are attempting to access Div's value. It may be possible that, the values is not assigned yet and you are accessing it through code-behind.

Div value should be assigned first with JS and than it will be accessible in server side code. So check the event and when the Div value is set in javascript.

Can you post some code here ?  
Manoj PatilSr. Software EngineerAuthor Commented:
I have a buton for login with facebook. When I logged in with facebook then I got user ID , Email, Profile Picture, Birthday through JavaScript and In Javascript. I am assigning the values to the div and those values are display also. And on Button Click I am getting the Values in Code Behind

 <script type="text/javascript">
                 appId: '109418582471189', cookie: true,
                 status: true, xfbml: true
             FB.api('/me', function Test (user) {
                 if (user != null) {
                    var id = document.getElementById('<%=Fid.ClientID%>').innerHTML =;

<div align="center">
           <table><tr><td> FBID=</td><td><div id="Fid" runat="server"></div></td></tr></table>
          <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Test()" onclick="Button1_Click" />

Open in new window

In Code Behind

protected void Button1_Click(object sender, EventArgs e)
        String gs = Fid.Value;

Open in new window

You are assigning the value to DIV before defining it in markup.
Move your script to after the DIV markup so it can assign value to it.
techChallenger1, I tried with ASP.NET 2.0 website, yes, you are correct, its behaving strange. While Hidden field worked perfectly in this case. I set value through javascript and accessed through my server side code. But Div did not behave normal in that case. I also set enableviewstat property of DIV to true.
Even SPAN tag behaved same way like DIV tag.
Manoj PatilSr. Software EngineerAuthor Commented:
I have tried a many ways.

Is there any other way to do this.......???

Manoj PatilSr. Software EngineerAuthor Commented:
Changed code is working.............
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.