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>
-
7 11981 Frinavale 9,735
Recognized Expert Moderator Expert
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 BindingContaine r 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("la belID") 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.
Frinavale 9,735
Recognized Expert Moderator Expert
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,571
Recognized Expert Moderator MVP
@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
Frinavale 9,735
Recognized Expert Moderator Expert
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 different word
like "PIZZAAAA" this new word is not available in the Update_Command
following :
|
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 try to retrieve the value of
the textbox by pressing the Linkbutton, like this, pretty much like I do it
in Datagrid:
Private Sub MyList_ItemCommand(ByVal source As Object, ByVal e As
|
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!:)
Whats missing in VStudio .Net?
Imports System.Data.SqlClient
|
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 change it.
So, I need to catch this nulls and convert them to zeroes so, they
display in the TD on the table I have in the datalist.
I have this method :
| |
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 can set the dropdownlist
selectedindex. How do I set the correct value?
private void lstDegrees_ItemDataBound(object sender, DataListItemEventArgs
e)
{
|
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 called "txtQty" and the current HTML is this (I need to
replace xxxx with the text value from the textbox):
<A href='UpdateCart.aspx?CartId=<%# DataBinder.Eval(Container.DataItem,
"CartID") %>&Qty=xxxx'><IMG src="Resources/UpdateBtn.jpg"...
|
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)
{
string UpdateName = ((TextBox)e.Item.FindControl("TextBoxName")).Text;//
|
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 ID="topicLabel" runat="server" Text='<%#
Eval("topic") %>'></asp:Label><br />
|
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 imfamous complexity and
undocumented vagaries of user controls and MasterPages which are also a type
of user control.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |