datalist分类显示产品问题!!!
我现在想在一个网页中分类显示作品,
作品的类别也是可以添加的
每种类别的作品也可以添加
我平时做的都是类别不变的,
所以我写的时候
写个SQL语句
select * from products where typeid = 1
就给它写死了
然后用DATALIST控件
现在类别是可以添加的 我就搞不定了
是不是应该用循环呢,但循环我不会写~~~
哪位仁兄帮帮忙!!
[解决办法]
up
[解决办法]
在sql语句中用select * from products group by typeid
[解决办法]
DataList嵌套DataList,把分类的详细信息绑定到外层DAtaList中,把具体分类下的产品信息绑定到内层DataList中(在外层DataList的ItemDataBound事件中绑定),楼主试一下:
<asp:DataList ID= "DataList1 " runat= "server " OnItemDataBound= "ItemDataBound ">
<ItemTemplate>
<asp:HiddenField ID= "hf " runat= "server " Value= ' <%# Eval( "type ") %> ' />
<div> <%# Eval( "name ") %> </div>
<div>
<asp:DataList ID= "dl1 " runat= "server ">
<ItemTemplate>
<%# Eval( "Name ") %>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
</asp:DataList>
[解决办法]
能用XML文件吗?给你代码你自己看看 用XML文件 增 删 改的 我觉得比数据库方便,贴过去直接用
--------------------------------------------------
xmlDemo.xml:
<?xml version= "1.0 " standalone= "yes "?>
<Advertisements>
<Ad>
<ImageUrl> ~/img/1.gif </ImageUrl>
<NavigateUrl> http://www.sina.com </NavigateUrl>
<AlternateText> 新浪1 </AlternateText>
<Keyword> 体育2 </Keyword>
<Impressions> 40 </Impressions>
</Ad>
<Ad>
<ImageUrl> ~/img/2.gif </ImageUrl>
<NavigateUrl> http://www.baidu.com </NavigateUrl>
<AlternateText> 猎报2 </AlternateText>
<Keyword> 新闻2 </Keyword>
<Impressions> 50 </Impressions>
</Ad>
<Ad>
<ImageUrl> ~/img/3.gif </ImageUrl>
<NavigateUrl> http://www.sohu.com </NavigateUrl>
<AlternateText> 搜狐 </AlternateText>
<Keyword> 学习园地 </Keyword>
<Impressions> 100 </Impressions>
</Ad>
<Ad>
<ImageUrl> 123 </ImageUrl>
<NavigateUrl> http://123 </NavigateUrl>
<AlternateText> 123 </AlternateText>
<Keyword> 123 </Keyword>
<Impressions> 123 </Impressions>
</Ad>
</Advertisements>
-------------------------------
[解决办法]
HTML:
<form id= "form1 " runat= "server ">
<div>
<asp:DataList ID= "DataList1 " runat= "server " CellPadding= "4 " ForeColor= "#333333 " Style= "position: relative " OnEditCommand= "DataList1_EditCommand " OnCancelCommand= "DataList1_CancelCommand " OnUpdateCommand= "DataList1_UpdateCommand " OnDeleteCommand= "DataList1_DeleteCommand " RepeatColumns= "2 ">
<FooterStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<SelectedItemStyle BackColor= "#D1DDF1 " Font-Bold= "True " ForeColor= "#333333 " />
<ItemTemplate>
<table style= "width: 452px; position: relative " border= "1 ">
<tr>
<td style= "width: 58px ">
<asp:Label ID= "Label1 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "广告名称 "> </asp:Label> </td>
<td>
<asp:Label ID= "lblname " runat= "server " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "AlternateText ") %> ' Font-Size= "Small "> </asp:Label> </td>
</tr>
<tr>
<td style= "width: 58px ">
<asp:Label ID= "Label2 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "图片地址 "> </asp:Label> </td>
<td>
<asp:Label ID= "lblimgurl " runat= "server " Font-Bold= "False " Font-Size= "Small " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "ImageUrl ") %> '> </asp:Label> </td>
</tr>
<tr>
<td style= "width: 58px ">
<asp:Label ID= "Label3 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "关键词 "> </asp:Label> </td>
<td>
<asp:Label ID= "lblwords " runat= "server " Font-Bold= "False " Font-Size= "Small " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "Keyword ") %> '> </asp:Label> </td>
</tr>
<tr>
<td style= "width: 58px; height: 18px ">
<asp:Label ID= "Label4 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "显示重权 "> </asp:Label> </td>
<td style= "height: 18px ">
<asp:Label ID= "lbldisp " runat= "server " Font-Bold= "False " Font-Size= "Small " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "Impressions ") %> '> </asp:Label> </td>
</tr>
<tr>
<td style= "width: 58px ">
<asp:Label ID= "Label5 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "广告指向 "> </asp:Label> </td>
<td>
<asp:Label ID= "lblzhix " runat= "server " Font-Bold= "False " Font-Size= "Small " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "NavigateUrl ") %> '> </asp:Label> </td>
</tr>
<tr>
<td align= "center " colspan= "2 ">
<table style= "width: 100px; position: relative ">
<tr>
<td>
<asp:Button ID= "editbtn " runat= "server " Style= "position: relative " Text= "修改广告 " CommandName= "Edit " /> </td>
<td>
<asp:Button ID= "delbtn " runat= "server " Style= "position: relative " Text= "删除广告 " CommandName= "Delete " /> </td>
<td>
<asp:Button ID= "addbtn " runat= "server " Style= "position: relative " Text= "增加广告 " OnClick= "addbtn_Click " /> </td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
[解决办法]
<EditItemTemplate>
<table style= "width: 468px; position: relative ">
<tr>
<td>
<asp:Label ID= "Label1 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "广告名称 "> </asp:Label> </td>
<td>
<asp:TextBox ID= "nametxt " runat= "server " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "AlternateText ") %> '> </asp:TextBox> </td>
</tr>
<tr>
<td>
<asp:Label ID= "Label2 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "图片地址 "> </asp:Label> </td>
<td>
<asp:TextBox ID= "imgurltxt " runat= "server " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "ImageUrl ") %> '> </asp:TextBox> </td>
</tr>
<tr>
<td>
<asp:Label ID= "Label3 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "关键词 "> </asp:Label> </td>
<td>
<asp:TextBox ID= "wordstxt " runat= "server " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "Keyword ") %> '> </asp:TextBox> </td>
</tr>
<tr>
<td>
<asp:Label ID= "Label4 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "显示重权 "> </asp:Label> </td>
<td>
<asp:TextBox ID= "disptxt " runat= "server " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "Impressions ") %> '> </asp:TextBox> </td>
</tr>
<tr>
<td style= "height: 21px ">
<asp:Label ID= "Label5 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "广告指向 "> </asp:Label> </td>
<td style= "height: 21px ">
<asp:TextBox ID= "zhixtxt " runat= "server " Style= "position: relative " Text= ' <%# DataBinder.Eval(Container.DataItem, "NavigateUrl ") %> '> </asp:TextBox> </td>
</tr>
<tr>
<td align= "center " colspan= "2 " style= "height: 21px ">
<asp:Button ID= "updatebtn " runat= "server " Style= "position: relative " Text= "更新 " CommandName= "Update " />
<asp:Button ID= "cancelbtn " runat= "server " Style= "position: relative " Text= "取消 " CommandName= "Cancel " /> </td>
</tr>
</table>
</EditItemTemplate>
<AlternatingItemStyle BackColor= "White " />
<ItemStyle BackColor= "#EFF3FB " />
<HeaderStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
</asp:DataList> </div>
<asp:Panel ID= "addpanel " runat= "server " Height= "197px " Style= "left: 0px; position: relative;
top: 0px " Width= "450px ">
<table style= "width: 461px; position: relative; left: 0px; top: 0px; " border= "1 ">
<tr>
<td>
<asp:Label ID= "Label1 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "广告名称 "> </asp:Label> </td>
<td>
<asp:TextBox ID= "nametxt " runat= "server " Style= "position: relative "> </asp:TextBox> </td>
</tr>
<tr>
<td>
<asp:Label ID= "Label2 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "图片地址 "> </asp:Label> </td>
<td>
<asp:TextBox ID= "imgurltxt " runat= "server " Style= "position: relative "> </asp:TextBox> </td>
</tr>
<tr>
<td>
<asp:Label ID= "Label3 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "关键词 "> </asp:Label> </td>
<td>
<asp:TextBox ID= "wordstxt " runat= "server " Style= "position: relative "> </asp:TextBox> </td>
</tr>
<tr>
<td style= "height: 26px ">
<asp:Label ID= "Label4 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "显示重权 "> </asp:Label> </td>
<td style= "height: 26px ">
<asp:TextBox ID= "disptxt " runat= "server " Style= "position: relative "> </asp:TextBox> </td>
</tr>
<tr>
<td style= "height: 21px ">
<asp:Label ID= "Label5 " runat= "server " Font-Bold= "True " Font-Size= "Small " Style= "position: relative "
Text= "广告指向 "> </asp:Label> </td>
<td style= "height: 21px ">
<asp:TextBox ID= "zhixtxt " runat= "server " Style= "position: relative "> http:// </asp:TextBox> </td>
</tr>
<tr>
<td align= "center " colspan= "2 " style= "height: 21px; background-color: #ccffff ">
<asp:Button ID= "addcmdbtn " runat= "server " Style= "position: relative " Text= "增加 " OnClick= "addcmdbtn_Click " />
<asp:Button ID= "backbtn " runat= "server " OnClick= "backbtn_Click " Style= "position: relative "
Text= "取消 " /> </td>
</tr>
</table>
</asp:Panel>
</form>
---------------------------------------------
[解决办法]
up
[解决办法]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
public partial class DataSetXml_DataList_Edit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.addpanel.Visible = false;
this.Bind();
}
}
#region//填充DataList方法
public void Bind()
{
this.addpanel.Visible = false;
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath( "xmlDemo.xml "));
this.DataList1.DataSource = ds;
this.DataList1.DataBind();
Session[ "ds "] = ds;
}
#endregion
#region//编辑事件
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
this.Bind();
}
#endregion
#region//点击DataList添加广告按钮,显示广告添加项
protected void addbtn_Click(object sender, EventArgs e)
{
this.Clear();
this.addpanel.Visible = true;
this.nametxt.Focus();
}
#endregion
#region//隐藏添加项
protected void backbtn_Click(object sender, EventArgs e)
{
this.addpanel.Visible = false;
}
#endregion
#region//取消事件
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
this.Bind();
}
#endregion
#region//更新信息事件
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
DataSet ds = (DataSet)Session[ "ds "];
ds.Tables[0].Rows[e.Item.ItemIndex][ "ImageUrl "] = ((TextBox)e.Item.FindControl( "imgurltxt ")).Text;
ds.Tables[0].Rows[e.Item.ItemIndex][ "NavigateUrl "] = ((TextBox)e.Item.FindControl( "zhixtxt ")).Text;
ds.Tables[0].Rows[e.Item.ItemIndex][ "AlternateText "] = ((TextBox)e.Item.FindControl( "nametxt ")).Text;
ds.Tables[0].Rows[e.Item.ItemIndex][ "Keyword "] = ((TextBox)e.Item.FindControl( "wordstxt ")).Text;
ds.Tables[0].Rows[e.Item.ItemIndex][ "Impressions "] = ((TextBox)e.Item.FindControl( "disptxt ")).Text;
ds.WriteXml(Server.MapPath( "xmlDemo.xml "));
Session[ "ds "] = ds;
this.DataList1.EditItemIndex = -1;
this.Bind();
}
#endregion
#region//删除信息事件
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
DataSet ds = (DataSet)Session[ "ds "];
ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[e.Item.ItemIndex]);
Session[ "ds "] = ds;
ds.WriteXml(Server.MapPath( "xmlDemo.xml "));
this.Bind();
}
#endregion
#region//添加xml文件中项
protected void addcmdbtn_Click(object sender, EventArgs e)
{
DataSet ds = (DataSet)Session[ "ds "];
DataRow dr = ds.Tables[0].NewRow();
dr[ "ImageUrl "] = this.imgurltxt.Text;
dr[ "NavigateUrl "] = this.zhixtxt.Text;
dr[ "AlternateText "] = this.nametxt.Text;
dr[ "Keyword "] = this.wordstxt.Text;
dr[ "Impressions "] = this.disptxt.Text;
ds.Tables[0].Rows.Add(dr);
Session[ "ds "] = ds;
ds.WriteXml(Server.MapPath( "xmlDemo.xml "));
this.Bind();
this.addpanel.Visible = false;
}
#endregion
#region//清楚文本框中文字方法
public void Clear()
{
this.imgurltxt.Text = " ";
this.zhixtxt.Text = "http:// ";
this.nametxt.Text = " ";
this.wordstxt.Text = " ";
this.disptxt.Text = " ";
}
#endregion
}
[解决办法]
up
[解决办法]
up