0
我正在加載UserControl
使用jQuery在aspx頁面中動態加載。但是我在UserControl
內觸發按鈕的點擊事件(以及下拉列表)時遇到了問題。它不會着火。使用C#和jQuery在ASP.NET中動態加載用戶控件後事件不會觸發
我想在ascx.cs文件(在按鈕單擊事件)上編寫insert/upadate代碼。 它可能有國家城市選擇等(我們必須去服務器獲得城市信息也許)
請在下面找到我的aspx,aspx.cs,ascx和ascx.cs文件代碼。
Default.aspx的
<%@ Page Title="Home Page" Language="C#" MasterPageFile="Site.master" AutoEventWireup="true"
Debug="true" EnableEventValidation="true" CodeFile="LoadControl.aspx.cs" Inherits="LoadControl" %>
<%@ Register Src="UserControls/Login.ascx" TagName="Login"
TagPrefix="uc3" %>
<%@ Register Src="UserControls/PersonelInfo.ascx" TagName="PersonelInfo" TagPrefix="uc4" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="js/blockUI.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#Button1').click(function() {
ControlLoad("PersonelInfo.ascx");
});
$('#Button2').click(function() {
ControlLoad("Login.ascx");
});
});
function ControlLoad(Name) {
$.blockUI({ message: '<h1> Processing...</h1><img src="img/loader.gif" />' });
$.ajax({
type: "POST",
url: "LoadControl.aspx/Result",
data: "{ controlName:'" + "UserControls/" + Name + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
$.unblockUI();
$("#result").fadeOut("slow", function() { $(this).html(response.d) }).fadeIn("slow");
},
error: function(msg) {
$.unblockUI();
$('#result').html(msg.d);
}
});
}
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#loaderGif1").hide();
$("#loaderGif2").hide();
$("#ddl_City").html("<option value=''>Select City </option>");
$("#ddl_Town").html("<option value=''>Select Town </option>");
$("#ddl_City").change(function() {
CityChange();
}
})
$("#ddl_Town").change(function() {
Change();
})
});
function CityChange() {
try {
$("#loaderGif1").show();
$("#ddl_Town").attr("disabled", "true").html("<option value=''> Select City</option>");
$("#ddl_Town").attr("disabled", "true").html("<option value=''>Select Town</option>");
var ilID = $("#ddl_City").val();
var pagePath = window.location.pathname;
$.ajax({
type: "POST",
url: pagePath + "/CityChange",
contentType: "application/json; charset=utf-8",
data: '{CityID:' + CityID + '}',
dataType: "json",
success: onSucceeded1,
error: onFailed
});
alert("succed");
return false;
}
catch (e) {
alert(e);
}
}
function onSucceeded1(result) {
$("#loaderGif1").hide();
$("#ddl_City").removeAttr("disabled").html(result.d);
$("#ddl_Town").removeAttr("disabled");
}
function onFailed(result) {
alert(result.d);
}
function TownChange() {
$("#loaderGif2").show();
$("#ddl_Town").attr("disabled", "true").html("<option value=''>Select Town</option>");
var CityID = $("#ddl_Town").val();
var pagePath = window.location.pathname;
$.ajax({
type: "POST",
url: pagePath + "/TownChange",
contentType: "application/json; charset=utf-8",
data: '{TownID:' + TownID + '}',
dataType: "json",
success: onSucceeded2,
error: onFailed
});
alert("succed");
return false;
}
function onSucceeded2(result) {
$("#loaderGif2").hide();
$("#ddl_Town").removeAttr("disabled").html(result.d);
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
</h2>
<p>
<input type="button" id="Button2" value="Login" />
<input type="button" id="Button1" value="Personel Info" />
<input type="button" id="Button2" value="Login" />
.
.
.
.
<div id="result">
</div>
</p>
</asp:Content>
general.ascx
[WebMethod]
public static string CityChange(string CityID)
我想我需要的地方綁定按鈕單擊事件。 (可能是我錯了)但不知道如何。
你是對我使我的用戶控制像那樣 – 2011-05-23 06:32:06
你是對的。我修復我的問題使用Jquery呈現我的用戶控件: function ControlLoad(Name){。{.block。{{message:'
正在處理...
'}); $ .ajax({type:「POST」, url:「LoadControl.aspx/Result」, data:「{controlName:'」+「UserControls /」+ Name +「'}」, contentType:「 application/json; charset = utf-8「, dataType:」json「, 成功:function(response){ $ .unblockUI(); $('#result')。html(response.d); }, 錯誤:函數(MSG){$ .unblockUI(); 警報(味精); $( '#結果')HTML(msg.d);} }); – 2011-05-23 07:21:34服務器網站我生成這樣的控制html: public static string Results(string controlName) { try { {page} = new Page(); 用戶控件用戶控件=(用戶控件)page.LoadControl(控件名稱); userControl.EnableViewState = true; HtmlForm form = new HtmlForm(); form.Controls.Add(用戶控件); page.Controls。添加(形式); StringWriter textWriter = new StringWriter(); HttpContext.Current.Server.Execute(page,textWriter,false); return textWriter.ToString(); } catch(Exception ex) { return ex.ToString(); } } – 2011-05-23 07:23:33