ここで数日間Googleを検索して、JavaScriptのhiddenfield変数の値を取得できない理由を理解しようとしています。アクセスされると、値は未定義として返されます。
私は、.aspx Webページ(標準の問題)のカスタムユーザーコントロールの一部であるUpdatePanel内にASP HiddenFieldを持っています。JavaScriptのASP HiddenFieldにアクセスする
私のユーザコントロールでは、C#で設定した後、JavaScriptでHiddenField(hdnServer)の.Valueを取得する必要があります。しかし、なんらかの理由で、以下のことが正しい値を得ていない。
C#コードのMessageBoxは正しい値を返します(ここでのコードはテスト値を持っています)が、javascriptでアクセスすると未定義です。
userControl.ascx:コードビハインド
//this function is called when the timer created in document.ready() elapses
//returns the correct hdnServer value in the check.
var checkHdn = function() {
var temp = document.getElementById("<%=hdnServer.ClientID%>").value;
temp = temp.toString();
if (temp != "") {
$('#LoadingViewer').hide();
clearInterval(checkSrv);
//enable start button
$('#startBtn').attr("Enabled", "true");
}
};
function RdpConnect() {
//serverName = undefined here. should be ip address when set in c#
var serverName = document.getElementById("<%= hdnServer.ClientID %>").value;
alert(serverName);
if (serverName != "") {
MsRdpClient.Server = serverName;
}
};
userControl.ascx.cs:ここ
public partial class userControl : System.Web.UI.UserControl
{
System.Timers.Timer timer;
protected void Page_Load(object sender, EventArgs e)
{
timer = new System.Timers.Timer(5000);
timer.Start();
}
protected void testOnTick(object sender, System.Timers.ElapsedEventArgs e)
{
hdnServer.Value = "test value";
startBtn.Enabled = true;
timer.Enabled = false;
}
}
は、念のためのHiddenFieldためのASPがある:userControl.ascx:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
<!--trigger not used -->
<!-- <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />-->
</Triggers>
<ContentTemplate>
<asp:HiddenField ID="hdnServer" runat="server" />
<asp:Label ID="Label1" Text="Loading, please wait." CssClass="loading" runat="server"
Font-Size="XX-Large" />
</ContentTemplate>
</asp:UpdatePanel>
事前にアドバイスをありがとうございます!
編集:ここでメッセージボックスが削除.. がレンダリングされたHTML:http://pastie.org/3122247
レンダリングされたHTMLの外観はどうですか? –
MessageBox.Showを追加する理由それを除く。 – adatapost
隠しフィールドとは関係ありません。 – gdoron