Method 1:
Front-end code:
Code
<div>
<%for (int i = 0; i < dtTitle.Rows.Count; i++)
{ %>
<div style="float:left;">
<a href="Default2.aspx?id=<%Response.Write(dtTitle.Rows[i]["ID"].ToString()); %>">
<%Response.Write(dtTitle.Rows[i]["title"].ToString()); %>
</a></div>
<%} %>
</div>
Background code:
Code
public partial class _Default : System.Web.UI.Page
{
public DataTable dtTitle = null;
protected void Page_Load(object sender, EventArgs e)
{
getData();
}
protected void getData()
{
string sql = "select top 5 * from viewTitle where isview=1";
DataSet dsTitle = DbHelperOleDb.Query(sql);
dtTitle = dsTitle.Tables[0];
}
}
Note: Public datatable dttitle = NULL;
Datatable is global. Otherwise, the front-end cannot read the able.
Method 2 (concatenated string ):
Foreground code: bind the background variable with <% = STR %>;
<Li>
<A href = "#"> News </a>
<Ul style = "width: 183px; display: inline; margin-left: 25px; font-size: 14px; font-weight: bold;">
<% = STR %>
</Ul>
</LI>
Background code:
Code
Public stringbuilder STR = new stringbuilder (); // note that public is required here.
Protected void page_load (Object sender, eventargs E)
{
String sqlstring = "select * From productclass where productid = 0 ";
String connectionstring = configurationmanager. connectionstrings ["connectionstring"]. tostring ();
Dataset DS = new dataset ();
Using (oledbconnection connection = new oledbconnection (connectionstring ))
{
Try
{
Connection. open ();
Oledbcommand command = new oledbcommand (sqlstring, connection );
Oledbdataadapter da = new oledbdataadapter (command );
Da. Fill (DS, "ds ");
}
Catch (system. Data. oledb. oledbexception ex)
{
Throw new exception (ex. Message );
}
}
Datatable dt = Ds. Tables [0];
Int COUNT = DT. Rows. count;
For (INT I = 0; I <count; I ++)
{
Str. append ("<li style = \" width: 113px; display: inline; margin-top: 15px; \ "> ");
Str. append ("<a Title =" + (Dt. Rows [I] ["productname"]). tostring () + "href = \" newslist. aspx? Id = "+ convert. toint32 (Dt. Rows [I] [" ID "]) + "\"");
Str. append ("style = \" color: # fdfc89 \"");
Str. append ("> ");
Str. append ("" + dt. Rows [I] ["productname"]. tostring () + "");
Str. append ("</a> ");
Str. append ("</LI> ");
}
// Response. Write (STR );
}