简体   繁体   English

如何将数据从ascx.cs页面绑定到ascx页面?

[英]How to bind data from ascx.cs page to ascx page?

I got some bunch of data from a service and deserialized and kept in variable 'StoreDetails' like below. 我从服务中获取了一些数据,然后反序列化并保存在变量“ StoreDetails”中,如下所示。

var StoreDetails= JsonConvert.DeserializeObject<Info.Wrapper>(json);

Sample data what StoreDetails contain is: StoreDetails包含的样本数据是:

{"Status":{"StatusCode":200,"StatusDisplay":"OK","StatusValue":true,"Version":2},"Store":[{"Carry Out":false,"Coupons":true,"CustomURL":"https:\/\/ordering.com\/5702\/locations\/7725","DeliveryAvailable":false,"Distance":1.05,"Drive Thru":false,"FranchiseStoreId":0000002,"HasPromotion":false,"}]

I want to pull this data from ascx.cs page to ascx page. 我想将此数据从ascx.cs页拉到ascx页。 I know how to pull data in cshtml page using model like below, but I'm now sure in aspx to pull data. 我知道如何使用下面的模型在cshtml页面中提取数据,但是现在确定在aspx中可以提取数据。

Eaxample: 例子

<h4>@Model.Store.ShortAdd.Address1</h4>
<h6>@Model.Store.ShortAdd.Address2</h6>

Can someone help me to pull data and display in a list. 有人可以帮我提取数据并显示在列表中吗?

If I understand your question correctly, you can use Literal control to pass plain data (without additional markup) from Code Behind. 如果我正确理解了您的问题,则可以使用Literal控件从Code Behind传递纯数据(无需附加标记)

ASPX ASPX

<h4><asp:Literal runat="server" ID="Address1Literal" /></h4>

Code Behind 背后的代码

protected void Page_Load(object source, EventArgs e)
{
    Address1Literal.Text = "Some text";
}

You can even pass JSON data directly to script tag . 您甚至可以将JSON数据直接传递到script标签

您可以将数据分配给ascx.cs上的文字或标签或隐藏字段,并在ascx页面中使用它。

You can use various controls for show json data in user control. 您可以使用各种控件在用户控件中显示json数据。 For example you can use GridView like this: 例如,您可以像这样使用GridView

ascx ascx

<asp:GridView ID="gvJsonData" runat="server" AutoGenerateColumns="False" Visible="true">
    <Columns>
        <asp:BoundField DataField="Code" HeaderText="Code" />
        <asp:BoundField DataField="Questions" HeaderText="Questions" />
    </Columns>
</asp:GridView>

ascx.cs ascx.cs

string JsonData = "[{'Code':'S1','Questions':'Q1,Q2'}" + "," + "{'Code':'S2','Questions':'Q3,Q4,Q5'}]";
var Students = JsonConvert.DeserializeObject(JsonData);
gvJsonData.DataSource = Students;
gvJsonData.DataBind();

Output 输出量

gvJsonData

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

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