Frank Myat Thu
Frank Myat Thu

Reputation: 4474

get html DIV content from asp.net c# codebehind event

I want to get HTML DIV content via asp.net C# code behind event.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="WebApplication1.Report.Report" %>
<!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> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript">

    $(document).ready(function () {

        $('#_Hidden_CrystalReportContent').hide();
        $('#_Hidden_CrystalReportContent').html("<b>I want to get Current value. 1<sup>st</sup></b>");

    });        
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="_Hidden_CrystalReportContent">I want to get Current value.</div>    
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>

My code behind file as below.

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

protected void Button1_Click(object sender, EventArgs e)
{
    string s = Request["_Hidden_CrystalReportContent"].ToString();
}
}

But I still cannot get div content value.
Please let me get your suggestion.

Upvotes: 1

Views: 15423

Answers (1)

Adil
Adil

Reputation: 148180

Make the div runat="server" to access on server.

Html

<div id="_Hidden_CrystalReportContent" runat="server">

Code behind

string htmlOfDiv = _Hidden_CrystalReportContent.innerHTML;

Javascript

$(document).ready(function () {

    $('#<% _Hidden_CrystalReportContent.ClientID %>').hide();
    $('#<%= _Hidden_CrystalReportContent.ClientID %>').html("<b>I want to get Current value. 1<sup>st</sup></b>");

});        

Making a div server accessible by puttin runat="server" attribute cause the changed client id if CLientIDMode is not static. You will need to use ClientID attribute to get client id of div in javascript.

Edit: based on comments. You are trying to get the updated html, if so you then you wont get it as on post back only html form elements are posted. Put the changes in some hidden field and assess it on server.

In html

<input type="hidden"  id="hdnDivContents" runat="server">

In javascript

$('#<% hdnDivContents.ClientID %>').val("<b>I want to get Current value. 1<sup>st</sup></b>");

In code behind

 _Hidden_CrystalReportContent.innerHTML = hdnDivContents.Value;

Upvotes: 2

Related Questions