Add and bind datalist dynamically in the. net background

Source: Internet
Author: User

[Csharp]
Public void GetDataListData (string standardclass, string toothtypeBig, int I)
{
DataTable dt = bindData (standardclass, toothtypeBig );
DataList d = new DataList ();
D. ID = "dlstTest ";
D. ItemTemplate = new mycolum( "litData", 200 );
D. RepeatColumns = 5;
D. RepeatDirection = RepeatDirection. Horizontal;
D. ItemDataBound + = new DataListItemEventHandler (d_ItemDataBound );
D. DataSource = dt;
D. DataBind ();
 
HtmlGenericControl div = new HtmlGenericControl ("div ");
Div. Attributes. Add ("class", "ProductIntroList ");
If (I = 0)
Div. Style. Add ("display", "block ");
Else
Div. Style. Add ("display", "none ");
Div. Controls. Add (d );
This. divList. Controls. Add (div );
}
Private void d_ItemDataBound (object sender, System. Web. UI. WebControls. DataListItemEventArgs e)
{
Literal t;
If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
{
T = (Literal) e. Item. FindControl ("litData ");
If (t! = Null)
{
String ulstring = string. Format (@ "<ul> <li> <a class = 'imageblock' href = 'productinfor. aspx? {0} '>
</a>
<A class = 'textblock' href = 'productinfor. aspx? {0} '> {2}-{3} </a> </li>
</Ul> ", (DataRowView) e. item. dataItem ). row ["Url"]. toString (), (DataRowView) e. item. dataItem ). row ["ImageUrl"]. toString (), (DataRowView) e. item. dataItem ). row ["Name"]. toString (), (DataRowView) e. item. dataItem ). row ["CDescription"]. toString ());
T. Text = ulstring;
}
}
}
Public DataTable bindData (string _ standardclass, string _ toothtypebig)
{
This. divShopNull. Visible = false;
Try
{
String SQL = @ "select DISTINCT Name, CDescription, StandardClass, Groove, HeadType, ToothTypeBig from ba_ShellTrumpet where 1 = 1 ";
If (_ standardclass! = "")
{
SQL + = "and StandardClass = '" + _ standardclass + "'";
}
If (_ toothtypebig! = "")
SQL + = "and ToothTypeBig = '" + _ toothtypebig + "'";
SQL + = "order by Name ";
DataTable standard = BLL. GetDataTable (SQL );
If (! CommonClass. DTRow. CheckDtIsEmpty (standard ))
{
Standard. Columns. Add ("Url", typeof (string ));
Standard. Columns. Add ("ImageUrl", typeof (string ));
For (int I = 0; I <standard. Rows. Count; I ++)
{
// StandardClass = {0} & Name = {1} & CDescription = {2}
Standard. rows [I] ["Url"] = "StandardClass =" + standard. rows [I] ["StandardClass"]. toString () + "& Name =" + standard. rows [I] ["Name"]. toString () + "& CDescription =" + standard. rows [I] ["CDescription"]. toString () + "& Groove =" + standard. rows [I] ["Groove"]. toString () + "& HeadType =" + standard. rows [I] ["HeadType"]. toString () + "& ToothTypeBig =" + standard. rows [I] ["ToothTypeBig"]. toString ();
Standard. Rows [I] ["ImageUrl"] = ImageUrl (standard. Rows [I] ["Name"]. ToString ());
}
Return standard;
}
Else
{
This. divShopNull. Visible = true;
Return null;
}
 
}
Catch (Exception ex)
{
 
Throw;
Return null;
}
 
}
Public class MyColumn: ITemplate
{
Private string pControlName; // Control name
Private int pWidth; // control width
Public MyColumn (string ControlName, int Width)
{
PControlName = ControlName;
PWidth = Width;
}
Public void InstantiateIn (Control container)
{
Literal h = new Literal ();
H. ID = pControlName;
Container. Controls. Add (h );
}
}
Public string ImageUrl (string name)
{
If (name = "")
Return "no_pic.jpg ";
String imagename = "imgProduct \" + name + ". jpg ";
String path = System. Web. HttpContext. Current. Server. MapPath (imagename); // WebHelper. GetSiteUrl () + "\" + imagename;
If (System. IO. File. Exists (path ))
{
Return name + ". jpg ";
}
Return "no_pic.jpg ";
}

Related Article

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.