Because you are just calling SetName javascript fuction on page load but you are not posting back the page by ckicking any button so how can the Request.Form[hfName.UniqueID] will return the value.
Refer the below sample code for your reference here i just added one Link Button which dont have click event. On SetName javascipt fuction i just calling the LinkButton Click event so it will postback the page and return the HiddenField value from Request.Form[hfName.UniqueID].
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblName" Text="Name" runat="server" />
<asp:HiddenField ID="hfName" runat="server" />
<asp:LinkButton ID="lnkfake" runat="server"></asp:LinkButton>
<br />
<br />
<script type="text/javascript">
function SetName() {
var label = document.getElementById("<%=lblName.ClientID %>");
//Set the value of Label.
label.innerHTML = "Mudassar Khan";
//Set the value of Label in Hidden Field.
document.getElementById("<%=hfName.ClientID %>").value = label.innerHTML;
$("[id*=lnkfake]")[0].click();
}
</script>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowDate", "SetName()", true);
}
if (this.IsPostBack)
{
lblName.Text = Request.Form[hfName.UniqueID];
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Page.ClientScript.RegisterStartupScript(Me.[GetType](), "ShowDate", "SetName()", True)
End If
If Me.IsPostBack Then
lblName.Text = Request.Form(hfName.UniqueID)
End If
End Sub
Screenshot
