这是这笔交易,我制作了一个销售某些产品的网站,根据产品的数量,我的按钮数量也有所变化。
例如,如果我有5个产品,我有5个按钮,如下所示:
所以我写了一个代码如下:
SqlHey SQLViewProducts = new SqlHey();
DataSet DSViewProducts = new DataSet();
string Sheilta = "SELECT TblShop.ItemName, TblShop.Price, TblShop.ShopType, TblShop.Image, TblShop.Description FROM TblShop;";
DSViewProducts = SQLViewProducts.chkData(Sheilta);
int I;
for (I = 0; I < DSViewProducts.Tables[0].Rows.Count; I++)
{
if (DSViewProducts.Tables[0].Rows.Count % 2 == 0)
{
if (I % 2 == 0)
CatalogProducts.InnerHtml += " <div class=\"row\"> <div style=\"position: relative\" class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12 col-sm-push-1 animated bounceInLeft\"> <div class=\"hovereffect\"> <img class=\"img-responsive\" src= \".." + DSViewProducts.Tables[0].Rows[I][3].ToString() + "\"" + " alt=\"\"> <div class=\"overlay\"> <h2>Add to cart<span style=\"font-size: 0.65em\"></span> <asp:Button ID=\"Button1\" runat=\"server\" BackColor=\"Transparent\" BorderStyle=\"None\" ForeColor=\"Transparent\" Height=\"43px\" Style=Height=\"z-index: 99; left: 0px; position: absolute; top: 0px\" Text=\"a\" Width=\"87px\" /></h2> <p class=\"icon-links\">" + DSViewProducts.Tables[0].Rows[I][0].ToString() + "<br /> " + DSViewProducts.Tables[0].Rows[I][4].ToString() + " </p> </div> </div> </div>";
else CatalogProducts.InnerHtml += " <div style=\"position: relative\" class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12 col-sm-push-1 animated bounceInLeft\"> <div class=\"hovereffect\"> <img class=\"img-responsive\" src= \".." + DSViewProducts.Tables[0].Rows[I][3].ToString() + "\"" + " alt=\"\"> <div class=\"overlay\"> <h2>Add to cart<span style=\"font-size: 0.65em\"></span> <asp:Button ID=\"Button1\" runat=\"server\" BackColor=\"Transparent\" BorderStyle=\"None\" ForeColor=\"Transparent\" Height=\"43px\" Style=\"z-index: 99; left: 0px; position: absolute; top: 0px\" Text=\"a\" Width=\"87px\" /></h2> <p class=\"icon-links\">" + DSViewProducts.Tables[0].Rows[I][0].ToString() + "<br /> " + DSViewProducts.Tables[0].Rows[I][4].ToString() + " </p> </div> </div> </div> </div> <br /> <br />";
}
if (DSViewProducts.Tables[0].Rows.Count % 2 != 0)
{
if (I + 1 == DSViewProducts.Tables[0].Rows.Count)
CatalogProducts.InnerHtml += " <div class=\"row\"> <div style=\"position: relative\" class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12 col-sm-push-1 animated bounceInLeft\"> <div class=\"hovereffect\"> <img class=\"img-responsive\" src= \".." + DSViewProducts.Tables[0].Rows[I][3].ToString() + "\"" + " alt=\"\"> <div class=\"overlay\"> <h2>Add to cart<span style=\"font-size: 0.65em\"></span> <asp:Button ID=\"Button1\" runat=\"server\" BackColor=\"Transparent\" BorderStyle=\"None\" ForeColor=\"Transparent\" Height=\"43px\" Style=Height=\"z-index: 99; left: 0px; position: absolute; top: 0px\" Text=\"a\" Width=\"87px\" /></h2> <p class=\"icon-links\">" + DSViewProducts.Tables[0].Rows[I][0].ToString() + "<br /> " + DSViewProducts.Tables[0].Rows[I][4].ToString() + " </p> </div> </div> </div> </div> ";
if (I % 2 == 0)
CatalogProducts.InnerHtml += " <div class=\"row\"> <div style=\"position: relative\" class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12 col-sm-push-1 animated bounceInLeft\"> <div class=\"hovereffect\"> <img class=\"img-responsive\" src= \".." + DSViewProducts.Tables[0].Rows[I][3].ToString() + "\"" + " alt=\"\"> <div class=\"overlay\"> <h2>Add to cart<span style=\"font-size: 0.65em\"></span> <asp:Button ID=\"Button1\" runat=\"server\" BackColor=\"Transparent\" BorderStyle=\"None\" ForeColor=\"Transparent\" Height=\"43px\" Style=Height=\"z-index: 99; left: 0px; position: absolute; top: 0px\" Text=\"a\" Width=\"87px\" /></h2> <p class=\"icon-links\">" + DSViewProducts.Tables[0].Rows[I][0].ToString() + "<br /> " + DSViewProducts.Tables[0].Rows[I][4].ToString() + " </p> </div> </div> </div> </div> <br /> <br /> <br /> <br />";
else CatalogProducts.InnerHtml += " <div style=\"position: relative\" class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12 col-sm-push-1 animated bounceInLeft\"> <div class=\"hovereffect\"> <img class=\"img-responsive\" src= \".." + DSViewProducts.Tables[0].Rows[I][3].ToString() + "\"" + " alt=\"\"> <div class=\"overlay\"> <h2>Add to cart<span style=\"font-size: 0.65em\"></span> <asp:Button ID=\"Button1\" runat=\"server\" BackColor=\"Transparent\" BorderStyle=\"None\" ForeColor=\"Transparent\" Height=\"43px\" Style=Height=\"z-index: 99; left: 0px; position: absolute; top: 0px\" Text=\"a\" Width=\"87px\" /></h2> <p class=\"icon-links\">" + DSViewProducts.Tables[0].Rows[I][0].ToString() + "<br /> " + DSViewProducts.Tables[0].Rows[I][4].ToString() + " </p> </div> </div> </div> </div> <br /> <br />";
}
}
protected void Allbuttons_Click(object sender, EventArgs e)
{
string BTNID = ((Button)sender).ID.ToString();
Session["ItemName"] = BTNID;
Response.Redirect("ViewItem.aspx");
}
但点击它时按钮不起作用。我在HTML上写了相同的代码(div one),看看也许,我做错了,按钮确实有效!
这就是我在HTML上编写它时的样子(这是有用的):
<div class="row">
<div style="position: relative" class="col-lg-3 col-md-4 col-sm-6 col-xs-12 col-sm-push-1 animated bounceInLeft">
<div class="hovereffect">
<img class="img-responsive" src="../User/Pictures/ShirtCheck.png" alt="">
<div class="overlay">
<h2>Add to cart<span style="font-size: 0.65em"></span>
<asp:Button ID="Button1" runat="server" BackColor="Transparent" BorderStyle="None"
ForeColor="Transparent" Height="43px" Style="z-index: 99; left: 0px; position: absolute;
top: 0px" Text="a" Width="87px" />
<p class="icon-links"></p>
</div>
</div>
</div>
为什么当我手动编写按钮时,按钮确实有效,当我按照代码执行操作时,他们不会这样做?
答案 0 :(得分:0)
这是一个非常基本的示例,但它可以应用于您在应用程序中执行的操作。好的,假设您有这样的数据源:
protected List<string> ListOfStrings { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (ListOfStrings == null)
ListOfStrings = new List<string>();
ListOfStrings.Insert(0, "First item");
ListOfStrings.Insert(1, "Second item");
ListOfStrings.Insert(2, "Third item");
ListOfStrings.Insert(3, "Fourth item");
}
这个数据源是你的产品...我看到你正在为代码背后的ASPX页面生成HTML,这通常被认为是一种不好的做法,因为你希望你的HTML和C#代码完全分开实现(这是这个答案的目的)。分离HTML和C#的方法是在ASPX页面中使用绑定表达式,如下所示:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<% for (int i = 0; i < ListOfStrings.Count(); i++)
{
%>
<p><%=ListOfStrings[i] %></p>
<% } %>
</asp:Content>
通过这种方式,您可以将HTML与C#代码本身完全分开,如果您需要在HTML本身进行更改,而不是修改C#代码(我认为),您以后更容易处理HTML是一个痛苦的屁股)...所以总是尝试将您的代码与HTML分开,这是一个很好的做法。希望这可以帮助你.. :)