Click here to Skip to main content
15,891,702 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 buttons in my view page. One button is a normal button that uses javascript code to add rows dynamically in a table and the other is a submit button.I want a post action to happen when the user clicks on the button that adds the rows in table,ie, I want to know when a row is being added by the user.
Posted

"Identifying which button has been clicked" would be a fundamental abuse of any event-oriented technology, MVC or not, ASP.NET or anything else. This is not how it works. You create/add a button and provide an event handle to each button individually. This way, you don't need to know which button was clicked: the event handler already does what that button was supposed to be used for.

Only in rare cases, when you have a lot of buttons doing nearly the same with some variation, identification of the button might be needed, to pass some data specific to a particular button, usually, something like an index in the array of controls. Sometimes, such technique is useful, but… mostly for edit boxes, check boxes and other similar controls. For buttons… no, it would rather indicate really bad UI design. I don't believe you needs that; don't even want to discuss such things. Better use the approach I described in the first paragraph.

—SA
 
Share this answer
 
Add an attribute in the submit button like below
HTML
<input type="submit" id="btnSubmit" name="button" class="button" value="Submit" />


and Access the button name from the action result like below
C#
[HttpPost]
public ActionResult Method(string button)
{
if(button=="yoursubmitbutton")
{
}
else
{
}

}


Hope this helps
 
Share this answer
 
v2
Thanks Jameel.I did get the button clicked using the code provided by you but have one problem,on giving the name attribute in the input tag for the button my post action doesn't work for any control(dropdownlist,textbox,etc).Is there any solution to that problem?
 
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