Link to home
Start Free TrialLog in
Avatar of troyvw
troyvwFlag for United States of America

asked on

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

How would i get the value of the inner html of a html div tag into a asp.net variable
Avatar of abhinayp86
abhinayp86
Flag of United States of America image

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
Avatar of troyvw

ASKER

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.

Avatar of raaziq
raaziq

<%@ 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
Avatar of troyvw

ASKER

I can read my div tags in asp.net but they are all blank even though I put something in the inner html
u placed div tag in the same manner ???


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

    </div>
can u provide ur code pls
Avatar of troyvw

ASKER

HTML

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

asp.net code:

strhtml = IntroPar.InnerHtml

Avatar of troyvw

ASKER

That code returns "" for strhtml
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.
ASKER CERTIFIED SOLUTION
Avatar of raaziq
raaziq

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of troyvw

ASKER

I get a error

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

ASKER

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"
Avatar of troyvw

ASKER

it worked thanks