0

my asp project has a asp button which named 'Save'. However, i need to change it to 'Update' in jQuery to do other thing in one button.

The problem is the C# onclick code is still read it as Save..

Code:

//Main
<asp:Button ID="btnSubmit" runat="server" Text="Save" />

//JQuery
 $("#<%=btnSubmit.ClientID %>").val("Update");

//C#
 var button = sender as Button;
var buttonName = button.Text; //Save

Update1

Question is: I need to change the button name in JQuery

$("#<%=btnChangeName.ClientID %>").click({
     $("#<%=btnSubmit.ClientID %>").val("Update"); //Change Save to Update
});

Then use it in C#:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
              var button = sender as Button;
              var buttonName = button.Text;
              if (buttonName == "Save")
              {//something}
              else
              {//something}
        }
king yau
  • 500
  • 1
  • 9
  • 28
  • If you change the Button.OnClick value, then it will move to a different method onClick; Button.Text = "Update";Button.OnClick = "ButtonUpdate";/Button.Text="Save";Button.OnClick="ButtonSave"; – Shannon Holsinger Aug 31 '16 at 15:03
  • @kingyau why cant u change thae name in the button_click itself – Krsna Kishore Aug 31 '16 at 15:15
  • @Webruster In my form, user can either create new item by selecting create => "Save" or update existing item by selecting edit => "Update" – king yau Aug 31 '16 at 15:20
  • @kingyau got it, will give u solution !! – Krsna Kishore Aug 31 '16 at 15:21
  • Why not set the button text in the code behind on page load? If a user selects an item to edit, set `button.Text = "Update";`, otherwise `button.Text = "Save";`. – Bryan Woodford Aug 31 '16 at 15:33
  • @BryanWoodford because any postback will destroy my code until the user clicks the save/ update button. – king yau Aug 31 '16 at 15:37
  • @kingyau - Surely, on postback, you will have the saved object and can change the value of the button accordingly? – Bryan Woodford Aug 31 '16 at 15:41
  • @BryanWoodford i also have plan bcdefg (e.g. double button hidden one ) if it is not possible to get the new button name. Moreover, i want my website has less refreshing. – king yau Aug 31 '16 at 15:50
  • @kingyau - I was going to suggest having two buttons as an alternative :) – Bryan Woodford Aug 31 '16 at 15:51
  • You really don't need to change the text - just call it save/update and test for existing record in the onclick; save if no, update if yes. – Shannon Holsinger Sep 01 '16 at 00:07
  • @ShannonHolsinger Hey, i found this [post](http://stackoverflow.com/questions/13479430/jquery-pass-asp-button-command-argument-with-jquery). Will it fits to my case? – king yau Sep 01 '16 at 02:30
  • It looks as though it might, but it surely seems like you're reinventing the wheel. My answer(below) seems a lot more C-A-T = "Cat" as opposed to C-A-T = "Felus F. Catus." If you drop-dead want to do it with JQuery then it looks like you're on the right track! – Shannon Holsinger Sep 01 '16 at 02:55

2 Answers2

1

if you want to change the Name to Update1 in Jquery

try like this

$("#<%=btnSubmit.ClientID%>").val('Update');

Event you neeed to use is OnClientClick

Usage example

OnClientClick="onupd();return false;"

Update

Decalre a Hidden FIeld

<asp:HiddenField runat="server" ID="hdntest" Value="0" />

assign the button change value to update and assign it to the hidden field

not access that hidden value in your code behind.

you can assign Button value to Hidden Field like this

$("#<%=btnSubmit.ClientID %>").val("Update");

 $("#<%=hdntest.ClientID%>").val($("#<%=btnSubmit.ClientID%>").attr("value"));
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
-1

If you change the Button.Click value, then it will move to a different method onClick;

private void Form1_Load(object sender, EventArgs e)
    {
        Button b = new Button();
        b.Name = "btnSubmit";// creating here for intellisense - you would create on your front-end
        bool recordExists = true;

        Button b1 = Controls.Find("btnSubmit", true)[0] as Button;
        //the above is for winforms - you'd have to change for web form
        b1.Text = recordExists ? "Update" : "Save";
        b1.Click += doUpdateOrSave;

    }
    protected void doUpdateOrSave(object sender, EventArgs e)
    {
        Button b = sender as Button;
        if (b.Text == "Save")
        {
            //save
        }else
        {
            //update
        }
    }
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21