Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1805
  • Last Modified:

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
0
troyvw
Asked:
troyvw
  • 7
  • 5
1 Solution
 
abhinayp86Commented:
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
 
troyvwAuthor Commented:
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
 
raaziqCommented:
<%@ 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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


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

    </div>
0
 
raaziqCommented:
can u provide ur code pls
0
 
troyvwAuthor Commented:
HTML

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

asp.net code:

strhtml = IntroPar.InnerHtml

0
 
troyvwAuthor Commented:
That code returns "" for strhtml
0
 
raaziqCommented:
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
 
raaziqCommented:
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
 
troyvwAuthor Commented:
I get a error

Cannot get inner content of header1 because the contents are not literal
0
 
troyvwAuthor Commented:
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
 
troyvwAuthor Commented:
it worked thanks
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now