簡體   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