【问题标题】:ASP.NET check which button was clickedASP.NET 检查单击了哪个按钮
【发布时间】:2016-07-01 09:57:13
【问题描述】:

这是交易,我制作了一个销售一些产品的网站,并且根据我拥有的产品数量,按钮的数量也会发生变化。

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

我想要做的是,当单击按钮时,我想要获取项目名称并将其保存(在会话中),以便我可以在不同的页面上使用此信息(后面的代码是 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 />";
            }
        }

方法 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;
}

【问题讨论】:

  • 你在用中继器吗..?
  • 如何创建按钮?给我们看代码
  • @Arvin 我添加了代码。
  • @AkashAmin 我添加了代码。
  • 所有按钮都有相同的id和相同的文字,如何区分按钮

标签: c# asp.net


【解决方案1】:

在 CatalogProducts.InnerHtml 中将 for 循环计数绑定到按钮 ID,如果无法绑定,请尝试使用 input type="button" 代替 asp 按钮

我刚刚添加到你的代码参考这个..

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

【讨论】:

    【解决方案2】:

    这个解决方案最好使用Repeater。查看this link。它会一步一步告诉你该怎么做。

    【讨论】:

    • 我怎样才能让Repeater像我一样制作我的div?
    • @harel486 如果你的意思是对偶数和奇数项目做不同的事情,你可以在中继器中使用 和 标签
    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多