我有以下内容:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ctlChatBox.ascx.cs" Inherits="SignalRPrivateChat.controls.ctlChatBox" %>
<div id="chat_widnow">
<div id="chat_title_bar"> <span class="col-sm-9 text-primary"><strong>Online Users</strong></span>
<div id="chat_min_button"><i class="fa fa-plus-square"></i></div>
</div>
<div id="chat_box" style="display: none;overflow-y:auto;">
</div>
</div>
<div id="chat_div"></div>
<input id="hdId" type="hidden" />
<input id="hdUserName" type="hidden" />
<asp:HiddenField ID="hdnCurrentUserName" runat="server" />
<asp:HiddenField ID="hdnCurrentUserID" runat="server" />
<script src="<%=Page.ResolveUrl("~") %>Scripts/jquery.signalR-2.2.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="<%=Page.ResolveUrl("~") %>signalr/hubs"></script>
<link href="<%=Page.ResolveUrl("~") %>styles/jquery.ui.chatbox.css" rel="stylesheet" />
<script src="<%=Page.ResolveUrl("~") %>scripts/jquery.ui.chatbox.js"></script>
<script src="<%=Page.ResolveUrl("~") %>scripts/chatboxManager.min.js"></script>
其中hdnCurrentUserName
和hdnCurrentUserID
是HiddenField
s,但是当我在这里调用它们时:
public partial class ctlChatBox : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null && Session["UserId"] != null)
{
hdnCurrentUserName.Value = Session["UserName"].ToString();
hdnCurrentUserID.Value = Session["UserId"].ToString();
}
}
}
我得到一个错误:
名称"hdnCurrentUserName"在当前上下文中不存在。的name "hdnCurrentUserID"在当前上下文中不存在。
为什么会发生这种情况?
ascx控件有一个指向页面的引用,我们可以在其中使用。
this.Page
现在我们需要从该页获得一个控件。因为我们知道它的名字你可以使用FindControl
HiddenField Info = this.Page.FindControl("hdnfldIncDesc");