locked
Access client side value in server side RRS feed

  • Question

  • User-1766695803 posted

    My aspx page has one textbox control ,2 button controls and 1 label. I have a javascript function which fills the textbox with "hello" when i click button1. This happens in the client side. I want to retrieve this value of the textbox and show that in the label on button2_click. This happens on server side.
    How do i retrieve cloent side value in server side?
    Example code help will be appreciated
    function Addtext(){
    var TheTextBox = document.getElementById("TextBox1");
     TheTextBox.value = "Hello";
    }
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick ="Addtext()"></asp:Button>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick ="Button2_Click"></asp:Button>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    Tuesday, May 10, 2011 6:39 AM

Answers

  • User369957501 posted

     

    To read the textbox value on server side

     

     protected void Button2_Click(object sender, EventArgs e)
        {
          string txtValue = TextBox1.Text;
        }

    Or Create a hidden button

     

    <input id="inpHide" type="hidden" runat="server" /> 

     

    function Addtext(){
    var TheTextBox = document.getElementById("TextBox1");
     TheTextBox.value = "Hello";
               var hiddenControl = '<%= inpHide.ClientID %>';
                document.getElementById(hiddenControl).value=TheTextBox.value;
    }
    

     

    and read the hidden label in the same way as I have shown in the above example

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2011 7:12 AM
  • User1867929564 posted

    javascript code,
    function Addtext()
    {
     document.getElementById("TextBox1").value="Hello";
       } 
    button2 click event write,
    Label1.Text=TextBox1.Text;  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2011 8:21 AM

All replies

  • User-1028151323 posted

    on your button click event:

    dim tbText as string = TextBox1.text

    Tuesday, May 10, 2011 7:10 AM
  • User369957501 posted

     

    To read the textbox value on server side

     

     protected void Button2_Click(object sender, EventArgs e)
        {
          string txtValue = TextBox1.Text;
        }

    Or Create a hidden button

     

    <input id="inpHide" type="hidden" runat="server" /> 

     

    function Addtext(){
    var TheTextBox = document.getElementById("TextBox1");
     TheTextBox.value = "Hello";
               var hiddenControl = '<%= inpHide.ClientID %>';
                document.getElementById(hiddenControl).value=TheTextBox.value;
    }
    

     

    and read the hidden label in the same way as I have shown in the above example

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2011 7:12 AM
  • User-2139489267 posted

    Two ways has been shown by Anuj already. Please check it out and let us know if you encounter any problem.

    Tuesday, May 10, 2011 7:27 AM
  • User-1766695803 posted

    protected

    void Button2_Click(Object sender, EventArgs e)

    {

    Label1.Text = inpHide.Value;

    }

    the label does not render "hello". The label becomes empty

    Tuesday, May 10, 2011 7:43 AM
  • User1867929564 posted

    javascript code,
    function Addtext()
    {
     document.getElementById("TextBox1").value="Hello";
       } 
    button2 click event write,
    Label1.Text=TextBox1.Text;  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2011 8:21 AM