简体   繁体   English

如何将动态控件添加到MasterPage

[英]How to add Dynamic Controls to MasterPage

I am getting my List of Images from JSON and adding to page like below: 我从JSON获取我的图像列表并添加到如下页面:

IEnumerable jsonData = default(IEnumerable);

            jsonData = GetJsonValues("http://www.viki.com/api/v2/channels.json");

            foreach (MovieDetails item in jsonData)
            {
                Image thumbNailImage = new Image();
                thumbNailImage.ImageUrl = item.Thumbnail;

                //this.Master.Controls.Add(thumbNailImage);
                this.Controls.Add(thumbNailImage);

            }

But this is adding the images at the buttom of the page as the page template comes from master page. 但是,当页面模板来自母版页时,这是在页面的底部添加图像。

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="VikiWeb._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    </asp:Content>

Now how do I add images in Master page. 现在如何在母版页中添加图像。

You place a PlaceHolder in the position you like to add the controls, and add the controls similar to your code, but using this PlaceHolder id as: 您将PlaceHolder放置在您想要添加控件的位置,并添加类似于您的代码的控件,但使用此PlaceHolder ID为:

<asp:PlaceHolder runat="server" id="placeOnMe" />

and

placeOnMe.Controls.Add(thumbNailImage);

Place an asp:Panel in MasterPage where you need the images to be rendered. 在MasterPage中放置一个asp:Panel ,您需要在其中呈现图像。 And in the code-behind of your content page, do this 在内容页面的代码隐藏中,执行此操作

var panel = Master.FindControl("your_panel_id") as Panel;
if(panel != null)
{
    IEnumerable jsonData = default(IEnumerable);
    jsonData = GetJsonValues("http://www.viki.com/api/v2/channels.json");
    foreach (MovieDetails item in jsonData)
    {
        Image thumbNailImage = new Image();
        thumbNailImage.ImageUrl = item.Thumbnail;
        panel.Controls.Add(thumbNailImage);
    }
}

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

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