user6634447 user6634447 - 2 months ago 27
ASP.NET (C#) Question

Data set from javascript getting cleared after postback

The values set from javascript is getting cleared after postback, I'm setting value to gridview process column from setdata button client click but when the button in start column is clicke the value set previously is cleared. Please see the code below and suggest a solution.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<script type="text/javascript">

function SetData(name, name2) {
document.getElementById(name).innerHTML = 'data.d.Name';
document.getElementById(name2).innerHTML = 'data.d.Name2';
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:GridView ID="gvMonitoring" runat="server" Width="100%" AllowPaging="false" PageSize="10"
AutoGenerateColumns="false" DataKeyNames="Id"
EnableViewState="true" OnRowDataBound="gvMonitoring_OnRowDataBound">
<asp:TemplateField HeaderText="Name">
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
<asp:TemplateField HeaderText="Name 2">
<asp:Label ID="lblName2" runat="server" EnableViewState="True" ></asp:Label>
<asp:TemplateField HeaderText="Set Data">
<asp:Button ID="btnSetData" runat="server" Text="Set Data" />
<asp:TemplateField HeaderText="Start">
<asp:Button ID="btnStartTime" runat="server" Text="Postback" />



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

public partial class ProcessDetail
public long Id { get; set; }
public string Name { get; set; }

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
var details = new List<ProcessDetail>();
for (var i = 1; i <= 5; i++)
details.Add(new ProcessDetail() { Name = "Process " + i});
gvMonitoring.DataSource = details;

protected void gvMonitoring_OnRowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
var lblName = ((Label)e.Row.FindControl("lblName"));
var lblName2 = ((Label)e.Row.FindControl("lblName2"));

((Button)e.Row.FindControl("btnSetData")).Attributes.Add("onclick", string.Format("SetData('{0}','{1}');return false;", lblName.ClientID, lblName2.ClientID));


gh9 gh9

To clarify @Imad, a postback is a complete page reload. Thus if you have any javascript it will also be reloaded. Which will clear out any values you stored. If you have to mix javascript with web forms there are two ways that I would recommend.

1) Your javascript will need to use ajax calls and update controls. Only using postback on a complete save. Meaning if you have a form and it takes Someones name/address. I would validate the data through ajax. And then, when it is all correct I would trigger a postback for save.

2) Create some Hidden Fields, that would hold the important var's that you need to save, and access those through server tags <%= =>. This way is pretty terrible but it can be done.