本文主要是介绍母版页与DataList的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
MasterPage.master(母版页)
方法一:
<div style=" float :left;width:127px; height:auto; background-color :Green; text-align:center">
<asp:GridView ID="gv_goodstype" runat="server" AutoGenerateColumns="false"
Height="175px" Width="113px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href='ClassGoods.aspx?id=<%#Eval("TypeID") %>'><%#Eval("TypeName")%></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Eval只用于模板列的绑定中 <a href='ClassGoods.aspx?id=<%#Eval("TypeID") %>'><%#Eval("TypeName")%></a>绑定的TypeID只是传值,真正在页面上显示出来的是<a></a>之间的TypeName
方法二:
<div style="float: left; width: 100px; height: 570px; background-color: gray; text-align:center">
<asp:GridView ID="goodType" runat="server" AutoGenerateColumns="False"
OnRowCommand="linkGood">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="link" CommandArgument='<%#Eval("TypeID") %>'><%#Eval("TypeName") %></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
通过触发GridView中OnRowCommand事件来传值,在页面中显示TypeName
在aspx.cs中
protected void linkGood(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "link")
{
int TypeID = int.Parse(e.CommandArgument.ToString());
Response.Redirect("ClassGoods.aspx?TypeID="+TypeID);
}
}
ClassGoods.aspx
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4"
RepeatDirection="Horizontal">
<ItemTemplate>
<div style="float:left">
<img src='<%#Eval("GoodsPicture")%>' alt="" width="142px" height="108px"/><br />
商品类型:<%#Eval("TypeName") %><br />
商品名称:<%#Eval("GoodsName")%><br />
商品价格:<%#Eval("GoodsPrice")%><br />
</div>
</ItemTemplate>
</asp:DataList>
ClassGoods.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//显示所有商品
showdatalist();
}
}
/// <summary>
/// 根据用户选择的商品类型显示商品
/// </summary>
protected void showdatalist()
{
int TypeID = 0;
if (Request.QueryString["id"] != null)
{
TypeID = int.Parse(Request.QueryString["id"].ToString());
}
string cmdText = "select * from T_GoodsInfo,T_GoodsType where T_GoodsInfo.TypeID=T_GoodsType.TypeID";
if (TypeID != 0)
{
cmdText += " and T_GoodsType.TypeID = " + TypeID;
}
SqlHelper helper = new SqlHelper();
DataTable dt =helper.ExecuteReturnTable(cmdText, null, CommandType.Text);
DataList1.DataSource = dt;
DataList1.DataBind();
}
Request.QueryString["id"] 中的id是<a href='ClassGoods.aspx?id=<%#Eval("TypeID") %>'><%#Eval("TypeName")%></a>中的id,即绑定的TypeID的值
AddGoods.aspx
商品类别:<asp:DropDownList ID="ddl_GoodsType" runat="server">
</asp:DropDownList><br />
商品名称:<asp:TextBox ID="txt_name" runat="server"></asp:TextBox><br />
商品价格:<asp:TextBox ID="txt_price" runat="server"></asp:TextBox><br />
图片路径:<asp:FileUpload ID="goodsImage" runat="server" /><asp:Label ID="Label1" runat="server"
Text="Label" Visible="false"></asp:Label><br/>
<asp:Button ID="bt_Add" runat="server" Text="添加" οnclick="Button1_Click" /><br />
AddGoods.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
string name = txt_name.Text.Trim();
float price = float.Parse(txt_price.Text.Trim());
int val = int.Parse(ddl_GoodsType.SelectedValue);
//上传图片
string str = Server.MapPath("image/");
if (goodsImage.PostedFile.FileName == "")
{
Label1.Text = "要上传的文件名不能为空";
return;
}
else
{
string filename = goodsImage.FileName;
string fileEx = filename.Substring(filename.LastIndexOf(".")+1);
string serverpath = Server.MapPath("image/")+filename;
if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif")
{
goodsImage.SaveAs(serverpath);
Label1.Text = "上传成功";
}
else
{
Label1.Text = "请检查文件类型";
}
string cmdText = "insert into T_GoodsInfo(TypeID,GoodsName,GoodsPrice,GoodsPicture) values(@TypeID,@GoodsName,@GoodsPrice,@GoodsPicture)";
SqlParameter[] para = { new SqlParameter("@TypeID", val),new SqlParameter("@GoodsName",name),
new SqlParameter("@GoodsPrice",price),new SqlParameter("@GoodsPicture","image//"+filename)};
int result=-1;
SqlHelper helper = new SqlHelper();
result = helper.ExecuteReturnInt(cmdText,para,CommandType.Text);
if (result > 0)
{
Response.Write("<script>alert('添加成功');</script>");
}
else
{
Response.Write("<script>alert('添加失败');</script>");
}
}
}
goodsImage:<asp:FileUpload ID="goodsImage" runat="server" />FileUpload控件 把客户机器上的文件上传到服务器的某个文件目录下。image/存放图片的文件夹
这篇关于母版页与DataList的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!