Reputation: 343
Hello,
I know razor is different from webforms but i don't want to mix a lot too.
I created ASP.NET MVC WebForms with Masterpages includes Pages and UserControls. It's working fine but i would like to know about how to handle click events in MVC Webforms?
Codebehind file is still there for example default.aspx.cs, if i can double click on button then it creates click event but not working.
Can we use asp.net components in MVC? What about Code Behind file?
(Example Controls: asp:label, asp:textbox, asp:hyperlink etc.)
Thanks,
UPDATE
This is sample code (contact.aspx):
<div class="contact_text">
<asp:Label ID="lbl_namesurname" runat="server" Text="<%$ Resources:contact, lbl_namesurname.Text%>"></asp:Label>
</div>
<div class="txt_namesurname">
<asp:TextBox ID="txt_namesurname" runat="server" ValidationGroup="contact"></asp:TextBox>
</div>
<div><asp:Button ID="btn_send" runat="server" CssClass="btn_send" Text="<%$ Resources:contact, btn_send.ToolTip%>" ToolTip="<%$ Resources:contact, btn_send.ToolTip%>" OnClick="btn_send_Click" ValidationGroup="contact"/></div>
contact.aspx.cs file:
public partial class contact : ViewPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_send_Click(object sender, EventArgs e)
{
//old code-behind is not working in mvc
}
}
}
Upvotes: 0
Views: 523
Reputation: 32699
There is no such thing as MVC Web Forms. There is ASP.NET MVC and ASP.NET Web Forms. Different technologies, and you can't mix them (though they can sit alongside each other).
MVC does not have the same lifecycle as Web Forms, and therefore you cannot have a code behind or User Controls. MVC must be thought about differently: a request gets routed to a controller. The controller examines the request, makes any calls to a data retrieval layer that it needs to, chooses a view, and passes any necessary data to that view, which is rendered to HTML and sent to the client. Subsequent communication goes through that same process, unless you use AJAX or similar technologies.
So, think about what that button would be doing. Is it supposed to send an email? Then it should be a form submission. Is it supposed to gather some more data and update the DOM on the already loaded page? Then AJAX is probably more appropriate.
Upvotes: 4