1

i used asp.net user control in one of my MVC5 application. user controls call correctly and simply working, but i have problem in when i call any event used in user control are not postback. please give me any suggestion, where is issue and how can we use it. please help me to resolve.

see my code how i used in MVC5

user control code cs

public partial class uc_forum_two : ViewUserControl
{
 if (!IsPostBack)
 {
  // some code here..
 }
 else {
   // some code here..
 }

}

user control ascx code

<form id="test" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnableTheming="True">
    </telerik:RadScriptManager>

 <asp:LinkButton OnClick="btnForumSearch_Click" ID="btnForumSearch"
                            runat="server">
 <img src="/images/search_button.png" alt="search_button" />    
                        </asp:LinkButton>


 </form>

button code

protected void btnForumSearch_Click(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(txtForumSearch.Text))
            {
                Response.Redirect("/forum/search/" + txtForumSearch.Text.Replace(" ", "-"));
            }
            else
            {
                Response.Redirect("/forum/");
            }
        }
        catch
        {
        }

View Code

@{
   ViewBag.Title = "SmallBiz Org";
   Layout = "~/Views/Shared/_ClientLayout.cshtml";
}

@{ Html.RenderPartial("~/UserControls/uc_forum_two.ascx"); }

any alternate way to use user controls with postback event in mvc5 razor view. Please do help if someone can,thanks for your time.

adnan
  • 1,429
  • 1
  • 16
  • 26
  • 1
    Possible duplicate of [MVC 3: Add usercontrol to Razor view](http://stackoverflow.com/questions/6317317/mvc-3-add-usercontrol-to-razor-view) – ale Oct 27 '15 at 11:10
  • 2
    WebForm control events are not supported in MVC (nor is `ViewState`). Refer [Compatibility of ASP.NET Web Forms and ASP.NET MVC](https://msdn.microsoft.com/en-us/library/dd381619(v=vs.100).aspx) –  Oct 27 '15 at 11:11
  • @StephenMuecke thanks for your valuable input, is this due to separation of concerns ???? – Suhail Mumtaz Awan Oct 27 '15 at 11:14
  • 1
    @SuhailMumtazAwan, Partially, but one of the reasons asp.net-mvc was developed was to provide support for test-driven development which which is difficult to do with event handlers –  Oct 27 '15 at 11:39
  • @StephenMuecke. thanks for your time, its a bad way to use user control in mvc razor view, but i need one of exiting project, there is a lot of work in user controls, i need to use user controls as it is. thanks again for your valuable time. – adnan Oct 28 '15 at 03:53

0 Answers0