Solved

get the value of DIV in code behind

Posted on 2011-03-09
11
1,103 Views
Last Modified: 2012-05-11
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.

<script>
 var id = document.getElementById('id')
                     id.innerHTML = user.id
</script>


<div id="id" ></div>
0
Comment
Question by:Manoj Patil
  • 6
  • 4
11 Comments
 
LVL 9

Expert Comment

by:jkofte
ID: 35092229
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:

<script>
 var id = document.getElementById('<%=Div1.ClientID%>').innerHTML = user.id;
</script>
<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

0
 
LVL 19

Author Comment

by:Manoj Patil
ID: 35092390
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.
0
 
LVL 9

Expert Comment

by:gery128
ID: 35092640
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?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 19

Author Comment

by:Manoj Patil
ID: 35092887
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
0
 
LVL 9

Expert Comment

by:gery128
ID: 35093213
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 ?  
0
 
LVL 19

Author Comment

by:Manoj Patil
ID: 35093284
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">
             FB.init({
                 appId: '109418582471189', cookie: true,
                 status: true, xfbml: true
             });
             FB.api('/me', function Test (user) {
                 if (user != null) {
                    var id = document.getElementById('<%=Fid.ClientID%>').innerHTML = user.id;
}});
</script>

<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" />
          </div>

Open in new window


In Code Behind

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

Open in new window

0
 
LVL 9

Expert Comment

by:gery128
ID: 35094063
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.
0
 
LVL 9

Expert Comment

by:gery128
ID: 35094802
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.
0
 
LVL 19

Author Comment

by:Manoj Patil
ID: 35096835
I have tried a many ways.

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

0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 0 total points
ID: 35105596
Hello,
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.
0
 
LVL 19

Author Closing Comment

by:Manoj Patil
ID: 35135967
Changed code is working.............
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 24
Joomla 2.5 shopping cart 7 23
Footer for each row on Gridview 2 21
Linq asp.net mvc 13 6
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question