简体   繁体   中英

How to generate HTML and CSS dynamically in asp.net?

I am trying to integrate this photo album in my asp.net website. I am selecting urls of 3 images from database and and displaying those 3 images from a particular album. Now the problem is that what if the user has multiple Albums? It should be able to generate as many Jquery albums on the page as there are in the database associated with that user.

 <div class="image_stack" style="margin-left:300px" runat="server" >
 <img  id="photo1" class="stackphotos"  runat="server" clientidmode="static" >
    <img  id="photo2" class="stackphotos"  runat="server" clientidmode="static">
     <img   id="photo3" class="stackphotos"  runat="server" clientidmode="static" > 
     </div>

 public DataTable dt = new DataTable();
    dboperation dbo = new dboperation();

    protected void Page_Load(object sender, EventArgs e)
    {

        string q = "select photourl ,AlbumName  from PhotoAlbum where UserId=42";
        dt = dbo.Getdt(q);
        string a = dt.Rows[0]["photourl"].ToString();
        string b = dt.Rows[1]["photourl"].ToString();
        string c = dt.Rows[2]["photourl"].ToString();

        photo1.Src = a.Substring(1, a.Length - 1);
        photo2.Src = b.Substring(1, b.Length - 1);
        photo3.Src = c.Substring(1, c.Length - 1);

        q = "  select PhotoAlbum.AlbumId,PhotoAlbum.AlbumName from PhotoAlbum where UserId=42";

         dt = dbo.Getdt(q);
         drpalbum.DataSource = dt;
         drpalbum.DataTextField = "albumname";
         drpalbum.DataValueField = "albumid";
         drpalbum.DataBind();
    }

Let me know if you need to see more code.

use simply a 'div'

in which like this

//HTML (.aspx)
<div id="MyDiv" runat="server" > </div>

//CodeBehind (.aspx.cs)
MyDiv.innerHTML = "Your HTML & CSS or Script code here CODE";

it will be applied to whole div

EDIT 1:

using System.Web.UI.HtmlControls;

import above

What you need to do is to have a HTML element on your page communicating against the server like this,

Pretty much identical to Photons

//HTML - Does not have to be div could be anything like <p>, <div>, <a> etc.
<span id="SomeHTMLElement" runat="server" ></span>

//C#
SomeHTMLElement.innerHTML = "Any text you want to your page (text, HTML, script and/or CSS";

checkout this to generate HTML and CSS dynamically in asp.net

http://www.dotnetperls.com/htmltextwriter

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