Abhijeet
Abhijeet

Reputation: 13856

Asp.net Button Click Event and MVC

When clicking the button for first time change button text, on 2nd click redirects to other page -

public partial class _Default : System.Web.UI.Page
{
    int clickCount;
    protected void btn_clicked(object sender, EventArgs e)
    {
        clickCount++;
        if (clickCount == 1)
            (sender as Button).Text = "go to landing page";
        else
            Response.Redirect("LandingPage.aspx");
    }
}

As obvious every time page reloads clickCount will be re-initialized and else block is never going to execute. To maintain the clickCount state I may use view state, session state, application state, hidden field or JS code may be etc. What is best for this particular case -> Ques 1.

As MVC is stateless and similar code written in controller as well, will re-initlialize clickCount every time ActionHandler or controller is instantiated. How do you handle similar situation in ASP.NET MVC -> Ques. 2

Upvotes: 1

Views: 4296

Answers (4)

user1477388
user1477388

Reputation: 21430

If I wanted to keep track of button clicks, I would use google analytics. If I wanted to keep track of them for within my own application, I would use a database (just make it update a database record on submit or click). If I didn't/ couldn't use a database, you could create a hidden field and keep updating it with the number of button clicks, like so:

Controller:

<HttpPost()>
Function MyFormPost(Optional ByVal clicks As Integer = 0) As ActionResult
  ViewBag.Clicks += clicks
  Return View()
End Function

View:

@Using Html.BeginForm()
    @Html.ValidationSummary(True)
    @Html.Hidden("clicks", ViewBag.Clicks)
End Using

Note: This is VB.NET.

Upvotes: 2

JConstantine
JConstantine

Reputation: 3931

This may not be the best solution to this problem, but it should put you on the right track.

public partial class _Default : System.Web.UI.Page
{
    protected void btn_clicked(object sender, EventArgs e)
    {
        int clickCount;
        try
        {
            clickCount = int.Parse(Session["ClickCount"]);
            clickCount++;
        }
        catch
        {
            clickCount = 1; 
        }

        Session["ClickCount"] = clickCount;

        if (clickCount == 1)
        {
            (sender as Button).Text = "go to landing page";
        }
        else
        {
            Response.Redirect("LandingPage.aspx");
        }
    }
}

By using Session it will persist if the user leaves the page and comes back. (Only within a certain time frame. If you want it to persist longer, then use a cookie.)

Upvotes: 1

Josh
Josh

Reputation: 10604

Create a model to pass into the view with the property on it:

public int ClickCount{get;set;}

Then in the view, create a hidden value

@Html.HiddenFor(f=>f.ClickCount)

During the post event on the controller, update the model

public ActionResult(ViewModel model){
model.ClickCount++;
}

Upvotes: 3

Andre Calil
Andre Calil

Reputation: 7692

Question 1

Depends:

  • do you need to use this click count anywhere else on the application?
  • is this count a sensible information?
  • is there a problem if the user sees this counting?

IMO, the best solution would be to keep the count on the viewstate, but it may change according to the above questions.

Question 2

It would be very different. To start, you wouldn't have a "button clicked event", you would get (or post) an action from your controller. So, as you can see, it would require a total different approach.

Side note: remember that it's not only MVC that is stateless. HTTP is stateless by design.

Upvotes: 1

Related Questions