有一个asp按钮控制字面 [英] Literal Control with an asp button

查看:106
本文介绍了有一个asp按钮控制字面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个电子商务网站,ASP .NET和我使用动态产品添加到购物页面从数据库中文字控制像这样:

 的foreach(在dt.Rows的DataRow博士)
{
    字符串产品名称=博士[PRO_Name]的ToString()。
    字符串productSku,则=博士[PRO_Price]的ToString()。
    字符串myUrl =?Handler.ashx的ProdID =+博士[PRO_ID]的ToString()。        字符串sProduct = @< D​​IV CLASS ='四店列同位素项目'>< D​​IV CLASS ='店项目'><图>< IMG SRC ='+ myUrl +'/ >< figcaption类='项目,说明'><&H5 GT; +产品名称+< / H5><跨度> R+ productSku,则+< / SPAN>< / figcaption>< /图>< / DIV>< / DIV>中;
        productPanel.Controls.Add(新LiteralControl(sProduct));
}

我想知道我怎么可以用一个click事件是在我的文字控件添加一个按钮。我创建了这个按钮,但不能将它添加到文本控件作为文字仅控制显示的标记,而不是服务器端控件。我该如何去从数据库添加到购物页面的每个产品动态添加该按钮。

 按钮按钮=新按钮();
button.Text =添加到购物车;
button.Click + = button_Click;


解决方案

试试这个办法。

 表TB =新表();的for(int i = 1; I< = 10;我++)
        {
            TR的TableRow =新的TableRow();
            TableCell的TD =新的TableCell();
            按钮BT =新按钮();
            bt.ID = i.ToString();
            bt.Text =按钮+ i.ToString();
            bt.Click + =新的EventHandler(btn_Click);
            td.Controls.Add(BT);
            tr.Controls.Add(TD);
            tb.Controls.Add(TR);
        }dvTest.Controls.Add(TB);

I'm creating an e-commerce website with ASP .NET and I dynamically add products to the shopping page from the database using a literal control like so:

foreach (DataRow dr in dt.Rows)
{
    string productName = dr["PRO_Name"].ToString();
    string productPrice = dr["PRO_Price"].ToString();
    string myUrl = "Handler.ashx?ProdID=" + dr["PRO_ID"].ToString();

        string sProduct = @"<div class='four-shop columns isotope-item'><div class='shop-item'><figure><img src='" + myUrl + "'/><figcaption class='item-description'><h5>" + productName + "</h5><span>R" + productPrice + "</span></figcaption></figure></div></div>";
        productPanel.Controls.Add(new LiteralControl(sProduct));
}

I would like to know how I can add a button with a click event that is in my literal control. I have created this button but can't add it to the literal control as literal controls only display markup and not server side controls. How can I go about adding this button dynamically with each product added to the shopping page from the database.

Button button = new Button();
button.Text = "Add to cart";
button.Click += button_Click;

解决方案

Try this approach.

Table tb = new Table();

for (int i = 1; i <= 10; i++)
        {
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            Button bt = new Button();
            bt.ID = i.ToString();
            bt.Text = "Button " + i.ToString();
            bt.Click += new EventHandler(btn_Click);
            td.Controls.Add(bt);
            tr.Controls.Add(td);
            tb.Controls.Add(tr);
        }

dvTest.Controls.Add(tb);

这篇关于有一个asp按钮控制字面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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