Passing values from javascript to code behind in ASP.NET

 

This article, I will show you how to pass values from client side means javascript from code behind i.e

C# with complet example and source code

$ads={1}} <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PassFromClientToServer.aspx.cs" Inherits="PassFromClientToServer" %> <!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>Untitled Page</title> <script type="text/javascript"> function Pass() { var hidden = document.getElementById("ContentType"); hidden.value = "test value from client side"; var frm = document.getElementById("form1"); frm.submit(); } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="hidden" id="ContentType" name="ContentType" /> <asp:TextBox ID="txt1" runat="Server"></asp:TextBox> <asp:Button ID="btnClick" runat="server" Text="Test" OnClientClick="Pass();" /> </div> </form> </body> </html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class PassFromClientToServer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form["ContentType"] != null)
        {
            string myHiddenFiledValue = Request.Form["ContentType"].ToString();
            txt1.Text = myHiddenFiledValue.ToString();
        }

    }
   
}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru