tags:

views:

103

answers:

4

Hi All,

Is there any way to check which button as caused post back in the controllfer action. My problem is I am using two buttons with no name attribute. How can i check which button has clicked when button is not having name attribute Please advice

Edit : I can't give name attribute as i am using button controls created by internal framework in that they don't have name attribute. So is there any alternative for it.

I know there is one way putting those buttons in different form tags but i want to check for different options thanks

A: 

If you can give your button a name attribute the you could simply do..

<input ... name="MyBtn" .../>

If (Request.Form["MyBtn"] != null) ...
Rippo
A: 

MVC will check that for you. The submit button is connected to the Action which has the same name as the View.

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Id">Id:</label>
            <%= Html.TextBox("Id") %>
            <%= Html.ValidationMessage("Id", "*") %>
        </p>
        <p>
            <label for="Thema">Thema:</label>
            <%= Html.TextBox("Thema") %>
            <%= Html.ValidationMessage("Thema", "*") %>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

In the Controller you use HttpVerbs for the Get and the Post

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Add()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(ThemaWebSites thema)
        {
            return View(thema);
        }
Roger
A: 

You can use the ID attribute if you don't want to use a name, then use the "value" attribute:

<button type="submit" name="action" value="OK" />
<button type="submit" name="action" value="Cancel" />

You need a name, otherwise it won't show up in the Request.Form or FormCollection and you won't be able to access the values.

Edit: if you really can't use name or ID attributes, you might be forced to use some JavaScript trickery so that each button sets the value of a hidden field on their onclick event...

Keith Williams
thanks for the reply but i can't give name attribute
prakash
I'm kinda curious to know why not? What's the restriction?
Keith Williams
+1  A: 

To answer your question, there is no way to check which button caused the form to be submitted without giving it a unique name.

gWiz