分配唯一的ID在foreach循环按钮 [英] Assign Unique id's to buttons in foreach loop

查看:263
本文介绍了分配唯一的ID在foreach循环按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立的网上购物网站,在这个时候有3个产品在里面。这里的产品样品。

首先,它是一个形象,产品名称,描述,价格,标识和加入购物车按钮,这是建立编程,它也是动态的。
可以看到,每一个产品都有自己的按钮,也通过foreach循环创建。
我混淆我怎么能得到这些唯一ID的,我分配给它?意味着当我点击加入购物车按钮,它给我的id = 3 foreach循环。
现在,我要帮助,如何加入购物车按钮,确定单击的是哪个产品,然后显示产品的所有规格,然后根据它进行操作。
这里是code的样品:
我使用3层架构为它和这里的code的样本:

 的DataSet DS = obj.searching_product();
DataTable的DT =新的DataTable();
DT = ds.Tables [Register_Product];
的foreach(在dt.Rows的DataRow博士)
{
    文字LI2 =新的文字();
    li2.Text =< BR />中;
    this.Panel1.Controls.Add(LI2);
    标签LB1 =新标签();
    lb1.Text =博士[名称]的ToString()。
    this.Panel1.Controls.Add(LB1);    //添加这里的价格,图像和标识等的文字    文字合谷=新的文字();
    li4.Text =< BR />中;
    this.Panel1.Controls.Add(LI4);
    按钮BTN =新按钮();
    btn.Height = 19;
    btn.Width = 100;
    btn.Text =加入购物车;
    btn.Click + =新的EventHandler(Button_Click);
    this.Panel1.Controls.Add(BTN);
}


解决方案

有办法很少做到这一点,您可以创建一个扩展按钮类,使用AA财产持有身份证或简单破解的可能是使用项目编号在巴顿的ID,如:

 按钮BTN =新按钮();
btn.ID =btnItem-+项目编号;

然后提取项目编号在这样的事件处理程序:

 无效Button_Click(对象发件人,EventArgs的发送)
{
    按钮senderButton =发件人的按钮;
    INT ITEMID = -1;
    如果(senderButton =空&放大器;&放大器;!senderButton.ID.Contains(' - '))
    {
        ITEMID = int.Parse(senderButton.ID.Split( - )[1]); //或int.TryParse(更好)
        //与ITEMID工作
    }
}

作为一个方面说明,一个更好的选择是创建自己的按钮类的(扩大现有Button类)的并添加条目标识的新属性,更妙的是选项是使用控制像 GridView控件使用或转发控件数据绑定。

I am building online shopping website and at this time there are 3 products in it. Here's the sample of products.

First it is a image, product name, description, price, id and "Add to Cart" Button which is build programmatically and It is also dynamically. You can see that every product have its own Button which is also created through foreach loop. I am confuse that how can I get those unique id's which I assigned to it? Means when I click on Add to Cart button, it give me id=3 in foreach loop. Now, I want a help that how "Add to Cart" button identify that which product is clicked and then show all the specification of product and then Proceed according to it. Here's the Sample of Code: I am using 3-tier architecture for it and here's the sample of code:

DataSet ds=obj.searching_product();
DataTable dt = new DataTable();
dt = ds.Tables["Register_Product"];
foreach(DataRow dr in dt.Rows)
{
    Literal li2 = new Literal();
    li2.Text = "<br/>";
    this.Panel1.Controls.Add(li2);
    Label lb1 = new Label();
    lb1.Text = dr["Name"].ToString();
    this.Panel1.Controls.Add(lb1);

    //Adding Here's Literal for price, Images and ID etc.

    Literal li4 = new Literal();
    li4.Text = "<br/>";
    this.Panel1.Controls.Add(li4);
    Button btn = new Button();
    btn.Height = 19;
    btn.Width = 100;
    btn.Text = "Add to Cart";
    btn.Click += new EventHandler(Button_Click);
    this.Panel1.Controls.Add(btn);
}

解决方案

There are few ways to do it, You can create an extension to Button class, with a a property to hold ID or a simpler hack could be to use the ItemID in Button's ID like:

Button btn = new Button();
btn.ID = "btnItem-" + ItemID;

Then extract the ItemID in event handler like:

void Button_Click(object sender, EventArgs e)
{
    Button senderButton = sender as Button;
    int itemID = -1;
    if (senderButton != null && senderButton.ID.Contains('-'))
    {
        itemID = int.Parse(senderButton.ID.Split('-')[1]); //or int.TryParse (better)
        //work with itemID
    }
}

As a side note, a better option would be to create your own button class (extending existing Button class) and add a new property for ItemID, Even better option would be to use controls like GridView or Repeater controls using Data Binding.

这篇关于分配唯一的ID在foreach循环按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆