Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.net get the value of a div tag into a string variable

Posted on 2010-08-27
13
Medium Priority
?
1,798 Views
Last Modified: 2012-05-10
How would i get the value of the inner html of a html div tag into a asp.net variable
0
Comment
Question by:troyvw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 9

Expert Comment

by:abhinayp86
ID: 33542941
If i understand correctly, u need all the things that lie in ur div tag from code behind.

For that, put the attributes runat= "server" and id="div1"

n in ur code behind u can try like, div1.Controls
0
 

Author Comment

by:troyvw
ID: 33543017
I did this in my html:

<div id="header1" runat="server"</div>

and tried this in my code

div1.Controls

and it did not know what header1 was.

0
 
LVL 3

Expert Comment

by:raaziq
ID: 33543440
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="div1" runat=server>
    Sample
    </div>
    </form>
</body>
</html>

------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("From Code : " + div1.InnerHtml);
        }
    }
}

-----------------------------------------------------------------------

output

From Code : Sample
 
Sample
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:troyvw
ID: 33543547
I can read my div tags in asp.net but they are all blank even though I put something in the inner html
0
 
LVL 3

Expert Comment

by:raaziq
ID: 33543621
u placed div tag in the same manner ???


  <div id="div1" runat=server>
 
    Sample

    </div>
0
 
LVL 3

Expert Comment

by:raaziq
ID: 33543630
can u provide ur code pls
0
 

Author Comment

by:troyvw
ID: 33543824
HTML

<div id="IntroPar" runat="server"></div>

asp.net code:

strhtml = IntroPar.InnerHtml

0
 

Author Comment

by:troyvw
ID: 33543828
That code returns "" for strhtml
0
 
LVL 3

Expert Comment

by:raaziq
ID: 33543881
there is nothing in between the div tag... as per ur code...

so it will return ""

check and compare my code snippets given... and it gives wht ever there in between the div opening and closing tags

hope u got the thing.
0
 
LVL 3

Accepted Solution

by:
raaziq earned 2000 total points
ID: 33544198
U may try this code...

======================================
<%@ Page ValidateRequest="false" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
   
</head>
<body>
    <form id="form1" runat="server">
   
    <div id="div1" runat=server>  
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    <asp:HiddenField ID="divHidden" runat=server  />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>

<script language=javascript>    
    var divVal = document.getElementById('<%= div1.ClientID %>').innerHTML;    
    var hid = document.getElementById('<%= divHidden.ClientID %>')
    hid.value = divVal;
</script>


</body>
</html>
===============================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(divHidden.Value);
        }

     
    }
}
==========================================

it works
0
 

Author Comment

by:troyvw
ID: 33544626
I get a error

Cannot get inner content of header1 because the contents are not literal
0
 

Author Comment

by:troyvw
ID: 33545299
Never mind on my last post i figure that portion out...

maybe i should mention header i am setting the innerhtml in code

div1.innerhtml = "TEST"
0
 

Author Closing Comment

by:troyvw
ID: 33546187
it worked thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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