Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My requirement is, i want to assign value to label before page load using jquery/javascript then i want to assign the label text to string in page load.
JavaScript
<script type="text/javascript">
<pre lang="xml">$(document).ready(function () {
        function MyFun() {
            $("#lblName").val("Test");
           
        }
    });
</script>




C#
protected void Page_Load(object sender, EventArgs e)
   {
      string str=lblName.Text;
   }


in page load method i am getting empty string.

Anybody help me that how to assign value to asp.net controls before page load then how to access those assigned values in page load.

Thanks you.
Posted
Comments
Asgard25 25-Mar-14 5:30am    
And try it on LoadComplete
vsrikanth87 25-Mar-14 6:06am    
try in pageinit

Try this, the JavaScript was binded to a click but just play around with it.
FunctionnameXwill be a function you made that you call.


C# code on page load:
VB
if( Request.QueryString[ "Action" ].Equals( "onload" ) )

                                         FunctionnameX( Request.QueryString[ "lblname" ].Trim()



The JavaScript:

JavaScript
var VARlblNAME = $("#lblName").text;
window.location.href = '../foldername/pagename.aspx?Action=onload&lblname=' + VARlblNAME;



Let me know if you got it working.
 
Share this answer
 
Comments
ponnapureddy 25-Mar-14 7:58am    
It is not working. Please suggest me.
Asgard25 25-Mar-14 8:52am    
Try removing TRIM() as it's not really needed, except if you actually need to trim it.
Getting object reference exception.

The path of the project is D:\Demos\LangugeDemo\Default3.aspx

http://localhost:64067/LanguageDemo/Default3.aspx[^]

the below is aspx page.

HTML
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>]]>
 
Share this answer
 
Comments
Asgard25 25-Mar-14 8:49am    
I have no idea how your code looks cause your hosting that locally..., but just make sure your IF statement is correct, just think by what I reckon is happening is that your calling an object that is null or not created yet
XML
<pre lang="HTML">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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">
    <script type="text/javascript">
        debugger;
        function start() {
            document.getElementById('Hidden1').value = "somme value";
            var VARlblNAME = $("#lblName").text;
            window.location.href = 'LangugeDemo/Default3.aspx?Action=onload&lblname=' + VARlblNAME;
        }
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <input type="text" id="Hidden1" name="Hidden1" runat="server" />
    <div>
        <input type="submit" value="submit" />
    </div>
    </form>
</body>
</html>


</pre>


<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        if( Request.QueryString[ "Action" ].Equals( "onload" ) )
        {

            string message = Request.QueryString["lblname"].Trim();
        }
    }
}

</pre>

Action value getting as null.

Please give me the solution.

Thanks in advance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900