Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to change the text value of button1 on click of another button2 then fetching the text value of button1 in code behind on click event of button1 triggered through JavaScript.
*Button Definition
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" CssClass="Button2" Text="Button2_Old" />

*JavaScript Value set & button click
<script>
    $(document).on("click", ".Button2", function () {
        var clientid = document.getElementById("<%=Button1.ClientID %>");
        clientid.value = "XYZ";
        alert(document.getElementById("<%=Button1.ClientID %>").value);
        document.getElementById("<%=Button1.ClientID %>").click();
return false;
    });
</script>

*Code Behind

protected void Button1_Click(object sender, EventArgs e)
{
    string x = Button1.Text;
}

I am not able to get the updated value at code behind. No error on console, The text value of button 1 is blank not as it shouls be "XYZ". Please guide me.

What I have tried:

I have tried everything,i know but now, i am kind of blank. no idea how to take it forward from here.
Posted
Updated 27-Aug-19 20:09pm

1 solution

Since the Button1 was generated at the server with no Text value, after postback there will be nothing to restore it with.
Basically it's a post back and changes made by Javascript/Jquery on the client won't be visible on the server side

You can use A HiddenField To Get The value after postback if you have to do it

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="../Scripts/jquery-3.3.1.min.js" type="text/javascript"></script>
<script>
    function btnSetText_OnClientClick() {
          $("#<%= lbl1.ClientID %>").text("123");
          $('#HiddenFileldVariable').val($("#<%= lbl1.ClientID %>").text());
    }
</script>
</head>
<body>
<form id="form1" runat="server">
  <div>
    <asp:Label ID="lbl1" runat="server"></asp:Label>
    <asp:Button ID="btnSetText" runat="server" Text="Set Text" 
       OnClientClick="btnSetText_OnClientClick(); return false;" />
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
   <asp:HiddenField ID="HiddenFileld1" runat="server" />
  </div>
</form>
</body>
</html>


C#
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
      string str123 = HiddenFileld1.Value;
}
 
Share this answer
 
Comments
Sanjeev236 28-Aug-19 3:14am    
Actually in the given code, i need to press 2 buttons to get value where as i want it on single click. Value received by javascipt button click of button btnSubmit doesn't populate hidden value.

function btnSetText_OnClientClick() {
$("#<%= lbl1.ClientID %>").text("123");
$('#HiddenFileldVariable').val($("#<%= lbl1.ClientID %>").text());
$("#<%= btnSubmit.ClientID %>").click();
}
dnxit 28-Aug-19 4:00am    
Well in that case you can use the same Client Click to do the post back for you check this article

https://www.codeproject.com/Articles/4368/Post-an-ASP-NET-form-with-JavaScript
Sanjeev236 28-Aug-19 4:03am    
Thanks for links.I'll prefer to use web method over transferring postback to some different temp page.

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