I am developing a add to cart page
I'm using using DataList to display items and a small TextBox with default value "1" to show quantity. if user change this value to some other integer then the new value should be needed on next page
I am finding it difficult that how to extract value from TextBox that i have used inside DataList
-------HERE IS MY CODE------ -
<asp:DataList ID="ProductDataList" DataSourceID ="ProductDataSource" RepeatColumns="3" RepeatDirection="Horizontal" runat="server" >
-
<ItemTemplate>
-
<div>
-
<div style="margin-top:10px;width:190px;height:auto;text-align:inherit">
-
<a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
<img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
-
</a>
-
</div>
-
-
<div>
-
<div style="width:190px;height:50px" >
-
<a style=" font-size:11px;color:#068EA8;background-color:White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>' >
-
<%# Eval("Product_Name") %>
-
</a>
-
</div>
-
-
<span style="font-size:12px;font-weight:bolder;color:#8B4513;background-color:White;" >
-
<strong>Price: </strong>
-
<%# Eval("Price", "{0:c}")%>
-
</span>
-
</div>
-
-
<div>
-
<table style="margin-left:70px;" >
-
<tr>
-
<td >
-
<asp:TextBox ID="TextBox1" Text="1" AutoPostBack="true" Width="14px" Height="18px" runat="server"></asp:TextBox>
-
</td>
-
<td style="width:5px;">
-
</td>
-
<td>
-
<a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
-
<img src='images/add_tocart.gif' alt="ADD TO CART" />
-
</a>
-
</td>
-
</tr>
-
</table>
-
</div>
-
-
<div>
-
<a style="font-size:11px;color:#068EA8;background-color:White;text-decoration:underline" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>Click for Details</a>
-
</div>
-
-
</div>
-
</ItemTemplate>
-
</asp:DataList>
-
Implement a method that will handle all of the TextBox TextChanged events.
(VB.NET) -
Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
-
-
End Sub
(C#) -
protected void text_change(object sender, EventArgs e)
-
{
-
}
In the asp.net code for the TextBox in your TemplateItem for your DataGrid, set the OnTextChanged attribute to the method that you just implemented. -
<asp:TextBox ID="Quantity"
-
runat="server"
-
AutoPostBack="true"
-
OnTextChanged="text_change"
-
Width="14px"
-
Height="18px"
-
Text='<%# Eval("Quantity") %>'>
-
</asp:TextBox>
So now, when the user changes the Text in the TextBox the page will post back to the server and the TextChanged method that you implemented will be called.
The sender parameter for the TextChanged method will be the TextBox whose Text Property changed.
Cast the sender parameter into a TextBox so that you can retrieve the Text from it.
(VB.NET) -
Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
-
Dim txtBox As TextBox = TryCast(sender, TextBox)
-
Dim qty As Integer
-
Integer.TryParse(txtBox.Text, qty)
-
End Sub
-
(C#) -
protected void text_change(object sender, EventArgs e)
-
{
-
TextBox txtBox = (TextBox)sender;
-
int qty;
-
int.TryParse(txtBox.Text, out qty);
-
}
Now you have the quantity that the user supplied.
You have to update your data source for the DataList so that the user's changes are actually accepted.
Before you can do this you need to retrieve the Item whose quantity changed.
To do that use the TextBox's BindingContainer property. This will be the DataListItem that the TextBox belongs to.
If you add an invisible asp.net Label to your TemplateItem that is bound to the ID of the product, you can retrieve the ID of the item from the DataListItem using the FindControl("labelID") method. -
<asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
(VB.NET) -
Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
-
Dim txtBox As TextBox = TryCast(sender, TextBox)
-
Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
-
Dim pidLabel As Label = listItem.FindControl("ProductId")
-
-
Dim pid As Integer
-
Integer.TryParse(pidLabel.Text, pid)
-
-
Dim qty As Integer
-
Integer.TryParse(txtBox.Text, qty)
-
'now you need to update your data source using the ProductId
-
End Sub
-
(C#) -
protected void text_change(object sender, EventArgs e)
-
{
-
TextBox txtBox = (TextBox)sender;
-
DataListItem listItem = (DataListItem)txtBox.BindingContainer;
-
Label pidLabel = (Label) listItem.FindControl("ProductId");
-
int pid;
-
int.TryParse(pidLabel.Text, out pid);
-
int qty;
-
int.TryParse(txtBox.Text, out qty);
-
//now you need to update your data source using the ProductId
-
}
Here is a full working example.
(ASP.NET code to hook up to a VB.NET code behind) - <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" Title="Products Page" %>
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<asp:DataList ID="ProductDataList" RepeatColumns="3" RepeatDirection="Horizontal"
-
runat="server">
-
<ItemTemplate>
-
<div style="margin-top: 10px; width: 190px; height: auto; text-align: inherit">
-
<asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
-
<a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
<img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
-
</a>
-
<div style="width: 190px;">
-
<a style="font-size: 11px; color: #068EA8; background-color: White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
<%# Eval("Product_Name") %>
-
</a>
-
<div>
-
<span style="font-size: 12px; font-weight: bolder; color: #8B4513; background-color: White;">
-
<strong>Price: </strong>
-
<%# Eval("Price", "{0:c}")%>
-
</span>
-
<asp:TextBox ID="Txt_Quantity" Text='<%# Eval("Quantity") %>' OnTextChanged="text_change"
-
AutoPostBack="true" runat="server" Style="margin: 0px,5px; width: 24px; height: 18px;"></asp:TextBox>
-
<div>
-
<strong>Cost:</strong>
-
<%#Eval("Cost", "{0:c}")%>
-
</div>
-
</div>
-
<div>
-
<a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
-
<img src='images/add_tocart.gif' alt="ADD TO CART" />
-
</a>
-
<br />
-
<a style="font-size: 11px; color: #068EA8; background-color: White; text-decoration: underline"
-
href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
Click for Details</a>
-
</div>
-
</div>
-
</div>
-
</ItemTemplate>
-
</asp:DataList>
-
</form>
-
</body>
-
</html>
If you want the ASP.NET code to hook up to a C# code behind then use the following <%Page %> definition: -
<%@ Page Language="C#" AutoEventWireup="true"
-
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" Title="Products Page" %>
(VB.NET Page Code) -
Partial Public Class _Default
-
Inherits System.Web.UI.Page
-
Private _products As List(Of Product)
-
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
GenerateList()
-
End Sub
-
-
Private Sub GenerateList()
-
If Session("_products") Is Nothing Then
-
_products = New List(Of Product)
-
For i As Integer = 0 To 9
-
Dim newProduct As New Product(String.Format("{0} {1}", "Product", i), i, String.Format("{0}{0}{0}-{0}-{0}{0}-{0}{0}{0}{0}{0}{0}-{0}", i), CType(i,Double))
-
_products.Add(newProduct)
-
Next
-
Else
-
_products = DirectCast(Session("_products"), List(Of Product))
-
End If
-
End Sub
-
-
-
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
-
Session("_products") = _list
-
ProductDataList.DataSource = _list
-
ProductDataList.DataBind()
-
End Sub
-
-
Protected Sub TxtChanged(ByVal sender As Object, ByVal e As EventArgs)
-
Dim txtBox As TextBox = TryCast(sender, TextBox)
-
Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
-
Dim pidLabel As Label = listItem.FindControl("ProductId")
-
Dim pid As Integer
-
Integer.TryParse(pidLabel.Text, pid)
-
-
Dim p As Product = _list.Find(Function(item) item.ProductId = pid)
-
If p IsNot Nothing Then
-
Integer.TryParse(txtBox.Text, p.Qty)
-
End If
-
End Sub
-
End Class
-
(C# Page Code) -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
namespace WebApplication1
-
{
-
public partial class _Default : System.Web.UI.Page
-
{
-
private List<Product> _products;
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (IsPostBack == false)
-
{
-
_products = new List<Product>();
-
for (int i = 1; i < 11; i++)
-
{
-
Product newProduct = new Product(String.Format("{0} {1}", "Product", i), i, String.Format("{0}{0}{0}-{0}-{0}{0}-{0}{0}{0}{0}{0}{0}-{0}", i), (double)i);
-
_products.Add(newProduct);
-
}
-
-
}
-
else
-
{
-
_products = (List<Product>)Session["_products"];
-
}
-
}
-
-
protected void text_change(object sender, EventArgs e)
-
{
-
TextBox txtBox = (TextBox)sender;
-
DataListItem listItem = (DataListItem)txtBox.BindingContainer;
-
Label pidLabel = (Label) listItem.FindControl("ProductID");
-
int pid;
-
int.TryParse(pidLabel.Text, out pid);
-
Product p = _products.Find((ep) => ep.ProductId == pid);
-
if (p != null)
-
{ int qty;
-
int.TryParse(txtBox.Text, out qty);
-
p.Quantity = qty;
-
}
-
-
}
-
protected void Page_PreRender(object sender, EventArgs e)
-
{
-
Session["_products"] = _products;
-
ProductDataList.DataSource = _products;
-
ProductDataList.DataBind();
-
}
-
}
-
-
}
-
Product class used as data for the data source
(VB.NET) -
Public Class Product
-
Private _name As String
-
Private _id As Integer
-
Private _isbn As String
-
Private _qty As Integer
-
Private _price As Double
-
-
Public Property Product_Name() As String
-
Get
-
Return _name
-
End Get
-
Set(ByVal value As String)
-
_name = value
-
End Set
-
End Property
-
Public Property ProductId() As Integer
-
Get
-
Return _id
-
End Get
-
Set(ByVal value As Integer)
-
_id = value
-
End Set
-
End Property
-
Public Property ISBN() As String
-
Get
-
Return _isbn
-
End Get
-
Set(ByVal value As String)
-
_isbn = value
-
End Set
-
End Property
-
Public Property Price() As Double
-
Get
-
Return _price
-
End Get
-
Set(ByVal value As Double)
-
_price = value
-
End Set
-
End Property
-
Public Property Quantity() As Integer
-
Get
-
Return _qty
-
End Get
-
Set(ByVal value As Integer)
-
_qty = value
-
End Set
-
End Property
-
Public ReadOnly Property Cost() As Double
-
Get
-
Return Price * Qty
-
End Get
-
End Property
-
-
Public Sub New(ByVal name As String, ByVal id As Integer, ByVal price As Double)
-
_id = id
-
_name = name
-
_qty = 0
-
_price = price
-
End Sub
-
End Class
(C#) -
public class Product
-
{
-
public string Product_Name { get; set; }
-
public int ProductId { get; set; }
-
public string ISBN { get; set; }
-
public double Quantity { get; set; }
-
public double Price { get; set; }
-
public double Cost
-
{
-
get { return this.Quantity * this.Price; }
-
}
-
public string Product_Image { get; set; }
-
-
public Product(string name, int id, string isbn, double price)
-
{
-
this.Quantity = 0;
-
this.Product_Name = name;
-
this.ProductId = id;
-
this.ISBN = isbn;
-
this.Price = price;
-
}
-
}
7 11916
Implement a method that will handle all of the TextBox TextChanged events.
(VB.NET) -
Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
-
-
End Sub
(C#) -
protected void text_change(object sender, EventArgs e)
-
{
-
}
In the asp.net code for the TextBox in your TemplateItem for your DataGrid, set the OnTextChanged attribute to the method that you just implemented. -
<asp:TextBox ID="Quantity"
-
runat="server"
-
AutoPostBack="true"
-
OnTextChanged="text_change"
-
Width="14px"
-
Height="18px"
-
Text='<%# Eval("Quantity") %>'>
-
</asp:TextBox>
So now, when the user changes the Text in the TextBox the page will post back to the server and the TextChanged method that you implemented will be called.
The sender parameter for the TextChanged method will be the TextBox whose Text Property changed.
Cast the sender parameter into a TextBox so that you can retrieve the Text from it.
(VB.NET) -
Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
-
Dim txtBox As TextBox = TryCast(sender, TextBox)
-
Dim qty As Integer
-
Integer.TryParse(txtBox.Text, qty)
-
End Sub
-
(C#) -
protected void text_change(object sender, EventArgs e)
-
{
-
TextBox txtBox = (TextBox)sender;
-
int qty;
-
int.TryParse(txtBox.Text, out qty);
-
}
Now you have the quantity that the user supplied.
You have to update your data source for the DataList so that the user's changes are actually accepted.
Before you can do this you need to retrieve the Item whose quantity changed.
To do that use the TextBox's BindingContainer property. This will be the DataListItem that the TextBox belongs to.
If you add an invisible asp.net Label to your TemplateItem that is bound to the ID of the product, you can retrieve the ID of the item from the DataListItem using the FindControl("labelID") method. -
<asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
(VB.NET) -
Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
-
Dim txtBox As TextBox = TryCast(sender, TextBox)
-
Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
-
Dim pidLabel As Label = listItem.FindControl("ProductId")
-
-
Dim pid As Integer
-
Integer.TryParse(pidLabel.Text, pid)
-
-
Dim qty As Integer
-
Integer.TryParse(txtBox.Text, qty)
-
'now you need to update your data source using the ProductId
-
End Sub
-
(C#) -
protected void text_change(object sender, EventArgs e)
-
{
-
TextBox txtBox = (TextBox)sender;
-
DataListItem listItem = (DataListItem)txtBox.BindingContainer;
-
Label pidLabel = (Label) listItem.FindControl("ProductId");
-
int pid;
-
int.TryParse(pidLabel.Text, out pid);
-
int qty;
-
int.TryParse(txtBox.Text, out qty);
-
//now you need to update your data source using the ProductId
-
}
Here is a full working example.
(ASP.NET code to hook up to a VB.NET code behind) - <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" Title="Products Page" %>
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<asp:DataList ID="ProductDataList" RepeatColumns="3" RepeatDirection="Horizontal"
-
runat="server">
-
<ItemTemplate>
-
<div style="margin-top: 10px; width: 190px; height: auto; text-align: inherit">
-
<asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
-
<a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
<img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
-
</a>
-
<div style="width: 190px;">
-
<a style="font-size: 11px; color: #068EA8; background-color: White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
<%# Eval("Product_Name") %>
-
</a>
-
<div>
-
<span style="font-size: 12px; font-weight: bolder; color: #8B4513; background-color: White;">
-
<strong>Price: </strong>
-
<%# Eval("Price", "{0:c}")%>
-
</span>
-
<asp:TextBox ID="Txt_Quantity" Text='<%# Eval("Quantity") %>' OnTextChanged="text_change"
-
AutoPostBack="true" runat="server" Style="margin: 0px,5px; width: 24px; height: 18px;"></asp:TextBox>
-
<div>
-
<strong>Cost:</strong>
-
<%#Eval("Cost", "{0:c}")%>
-
</div>
-
</div>
-
<div>
-
<a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
-
<img src='images/add_tocart.gif' alt="ADD TO CART" />
-
</a>
-
<br />
-
<a style="font-size: 11px; color: #068EA8; background-color: White; text-decoration: underline"
-
href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
Click for Details</a>
-
</div>
-
</div>
-
</div>
-
</ItemTemplate>
-
</asp:DataList>
-
</form>
-
</body>
-
</html>
If you want the ASP.NET code to hook up to a C# code behind then use the following <%Page %> definition: -
<%@ Page Language="C#" AutoEventWireup="true"
-
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" Title="Products Page" %>
(VB.NET Page Code) -
Partial Public Class _Default
-
Inherits System.Web.UI.Page
-
Private _products As List(Of Product)
-
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
GenerateList()
-
End Sub
-
-
Private Sub GenerateList()
-
If Session("_products") Is Nothing Then
-
_products = New List(Of Product)
-
For i As Integer = 0 To 9
-
Dim newProduct As New Product(String.Format("{0} {1}", "Product", i), i, String.Format("{0}{0}{0}-{0}-{0}{0}-{0}{0}{0}{0}{0}{0}-{0}", i), CType(i,Double))
-
_products.Add(newProduct)
-
Next
-
Else
-
_products = DirectCast(Session("_products"), List(Of Product))
-
End If
-
End Sub
-
-
-
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
-
Session("_products") = _list
-
ProductDataList.DataSource = _list
-
ProductDataList.DataBind()
-
End Sub
-
-
Protected Sub TxtChanged(ByVal sender As Object, ByVal e As EventArgs)
-
Dim txtBox As TextBox = TryCast(sender, TextBox)
-
Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
-
Dim pidLabel As Label = listItem.FindControl("ProductId")
-
Dim pid As Integer
-
Integer.TryParse(pidLabel.Text, pid)
-
-
Dim p As Product = _list.Find(Function(item) item.ProductId = pid)
-
If p IsNot Nothing Then
-
Integer.TryParse(txtBox.Text, p.Qty)
-
End If
-
End Sub
-
End Class
-
(C# Page Code) -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
namespace WebApplication1
-
{
-
public partial class _Default : System.Web.UI.Page
-
{
-
private List<Product> _products;
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (IsPostBack == false)
-
{
-
_products = new List<Product>();
-
for (int i = 1; i < 11; i++)
-
{
-
Product newProduct = new Product(String.Format("{0} {1}", "Product", i), i, String.Format("{0}{0}{0}-{0}-{0}{0}-{0}{0}{0}{0}{0}{0}-{0}", i), (double)i);
-
_products.Add(newProduct);
-
}
-
-
}
-
else
-
{
-
_products = (List<Product>)Session["_products"];
-
}
-
}
-
-
protected void text_change(object sender, EventArgs e)
-
{
-
TextBox txtBox = (TextBox)sender;
-
DataListItem listItem = (DataListItem)txtBox.BindingContainer;
-
Label pidLabel = (Label) listItem.FindControl("ProductID");
-
int pid;
-
int.TryParse(pidLabel.Text, out pid);
-
Product p = _products.Find((ep) => ep.ProductId == pid);
-
if (p != null)
-
{ int qty;
-
int.TryParse(txtBox.Text, out qty);
-
p.Quantity = qty;
-
}
-
-
}
-
protected void Page_PreRender(object sender, EventArgs e)
-
{
-
Session["_products"] = _products;
-
ProductDataList.DataSource = _products;
-
ProductDataList.DataBind();
-
}
-
}
-
-
}
-
Product class used as data for the data source
(VB.NET) -
Public Class Product
-
Private _name As String
-
Private _id As Integer
-
Private _isbn As String
-
Private _qty As Integer
-
Private _price As Double
-
-
Public Property Product_Name() As String
-
Get
-
Return _name
-
End Get
-
Set(ByVal value As String)
-
_name = value
-
End Set
-
End Property
-
Public Property ProductId() As Integer
-
Get
-
Return _id
-
End Get
-
Set(ByVal value As Integer)
-
_id = value
-
End Set
-
End Property
-
Public Property ISBN() As String
-
Get
-
Return _isbn
-
End Get
-
Set(ByVal value As String)
-
_isbn = value
-
End Set
-
End Property
-
Public Property Price() As Double
-
Get
-
Return _price
-
End Get
-
Set(ByVal value As Double)
-
_price = value
-
End Set
-
End Property
-
Public Property Quantity() As Integer
-
Get
-
Return _qty
-
End Get
-
Set(ByVal value As Integer)
-
_qty = value
-
End Set
-
End Property
-
Public ReadOnly Property Cost() As Double
-
Get
-
Return Price * Qty
-
End Get
-
End Property
-
-
Public Sub New(ByVal name As String, ByVal id As Integer, ByVal price As Double)
-
_id = id
-
_name = name
-
_qty = 0
-
_price = price
-
End Sub
-
End Class
(C#) -
public class Product
-
{
-
public string Product_Name { get; set; }
-
public int ProductId { get; set; }
-
public string ISBN { get; set; }
-
public double Quantity { get; set; }
-
public double Price { get; set; }
-
public double Cost
-
{
-
get { return this.Quantity * this.Price; }
-
}
-
public string Product_Image { get; set; }
-
-
public Product(string name, int id, string isbn, double price)
-
{
-
this.Quantity = 0;
-
this.Product_Name = name;
-
this.ProductId = id;
-
this.ISBN = isbn;
-
this.Price = price;
-
}
-
}
Thanks for your reply...
but m using C# coding and you had given me solution in VB.
Please provide me solution in C#
Thanks in adv.
I took the time to go through your incomplete code and provide you with a thorough answer.
The least you could do is attempt to read read my answer to understand what I was doing...that way you could take the concepts that I posted and attempt to implement the solution on your own.
The absolute VERY least you could do is search for a website (like this one) that will convert C# to VB.NET and vice versa.
By the way, while my answer includes a working solution that is roughly based on your code, it will not work as-is for your specific application you if copy & paste the solution into your application. The code example that I posted is meant as a guideline to help you understand the concepts I talked about in my answer.
Edit: I added the C# syntax version of the solution.
-Frinny
NeoPa 32,556
Expert Mod 16PB
@upcomingaman
Please see my PM (Private Message).
No problem Sir....thanks a lot for your support. I will try to Implement this logic in my project using C# after going through your hint in VB
I am trying to extract value of textbox used in Datalist on textchange event but when i run this coe it gives me an error
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index " -
<asp:DataList ID="ProductDataList" DataSourceID ="ProductDataSource" RepeatColumns="3" RepeatDirection="Horizontal" runat="server" >
-
<ItemTemplate>
-
<div >
-
<div style="margin-top:10px;width:190px;height:auto;text-align:inherit">
-
<a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
-
<img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
-
</a>
-
</div>
-
<div>
-
<div style="width:190px;height:50px" >
-
<a style=" font-size:11px;color:#068EA8;background-color:White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>' >
-
<%# Eval("Product_Name") %>
-
</a>
-
<div>
-
<span style="font-size:12px;font-weight:bolder;color:#8B4513;background-color:White;" >
-
<strong>Price: </strong>
-
<%# Eval("Price", "{0:c}")%>
-
</span>
-
</div>
-
-
<div>
-
<table style="margin-left:70px;" >
-
<tr>
-
<td >
-
<asp:TextBox ID="TextBox1" Text="1" OnTextChanged="text_change" AutoPostBack="true" Width="14px" Height="18px" runat="server"></asp:TextBox>
-
</td>
-
<td style="width:5px;">
-
</td>
-
<td>
-
<a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
-
<img src='images/add_tocart.gif' alt="ADD TO CART" />
-
</a>
-
</td>
-
</tr>
-
</table>
-
</div>
-
<div>
-
<a style="font-size:11px;color:#068EA8;background-color:White;text-decoration:underline" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>Click for Details</a>
-
</div>
-
</div>
-
-
</ItemTemplate>
-
</asp:DataList>
-
-
-
protected void text_change(object sender, EventArgs e)
-
{
-
string noofbooks = Server.HtmlEncode(((TextBox)ProductDataList.Items[Product DataList.SelectedIndex].FindControl("TextBox1")).Text);
-
}
Please provide me some help
Thanks in adv
I don't think you actually read my previous post....
But, at least you showed that you've made an attempt to solve your problem.
Your text_changed method handles the TextBox1 's OnTextChanged event.
Using the FindControl method on the ProductDataList to retrieve the TextBox won't work in the text_changed method because there are multiple TextBox1's in the ProductDataList method. It doesn't really make sense to try to do this in the first place...
Since the text_changed handles the OnTextChanged event for the TextBox, you know that the sender parameter is the TextBox1 that you need to retrieve the value from.
So, change your code to cast the sender into a TextBox and retrieve the text from it: -
protected void text_change(object sender, EventArgs e)
-
{
-
TextBox txtBox = (TextBox)sender;
-
string noofbooks = txtBox.Text;
-
}
-Frinny
Sign in to post your reply or Sign up for a free account.
Similar topics
by: etantonio |
last post by:
Good Morning,
I still havent solved my problem with a textbox inside an
editItemTemplate
of a datalist,
in fact now it is filled with the word "CIAOOOOOO" , if a runtime in
the
form I put a...
|
by: Mark Sandfox |
last post by:
What I need to do is simple in any other code, but I am having a problem
finding the correct syntax for this function.
I have the following:
<form Runat="Server">
|
by: Peter Afonin |
last post by:
Hello:
I've never used the DataList before, and I have a problem now:
In the ItemTemplate I put the LinkButton and a TextBox (not databound, I
want the user to be able to type in it). Then I...
|
by: Patrick.O.Ige |
last post by:
Hi guys,
Just using these small snippet below using Datalist with sorting!
But it appears i can see no Data on the screen when compiled with Visual
Studio .Net!
With WebMatrix its running fine!:)...
|
by: craigkenisston |
last post by:
Hi,
In a datalist I'm displaying data comming from a SQL Server stored
procedure that contains null values in numeric values. Since the stored
is already used in other applications I can't...
|
by: Chris Kettenbach |
last post by:
Good morning all,
I am sure this has been asked but I did not see anything.
I have a datalist control. In the edititemtemplate I have a dropdownlist.
I know on the itemdatabound event is where I...
|
by: VB Programmer |
last post by:
In my ASPX page how do I get the .text value of a textbox that is in the
ItemTemplate of a datalist (using HTML)?
The textbox contains the quantity for a shopping cart item.
The textbox is...
|
by: mintboy |
last post by:
hello
thank U for take a look this Message !
/////////////////c# code file
private void DataListClass_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{...
|
by: etam |
last post by:
Hi,
i have a DataList with a TextBox added by me:
<asp:DataList ID="GradeDataList" runat="server"
DataKeyField="id" DataSourceID="ProjectsObjectDataSource">
<ItemTemplate>
topic:
<asp:Label...
|
by: Hillbilly |
last post by:
Anybody have any sage advice on this frustrating "feature" of ASP.NET? I
have a TextBox in the EditItemTemplate of a DataList I can't seem to find
for some reason that is I believe related to the...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |