我有四個相同的UserControl插入到Default.aspx中。我想將數據寫入HiddenField並使用javascript將這些數據寫入Label1。asp.net usercontrols中的Javascript變量 - 在同一插入的usercontrol中覆蓋變量
該方法僅適用於上次加載的UserControl - HiddenFieldLoader3。
爲什麼該方法不適用於所有的用戶控件?我如何修復我的代碼?
Default.aspx的
<%@ Register Src="~/HiddenFieldLoader.ascx" TagPrefix="uc1" TagName="HiddenFieldLoader" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Default.aspx</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="HiddenFieldLoader.js"></script>
</head>
<body>
<form id="form1" runat="server">
<uc1:HiddenFieldLoader runat="server" ID="HiddenFieldLoader" />
<uc1:HiddenFieldLoader runat="server" ID="HiddenFieldLoader1" />
<uc1:HiddenFieldLoader runat="server" ID="HiddenFieldLoader2" />
<uc1:HiddenFieldLoader runat="server" ID="HiddenFieldLoader3" />
</form>
</body>
</html>
用戶控件:HiddenFieldLoader.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HiddenFieldLoader.ascx.cs" Inherits="NewControls.HiddenFieldLoader" %>
<script type="text/javascript">
HFLoader.declareVariables("<%=Button1.ClientID %>", "<%=HiddenField1.ClientID %>", "<%=Label1.ClientID %>");
</script>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:HiddenField ID="HiddenField1" runat="server" />
<input type="button" ID="Button1" value="Save" runat="server" />
<input type="button" ID="Button2" onclick="HFLoader.showEvent();" value="Show" runat="server" />
JavaScript文件:HiddenFieldLoader.js
HFLoader = {
button: null,
hiddenField: null,
label: null,
declareVariables: function (btn, hf, label) {
HFLoader.button = btn;
HFLoader.hiddenField = hf;
HFLoader.label = label;
},
///////SHOW EVENT
showEvent: function() {
$("#" + HFLoader.label).html($("#" + HFLoader.hiddenField).val());
},
//////!SHOW EVENT
saveHiddenField: function (data) {
$("#" + HFLoader.hiddenField).val(data);
},
buttons: function() {
$("#" + HFLoader.button).click(function() {
var datatest = "Data from button ID: " + $(this).attr("id");
HFLoader.saveHiddenField(datatest);
$("#" + HFLoader.label).html($("#" + HFLoader.hiddenField).val());
return false;
});
},
init: function() {
$(function() {
HFLoader.buttons();
});
}
};
HFLoader.init();
輝煌的代碼!非常感謝你! :-) – Jenan
我添加了一個函數來顯示消息 - showEvent和帶有ID - Button2的按鈕。我怎樣才能改變你的方法來調用這個功能?謝謝! – Jenan
我在嘗試這個「onclick =」HFLoader.showEvent();「」,它不起作用。我怎樣才能解決這個問題?謝謝@Eli Gassert – Jenan