Question

I am working on an MVC 3 project. In my view I have textboxes and Text area which I am calling using @Html.TextBox/TextArea.

Can anyone please tell me, how can I define the onclick or onfocus even of these controls? I am unable to find a syntax for this.

Was it helpful?

Solution

TextBox

@Html.TextBox("ControlName",
                "Some Value",
                  new { onclick = "alert('Razor Engine')" });
@Html.TextBoxFor(m => m.Value, new
                                {
                                    onclick = "alert('Razor Engine')",
                                    @Value = "Some Value"
                                });

TextArea

@Html.TextArea("ControlName",
                "Some Value",
                  new { onclick = "alert('Razor Engine')" });
@Html.TextAreaFor(m => m.Value, new
                                {
                                    onclick = "alert('Razor Engine')"
                                });

OTHER TIPS

Try something like below.

 <%= Html.TextBox("obpt9",ViewData.Eval("obpt9"), new { onclick = "alert('hi')" })%>

You can find more information from below link. Add OnClick Event to Html.RadioButton

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top