简体   繁体   中英

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.

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

Sample data what StoreDetails contain is:

{"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. I know how to pull data in cshtml page using model like below, but I'm now sure in aspx to pull data.

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.

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 .

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

You can use various controls for show json data in user control. For example you can use GridView like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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