Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I get javascript value to server side on post back with page contains master page?
Posted
Comments
Sandeep Mewara 28-Aug-10 5:22am    
Just in case, hope you are not talking of partial update via postback using Javascript == AJAX.

Set the value of a hidden input field.
 
Share this answer
 
The master page is utterly irrelevant. You should set a literal, so that it has viewstate, that's a lot simpler than creating a HTML control and searching for it in the post data.
 
Share this answer
 
Comments
AspDotNetDev 28-Aug-10 5:06am    
Are you talking about an asp:Literal control? How is that supposed to help transfer client state to the server on postback? Literal controls don't render to anything except for the text you set on them, so it's not like you can find that rendered literal and set a value on it that will get sent back to the server.
AspDotNetDev 28-Aug-10 5:11am    
I wasn't sure what you were talking about, so I added a working example as an answer.
Not sure what Graus is talking about, so I thought I'd create a working example. The page:
XML
<%@ Page Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs"
    Inherits="WebApplication1._Default" %>
<html>
<body>
    <form id="form1" runat="server">
    <div>
        <input runat="server" id="myData" type="hidden" />
        <script language="javascript">
            var field = document.getElementById("<%= myData.ClientID %>");
            field.value = "javascript set this value";
        </script>
        <asp:Button runat="server" ID="btnGo" Text="Go" />
    </div>
    </form>
</body>
</html>

The code behind:
C#
namespace WebApplication1
{
    using System;
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                btnGo.Text = myData.Value;
            }
        }
    }
}
 
Share this answer
 
Comments
Christian Graus 28-Aug-10 5:11am    
There's a way to generate a hidden field using an asp control, that's what I was talking about. But, using runat="server" on an html control works just as well, I'd just never do it that way.
AspDotNetDev 28-Aug-10 14:22pm    
Are you talking about asp:HiddenField? That renders to a hidden input field as well. Perhaps it has some other nifty features though.
I putted hidden field but when you fired
server side code the controls for server
side didn't affect
 
Share this answer
 
Comments
AspDotNetDev 28-Aug-10 14:22pm    
The example I posted works. I tried it myself. If you have a new question, post a new question as a new question, not as a fake 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