Mega Code Archive
Categories
/
ASP.Net Tutorial
/
Sessions
Insert view state data in Page render event (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Untitled Page
File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ViewState["Example"] = "rntsoft.com"; } protected override void Render(System.Web.UI.HtmlTextWriter writer) { System.IO.StringWriter stringWriter = new System.IO.StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); base.Render(htmlWriter); string html = stringWriter.ToString(); int StartPoint = html.IndexOf("
= 0) { int EndPoint = html.IndexOf("/>", StartPoint) + 2; string viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint); html = html.Remove(StartPoint, EndPoint - StartPoint); int FormEndStart = html.IndexOf("") - 1; if (FormEndStart >= 0) { html = html.Insert(FormEndStart, viewstateInput); } } writer.Write(html); } }