Events not fire after loading usercontrols dynamically in asp.net with c#
I am loading UserControl Dynamically in aspx page by using jquery.But I am facing problem while firing a click event of button (as well as dropdownlist) within the UserControl. It does not get fire.
I want to write insert/upadate code, in ascx.cs file (on button click event).
Please find below herewith my aspx, aspx.cs, ascx and ascx.cs file code.
-- Default.aspx --
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function(){
loadData("Home");
$("#tabHome").click(function(){
loadData("Home");
});
$("#tabGeneral").click(function(){
loadData("General");
});
$("#tabBusiness").click(function(){
loadData("Business");
});
$("#tabLogin").click(function(){
loadData("Login");
});
});
function loadData(pageName)
{
var ControlName = "forms/"+pageName+".ascx";
$.ajax({
type: "POST",
url: "Default.aspx/Result",
data: "{ controlName:'" + ControlName + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#divAjaxLoad').html(response.d);
},
error: function (msg) {
alert(msg);
$('#divAjaxLoad').html(msg.d);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="header">
<div class="companyTitle">Essar Vendor Management System</div>
</div>
<div id="topnav" style="text-align:center;">
<ul id="topnavlist">
<li><a href="javascript:void(0);" class="active" id="tabHome">Home</a></li>
<li><a href="javascript:void(0);" id="tabGeneral">General Details</a></li>
<li ><a href="javascript:void(0);" id="tabBusiness">Business Details</a></li>
<li><a href="javascript:void(0);" id="tabLogin">Login</a></li>
</ul>
</div>
<div id="gutter">
</div>
<div id="divAjaxLoad"></div>
</form>
</body>
</html>
-- Default.aspx.cs --
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string Result(string controlName)
{
return Results(controlName);
}
public static string Results(string controlName)
{
try
{
Page page = new Page();
UserControl userControl = (UserControl)page.LoadControl(controlName);
userControl.EnableViewState = false;
HtmlForm form = new HtmlForm();
form.Controls.Add(userControl);
page.Controls.Add(form);
StringWriter textWriter = new StringWriter();
HttpContext.Current.Server.Execute(page, textWriter, false);
return textWriter.ToString();
}
catch (Exception ex)
{
return ex.ToString();
}
}
}
-- general.ascx --
<input type="text" name="txtVendorName" id="txtVendorName" class="inputTypeText" />
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click1" />
-- general.ascx.cs --
protected void Button1_Click1(object sender, EventArgs e) //not fired
{
}
I think I need to bind button click event somewhere. (May be I am wrong) But dont know how?
Please help me to achieve the same.
Thanks and regards,
Vikas Parab