【问题标题】:Cannot access hiddenfield value in webform aspx with MasterPage无法使用 MasterPage 访问 webform aspx 中的隐藏字段值
【发布时间】:2020-05-06 08:10:55
【问题描述】:

我正在尝试从我的 webform aspx 页面中设置的 masterpage 访问隐藏字段 hidJsonHolder 值,但无法通过母版页 codebehind 访问它。

我怎样才能让它工作?

看起来很简单,但是不知道为什么不起作用...隐藏字段hidJsonHolder值为null...

我在没有母版页的 webform aspx 中尝试了相同的代码并且工作正常......

Mp cs

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField hid1 = (HiddenField)ContentPlaceHolder1.FindControl("hidJsonHolder");
        if (hid1 != null)
        {
            Response.Write(hid1.Value);
        }
    }
}

Mp aspx

<html>
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
    <script src="DDScript.js" type="text/javascript"></script>
    <style>
        DIV.ContainerCenter {
            width: 700px;
            margin-left: auto;
            margin-right: auto;
        }

            DIV.ContainerCenter TD {
                width: 150px;
                margin-right: 30px;
                text-align: center;
            }

        DIV.connectedSortable {
            text-align: left;
            border: 1px solid black;
            float: left;
            margin-right: 30px;
            overflow: auto;
            height: 200px;
            width: 300px;
            white-space: nowrap;
        }

            DIV.connectedSortable DIV {
                margin: 0 5px 2px 5px;
                cursor: default;
                white-space: nowrap;
            }

        DIV.selectedItem {
            background-color: Blue;
            color: White;
        }
    </style>

    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

网络表单cs

   protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField hid1 = (HiddenField)FindControl("hidJsonHolder");
        if (hid1 != null)
        {
            Response.Write(hid1.Value);
        }
    }
    protected void btnFinal_Click(object sender, EventArgs e)
    {
        JavaScriptSerializer jsSer = new JavaScriptSerializer();
        object obj = jsSer.DeserializeObject(hidJsonHolder.Value);

        if (obj != null)
        {
            Movie[] listMovie = jsSer.ConvertToType<Movie[]>(obj);
            foreach (Movie p in listMovie)
            {
                string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                Regex re = new Regex(pattern);
                if (p.ToString() != null)
                {
                    MatchCollection matches = re.Matches(p.ToString());
                    if (matches.Count > 0)
                    {
                        for (int i = 0; i < matches.Count; i++)
                        {
                            Response.Write(matches[i] + "; ");
                        }
                    }
                }
            }
        }
    }
    protected string GetJsonData()
    {
        List<Movie> listOfMovies = new List<Movie>();
        string query = "SELECT * FROM Country ORDER BY NAME DESC LIMIT 15;";
        string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        Movie item = new Movie();
                        item.id = Convert.ToInt32(sdr["Capital"]);
                        item.name = sdr["Name"].ToString();
                        listOfMovies.Add(item);
                    }
                }
            }
            con.Close();

            List<Movie> someList = (from item in listOfMovies
                                    select new Movie { id = item.id, name = item.name }).ToList<Movie>();

            JavaScriptSerializer jsSer = new JavaScriptSerializer();
            string str = jsSer.Serialize(someList);
            return str;
        }
    }

    public class Movie
    {
        public string name { get; set; }
        public int id { get; set; }

        public override string ToString()
        {
            return string.Format("[id={0};name={1}]\n", id, name);
        }
    }

网络表单 aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <script type="text/javascript">
        $(function () {
            var $json = <% =GetJsonData() %>;
            pageload($json);
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div class="ContainerCenter">
        <table>
            <tr>
                <td>Source</td>
                <td>Destination</td>
            </tr>
        </table>
        <div id="list1" class="connectedSortable">
        </div>
        <div id="list2" class="connectedSortable">
        </div>
    </div>
    <div style="clear: both;"></div>
    <asp:Button Text="GetLength" runat="server"
        ID="btnFinal"
        OnClick="btnFinal_Click"
        OnClientClick="CreateJson()" />
    <br />
    <asp:HiddenField runat="server" ID="hidJsonHolder" />
</asp:Content>

【问题讨论】:

    标签: json master-pages


    【解决方案1】:

    在浏览器中使用“查看源代码”,您应该会看到客户端 ID 不匹配。

    尝试在您的隐藏字段中添加ClientIDMode="Static"

    有关如何生成客户端 ID 的详细信息,请参阅 netframework-4.8

    另一种选择是通过view=netframework-4.8

    在客户端始终使用由 ASP.NET 生成的实际客户端 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 2013-05-03
      • 2020-01-17
      相关资源
      最近更新 更多