从动态创建的组合框中获取值以用于postbackurl

时间:2015-05-14 20:23:22

标签: c# asp.net combobox devexpress

这就是事情,我是一个没有经验的程序员,我正在从头开始创建一个网上商店,我可以列出所有产品,并且我在每个产品上动态创建了一个按钮,将其添加到购物车,然后我添加了一个组合框用于产品数量,这就是问题所在。

之前我在网址上发送了产品ID,但现在我找不到发送产品数量的方法,因为在我使用postbackURL创建网址并将产品ID添加到网址之前,没关系。< / p>

所以现在,我需要一种方法将数量添加到URL,我不知道如何使用动态创建的控件执行此操作,我为每个按钮和组合框创建了一个ID +一个随机生成的数字(两个按钮)和combobox有相同的数字,所以我知道每个的名称。

btw我正在使用devexpress框架。

这是我插入容器的代码:

using System;
using System.Text;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DevExpress.Web;
using Microsoft.Win32;
using WebArrendatariosDX.Class;
using System.Data.SqlClient;

namespace WebArrendatariosDX.Productos
{
    public partial class ProductMaster : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            GetProducts();
        }

        private void GetProducts()
        {
            var icc = (string) Session["icc"];
            var cid = (string) Session["uid"];
            var dbc = new DbClass();
            dbc.GetConnStr("dbtoconnect");
            string str;
            dbc.SetQueryType("TXT", out str);
            dbc.SetQuery("SP_GET_PRODUCTS @CUSTOMER_ID=" + cid + ", @CLIENT_ID=" + icc, out str);
            SqlDataReader rdr = dbc.exec_ret_sqldatareader();
            try
            {
                while (rdr.Read())
                {
                    //<Tabla de producto>
                    var productTable = new Table {CssClass = "templateTable"}; //<table></table>
                    productTable.Attributes.Add("RUNAT","SERVER");
                    productTable.Rows.Add(maketitleRow(rdr["ST_DESC_TIPOPRODUCTO"].ToString(), rdr["ID"].ToString(),
                        rdr["ST_CODINTERNO_TIPOPRODUCTO"].ToString()));
                    //<tr></tr>
                    productTable.Rows.Add(makecontentRow(rdr["ST_DESC_MARCAPRODUCTO"].ToString(),
                        rdr["IN_PRECIOVENTA_TIPOPRODUCTO"].ToString(), 1)); //<tr></tr>
                    productTable.Rows.Add(makecontentRow(rdr["ST_DESC_MARCAPRODUCTO"].ToString(),
                        rdr["IN_PRECIOVENTA_TIPOPRODUCTO"].ToString(), 2)); //<tr></tr>

                    //</Tabla de producto>
                    products_container.Controls.Add(productTable); //meter contenido a la tabla
                    products_container.Controls.Add(new Literal() {Text = "<br />"});
                }
            }
            catch (Exception ex)
            {
                var errorTable = new Table {CssClass = "templateTable"}; //<table></table>
                var errorRow = new TableRow(); //<tr></tr>
                var errorCell = new TableCell
                {
                    Text = "Error al consultar productos: " + ex.Message + "<br> Intente recargando la pagina.",
                    ColumnSpan = 4,
                    CssClass = "value"
                }; //<td></td>
                errorRow.Cells.Add(errorCell);
                errorTable.Rows.Add(errorRow);
                products_container.Controls.Add(errorTable);
            }

        }

        private string tob64(string toconv)
        {
            byte[] bytesToEncode = Encoding.UTF8.GetBytes(toconv);
            string encodedText = Convert.ToBase64String(bytesToEncode);
            return encodedText;
        }

        private TableRow maketitleRow(string productName, string imgid, string sku)
        {
            var titleRow = new TableRow(); //<tr></tr>
            var titleImageCell = new TableCell { CssClass = "imageCell", RowSpan = 3 };
            var ime = new ASPxImage {Width = 120, Height = 120};
            ime.ImageUrl = "~/Util/image.ashx?id=" + imgid;
            titleImageCell.Controls.Add(ime);
            var titleCell = new TableCell {Text = productName, ColumnSpan = 3, CssClass = "value"}; //<td></td>
            //**** COLUMNA CON BOTONES
            var ButtonsCell = new TableCell { CssClass = "imageCell", RowSpan = 3 };
            var btn_addtoCart = new ASPxButton();
            var cbx_qty = new ASPxComboBox(); //the combobox that I need to get the value from
            cbx_qty.DropDownStyle = DropDownStyle.DropDownList;
            cbx_qty.Items.Add("Cantidad...", 0);
            for (int i = 1; i < 11; i++)
            {
                cbx_qty.Items.Add(Convert.ToString(i), i);
            }
            cbx_qty.SelectedIndex = 0;
            btn_addtoCart.Text = "Agregar al Carro";
            var rnd = new Random();
            int rnds = rnd.Next(6236, 6124125);
            btn_addtoCart.ID = "btn_atc_" + rnds; //my button that is created dynamically with a custom name
            string idbtn = btn_addtoCart.ID.ToString();
            cbx_qty.ID = "cbx_" + idbtn; //my custom ID for each combobox that is created dynamically
            btn_addtoCart.PostBackUrl = "~/Productos/Cart/CartMaster.aspx?nod=" + tob64(sku) + "&pid=" + rnds;// this works but now I need to add the quantity
            ButtonsCell.Controls.Add(cbx_qty);
            ButtonsCell.Controls.Add(btn_addtoCart);
            //**** /COLUMNA CON BOTONES
            titleRow.Cells.Add(titleImageCell); //IMAGEN DEL PRODUCTO
            titleRow.Cells.Add(titleCell); //TITULO DEL PRODUCTO
            titleRow.Cells.Add(ButtonsCell); //AGREGA BOTONES
            return titleRow;
        }

        private TableRow makecontentRow(string manufacturer, string price, int rowno)
        {
            var contentRow = new TableRow(); //<tr></tr>
            if (rowno == 1)
            {
                var contentManufacturerTextCell = new TableCell { Text = "Fabricante" }; //<td></td>
                var contentManufacturerCell = new TableCell { Text = manufacturer, CssClass = "value" }; //<td></td>
                contentRow.Cells.Add(contentManufacturerTextCell);
                contentRow.Cells.Add(contentManufacturerCell);
            }
            else if (rowno == 2)
            {
                var contentPriceTextCell = new TableCell { Text = "Precio" }; //<td></td>
                var contentPriceCell = new TableCell { Text = "$ " + price, CssClass = "value" }; //<td></td>
                contentRow.Cells.Add(contentPriceTextCell);
                contentRow.Cells.Add(contentPriceCell);
            }
            return contentRow;
        }
    }
}

0 个答案:

没有答案