繁体   English   中英

在页面上的UserControl中获取页面的HiddenField值

[英]get the Page's HiddenField Value in UserControl on Page

我想读取放置在该页面上的用户控件中的Page(.aspx)隐藏字段值,并处理一些逻辑。

例如:我在页面上有一个隐藏字段x。 该页面有许多用户控件,我想访问那些用户控件中的该隐藏字段(x),其中x的值将由该页面中的Javascript设置。

我试图找到HiddenControl并从usercontrol(.ascx.cs)的代码隐藏中读取其值,但始终为null。

HiddenField colname = UIUtils.FindControlRecursive(this.Parent.Page, "MainContent_AssignedTo_ColName") as HiddenField;

该ID与客户端上的hiddenfield相同。 我也尝试了this.Parentthis.Parent.Parent作为第一个参数,但是没有运气。

我在这里想念什么。

尝试:

HiddenField colname = (HiddenField)Page.FindControl("The id of control");

这是我的aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestFindControl._Default" %>
<%@ Register Src="ReadHiddenField.ascx" TagName="Assign" TagPrefix="uc1" %>

<!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">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:HiddenField ID="HiddenField1" runat="server" Value="10">
    </asp:HiddenField>

<asp:placeholder ID="Placeholder1" runat="server"><uc1:Assign id="test" runat="server"></uc1:Assign> </asp:placeholder> 
    </div>
    </form>
</body>
</html>

这是我的控制代码:

protected void Page_Load(object sender, EventArgs e)
        {
            HiddenField test = (HiddenField)Page.FindControl("HiddenField1");
            var j = test.Value;

        }
private void GetParentPageHiddenField()
    {
        System.Web.UI.WebControls.HiddenField ParenthiddenField = null;
        Control ctl = this.Parent;
        while (true)
        {
            ParenthiddenField = (System.Web.UI.WebControls.HiddenField)ctl.FindControl("ParentPageHiddenFieldID");
            if (ParenthiddenField == null)
            {
                if (ctl.Parent == null)
                {
                    return;
                }
                ctl = ctl.Parent;
                continue;
            }
            break;
        }
        var parentHiddenFieldValue=ParenthiddenField.Value;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM