Obtain the background able at the front end

Source: Internet
Author: User

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 );
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.