aspx页面上的C#代码-为什么不是';t将值赋值给隐藏字段

本文关键字:赋值 字段 隐藏 为什么不 代码 aspx | 更新日期: 2023-09-27 18:01:01

我有一个问题感觉有点琐碎,但我无法解决。

<asp:HiddenField ID="hdnJsonData" runat="server" Value="<%#GetInterviewData%>"/>

我想给hdnJsonData分配一些json数据,我计划将其用于敲除。

using KnockoutApp.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace KnockoutApp.Tutorials
{
    public partial class WorkingWithLists : System.Web.UI.Page
    {
        protected string GetInterviewData
        {
            get
            {
                Product product = new Product();
                product.Name = "Apple";
                product.ExpiryDate = new DateTime(2008, 12, 28);
                product.Price = 3.99M;
                product.Sizes = new string[] { "Small", "Medium", "Large" };
                string output = JsonConvert.SerializeObject(product);
                Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
                return output;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

GetInterviewData上未命中断点。

有人能纠正我的语法吗?

此外,我知道我可以在页面加载中绑定该值。

谢谢!

aspx页面上的C#代码-为什么不是';t将值赋值给隐藏字段

如果您正在使用服务器控件,并且希望从服务器数据中填充它,那么您应该简单地使用后端使用Page_Load来填充它。。。这是正确的方法…现在如果你绝对想在aspx:中做

<%
  string val = GetInterviewData;
%>
<asp:HiddenField ID="hdnJsonData" runat="server" Value="<%=val%>"/>

试试看,让我知道这是否有效。。。请注意,要从aspx中查看GetInterviewData,您可能需要拼写名称空间路径。。。

正如你所说的,你可以在后面的代码中完成,正如你所承认的,我认为这不是你想要的!

您的代码当前为:

<asp:HiddenField ID="hdnJsonData" runat="server" Value="<%#GetInterviewData%>">

我看到的问题是hdnJsonData是一个服务器控件,不能以这种方式为其分配值。

你可以试试

<input type="hidden" id="hdnJsonData" value="<%=(GetInterviewData)%>">

请注意缩写response.write=((,而不是绑定表达式#((


如果你真的必须使用hdnJsonData作为服务器控件,你可以保留你的代码并在代码后面调用Page.DataBind((-这不是100%有效的,因为这不是我通常使用的方法,但你可能会在这方面取得一些成功。