ASP.NET检查单击了哪个按钮

时间:2016-07-01 09:57:14

标签: c# asp.net

这是交易,我创建了一个销售某些产品的网站,根据我有多少产品,按钮数量也会发生变化。

例如,如果我有5个产品,我将有5个按钮,如下所示: Example

我想要做的是当点击一个按钮时我想获取项目名称并将其保存(在会话中),这样我就可以在不同的页面上使用这些信息(背后的代码在C#中)。

我该怎么做?

HTML:

<div runat="server" id="CatalogProducts" class="container-fluid">


</div>

代码背后:

        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>הוסף<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>הוסף<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 />";
            }

            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>הוסף לסל</h2> <p class=\"icon-links\">" + DSViewProducts.Tables[0].Rows[I][0].ToString() + "<br /> " + DSViewProducts.Tables[0].Rows[I][4].ToString() + " </p> </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>הוסף<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 />";
                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>הוסף<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 />";
            }
        }

the methood chkData:

public DataSet chkData(string sqlstr)
{



        string path = HttpContext.Current.Server.MapPath("~/App_Data/");
        string fileName = "Hey.mdb";
        path += fileName;
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + path;
        OleDbConnection conn = new OleDbConnection(connString);


        OleDbDataAdapter da = new OleDbDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
}

2 个答案:

答案 0 :(得分:0)

最好将Repeater用于此解决方案。查看this link。它会告诉你一步一步做什么。

答案 1 :(得分:0)

在CatalogProducts.InnerHtml中将for循环计数绑定到按钮ID,如果不能绑定那么尝试代替asp按钮你可以使用input type =“button”

我刚刚在你的代码中添加了这个..

<input type="Button" id=\"Button_\"+I />