473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to extract value of textbox used in datalist

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------



Expand|Select|Wrap|Line Numbers
  1. <asp:DataList ID="ProductDataList"   DataSourceID ="ProductDataSource"  RepeatColumns="3"  RepeatDirection="Horizontal" runat="server"  >
  2.   <ItemTemplate>         
  3.     <div>                   
  4.       <div style="margin-top:10px;width:190px;height:auto;text-align:inherit">
  5.         <a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  6.           <img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
  7.         </a>
  8.       </div>
  9.  
  10.       <div>
  11.         <div style="width:190px;height:50px" >
  12.           <a  style=" font-size:11px;color:#068EA8;background-color:White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>' >
  13.           <%# Eval("Product_Name") %> 
  14.           </a>
  15.         </div>
  16.  
  17.         <span style="font-size:12px;font-weight:bolder;color:#8B4513;background-color:White;" >
  18.           <strong>Price: </strong>
  19.           <%# Eval("Price", "{0:c}")%>
  20.        </span>
  21.       </div>         
  22.  
  23.       <div>
  24.         <table style="margin-left:70px;" >
  25.           <tr>
  26.             <td >
  27.               <asp:TextBox ID="TextBox1"  Text="1" AutoPostBack="true" Width="14px" Height="18px" runat="server"></asp:TextBox>
  28.             </td>
  29.             <td style="width:5px;">
  30.             </td>
  31.             <td>
  32.               <a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
  33.                 <img  src='images/add_tocart.gif' alt="ADD TO CART"   />
  34.               </a>             
  35.             </td>
  36.           </tr>
  37.         </table>
  38.       </div>
  39.  
  40.       <div>
  41.         <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>
  42.       </div>
  43.  
  44.     </div>                         
  45.   </ItemTemplate>
  46. </asp:DataList>
  47.  
Aug 25 '11 #1

✓ answered by Frinavale

Implement a method that will handle all of the TextBox TextChanged events.

(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
  2.  
  3. End Sub
(C#)
Expand|Select|Wrap|Line Numbers
  1. protected void text_change(object sender, EventArgs e)
  2. {
  3. }
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.
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="Quantity" 
  2.              runat="server" 
  3.              AutoPostBack="true" 
  4.              OnTextChanged="text_change"
  5.              Width="14px" 
  6.              Height="18px" 
  7.              Text='<%# Eval("Quantity") %>'>
  8. </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)
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
  2.         Dim txtBox As TextBox = TryCast(sender, TextBox)
  3.         Dim qty As Integer
  4.         Integer.TryParse(txtBox.Text, qty)
  5.     End Sub
  6.  
(C#)
Expand|Select|Wrap|Line Numbers
  1. protected void text_change(object sender, EventArgs e)
  2. {
  3.   TextBox txtBox = (TextBox)sender;
  4.   int qty;
  5.   int.TryParse(txtBox.Text, out qty);
  6. }
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.

Expand|Select|Wrap|Line Numbers
  1. <asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
  2.         Dim txtBox As TextBox = TryCast(sender, TextBox)
  3.         Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
  4.         Dim pidLabel As Label = listItem.FindControl("ProductId")
  5.  
  6.         Dim pid As Integer
  7.         Integer.TryParse(pidLabel.Text, pid)
  8.  
  9.         Dim qty As Integer
  10.         Integer.TryParse(txtBox.Text, qty)
  11.         'now you need to update your data source using the ProductId
  12.     End Sub
  13.  
(C#)
Expand|Select|Wrap|Line Numbers
  1.  protected void text_change(object sender, EventArgs e)
  2. {
  3.   TextBox txtBox = (TextBox)sender;
  4.   DataListItem listItem = (DataListItem)txtBox.BindingContainer;
  5.   Label pidLabel = (Label) listItem.FindControl("ProductId");
  6.   int pid;
  7.   int.TryParse(pidLabel.Text, out pid);
  8.   int qty;
  9.   int.TryParse(txtBox.Text, out qty);
  10.   //now you need to update your data source using the ProductId
  11. }


Here is a full working example.
(ASP.NET code to hook up to a VB.NET code behind)
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" Title="Products Page" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.  
  7. </head>
  8. <body>
  9.     <form id="form1" runat="server">
  10.       <asp:DataList ID="ProductDataList" RepeatColumns="3" RepeatDirection="Horizontal"
  11.     runat="server">
  12.         <ItemTemplate>
  13.           <div style="margin-top: 10px; width: 190px; height: auto; text-align: inherit">
  14.             <asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
  15.             <a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  16.                 <img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
  17.             </a>
  18.             <div style="width: 190px;">
  19.                 <a style="font-size: 11px; color: #068EA8; background-color: White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  20.                     <%# Eval("Product_Name") %>
  21.                 </a>
  22.                 <div>
  23.                     <span style="font-size: 12px; font-weight: bolder; color: #8B4513; background-color: White;">
  24.                         <strong>Price: </strong>
  25.                         <%# Eval("Price", "{0:c}")%>
  26.                     </span>
  27.                     <asp:TextBox ID="Txt_Quantity" Text='<%# Eval("Quantity") %>' OnTextChanged="text_change"
  28.                         AutoPostBack="true" runat="server" Style="margin: 0px,5px; width: 24px; height: 18px;"></asp:TextBox>
  29.                     <div>
  30.                         <strong>Cost:</strong>
  31.                         <%#Eval("Cost", "{0:c}")%>
  32.                     </div>
  33.                 </div>
  34.                 <div>
  35.                     <a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
  36.                         <img src='images/add_tocart.gif' alt="ADD TO CART" />
  37.                     </a>
  38.                     <br />
  39.                     <a style="font-size: 11px; color: #068EA8; background-color: White; text-decoration: underline"
  40.                         href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  41.                         Click for Details</a>
  42.                 </div>
  43.             </div>
  44.           </div>
  45.         </ItemTemplate>
  46.       </asp:DataList>
  47.     </form>
  48. </body>
  49. </html>
If you want the ASP.NET code to hook up to a C# code behind then use the following <%Page %> definition:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page  Language="C#" AutoEventWireup="true"
  2.     CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" Title="Products Page" %>
(VB.NET Page Code)
Expand|Select|Wrap|Line Numbers
  1. Partial Public Class _Default
  2.     Inherits System.Web.UI.Page
  3.     Private _products As List(Of Product)
  4.  
  5.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.         GenerateList()
  7.     End Sub
  8.  
  9.     Private Sub GenerateList()
  10.         If Session("_products") Is Nothing Then
  11.             _products = New List(Of Product)
  12.             For i As Integer = 0 To 9
  13.                 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))
  14.                 _products.Add(newProduct)
  15.             Next
  16.         Else
  17.             _products = DirectCast(Session("_products"), List(Of Product))
  18.         End If
  19.     End Sub
  20.  
  21.  
  22.     Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  23.         Session("_products") = _list
  24.         ProductDataList.DataSource = _list
  25.         ProductDataList.DataBind()
  26.     End Sub
  27.  
  28.     Protected Sub TxtChanged(ByVal sender As Object, ByVal e As EventArgs)
  29.         Dim txtBox As TextBox = TryCast(sender, TextBox)
  30.         Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
  31.         Dim pidLabel As Label = listItem.FindControl("ProductId")
  32.         Dim pid As Integer
  33.         Integer.TryParse(pidLabel.Text, pid)
  34.  
  35.         Dim p As Product = _list.Find(Function(item) item.ProductId = pid)
  36.         If p IsNot Nothing Then
  37.             Integer.TryParse(txtBox.Text, p.Qty)
  38.         End If
  39.     End Sub
  40. End Class
  41.  
(C# Page Code)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class _Default : System.Web.UI.Page
  11.     {
  12.         private List<Product> _products;
  13.         protected void Page_Load(object sender, EventArgs e)
  14.         {
  15.             if (IsPostBack == false)
  16.             {
  17.                 _products = new List<Product>();
  18.                 for (int i = 1; i < 11; i++)
  19.                 {
  20.                     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);
  21.                     _products.Add(newProduct);
  22.                 }
  23.  
  24.             }
  25.             else
  26.             {
  27.                 _products = (List<Product>)Session["_products"];
  28.             }
  29.         }
  30.  
  31.         protected void text_change(object sender, EventArgs e)
  32.         {
  33.             TextBox txtBox = (TextBox)sender;
  34.             DataListItem listItem = (DataListItem)txtBox.BindingContainer;
  35.             Label pidLabel = (Label) listItem.FindControl("ProductID");
  36.             int pid;
  37.             int.TryParse(pidLabel.Text, out pid);
  38.             Product p = _products.Find((ep) => ep.ProductId == pid);
  39.             if (p != null)
  40.             {   int qty;
  41.                 int.TryParse(txtBox.Text, out qty);
  42.                 p.Quantity = qty;
  43.             }
  44.  
  45.         }
  46.         protected void Page_PreRender(object sender, EventArgs e)
  47.         {
  48.             Session["_products"] = _products;
  49.             ProductDataList.DataSource = _products;
  50.             ProductDataList.DataBind();
  51.         }
  52.     }
  53.  
  54. }
  55.  
Product class used as data for the data source
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Public Class Product
  2.     Private _name As String
  3.     Private _id As Integer
  4.     Private _isbn As String
  5.     Private _qty As Integer
  6.     Private _price As Double
  7.  
  8.     Public Property Product_Name() As String
  9.         Get
  10.             Return _name
  11.         End Get
  12.         Set(ByVal value As String)
  13.             _name = value
  14.         End Set
  15.     End Property
  16.     Public Property ProductId() As Integer
  17.         Get
  18.             Return _id
  19.         End Get
  20.         Set(ByVal value As Integer)
  21.             _id = value
  22.         End Set
  23.     End Property
  24.     Public Property ISBN() As String
  25.         Get
  26.             Return _isbn
  27.         End Get
  28.         Set(ByVal value As String)
  29.             _isbn = value
  30.         End Set
  31.     End Property
  32.     Public Property Price() As Double
  33.         Get
  34.             Return _price
  35.         End Get
  36.         Set(ByVal value As Double)
  37.             _price = value
  38.         End Set
  39.     End Property
  40.     Public Property Quantity() As Integer
  41.         Get
  42.             Return _qty
  43.         End Get
  44.         Set(ByVal value As Integer)
  45.             _qty = value
  46.         End Set
  47.     End Property
  48.     Public ReadOnly Property Cost() As Double
  49.         Get
  50.             Return Price * Qty
  51.         End Get
  52.     End Property
  53.  
  54.     Public Sub New(ByVal name As String, ByVal id As Integer, ByVal price As Double)
  55.         _id = id
  56.         _name = name
  57.         _qty = 0
  58.         _price = price
  59.     End Sub
  60. End Class
(C#)
Expand|Select|Wrap|Line Numbers
  1.   public class Product
  2.     {
  3.         public string Product_Name { get; set; }
  4.         public int ProductId { get; set; }
  5.         public string ISBN { get; set; }
  6.         public double Quantity { get; set; }
  7.         public double Price { get; set; }
  8.         public  double Cost 
  9.         {
  10.             get { return this.Quantity * this.Price; } 
  11.         }
  12.         public string Product_Image { get; set; }
  13.  
  14.         public Product(string name, int id, string isbn, double price)
  15.         {
  16.             this.Quantity = 0;
  17.             this.Product_Name = name;
  18.             this.ProductId = id;
  19.             this.ISBN = isbn;
  20.             this.Price = price;
  21.         }
  22.     }

7 11916
Frinavale
9,735 Expert Mod 8TB
Implement a method that will handle all of the TextBox TextChanged events.

(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
  2.  
  3. End Sub
(C#)
Expand|Select|Wrap|Line Numbers
  1. protected void text_change(object sender, EventArgs e)
  2. {
  3. }
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.
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="Quantity" 
  2.              runat="server" 
  3.              AutoPostBack="true" 
  4.              OnTextChanged="text_change"
  5.              Width="14px" 
  6.              Height="18px" 
  7.              Text='<%# Eval("Quantity") %>'>
  8. </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)
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
  2.         Dim txtBox As TextBox = TryCast(sender, TextBox)
  3.         Dim qty As Integer
  4.         Integer.TryParse(txtBox.Text, qty)
  5.     End Sub
  6.  
(C#)
Expand|Select|Wrap|Line Numbers
  1. protected void text_change(object sender, EventArgs e)
  2. {
  3.   TextBox txtBox = (TextBox)sender;
  4.   int qty;
  5.   int.TryParse(txtBox.Text, out qty);
  6. }
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.

Expand|Select|Wrap|Line Numbers
  1. <asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Protected Sub text_change(ByVal sender As Object, ByVal e As EventArgs)
  2.         Dim txtBox As TextBox = TryCast(sender, TextBox)
  3.         Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
  4.         Dim pidLabel As Label = listItem.FindControl("ProductId")
  5.  
  6.         Dim pid As Integer
  7.         Integer.TryParse(pidLabel.Text, pid)
  8.  
  9.         Dim qty As Integer
  10.         Integer.TryParse(txtBox.Text, qty)
  11.         'now you need to update your data source using the ProductId
  12.     End Sub
  13.  
(C#)
Expand|Select|Wrap|Line Numbers
  1.  protected void text_change(object sender, EventArgs e)
  2. {
  3.   TextBox txtBox = (TextBox)sender;
  4.   DataListItem listItem = (DataListItem)txtBox.BindingContainer;
  5.   Label pidLabel = (Label) listItem.FindControl("ProductId");
  6.   int pid;
  7.   int.TryParse(pidLabel.Text, out pid);
  8.   int qty;
  9.   int.TryParse(txtBox.Text, out qty);
  10.   //now you need to update your data source using the ProductId
  11. }


Here is a full working example.
(ASP.NET code to hook up to a VB.NET code behind)
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" Title="Products Page" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.  
  7. </head>
  8. <body>
  9.     <form id="form1" runat="server">
  10.       <asp:DataList ID="ProductDataList" RepeatColumns="3" RepeatDirection="Horizontal"
  11.     runat="server">
  12.         <ItemTemplate>
  13.           <div style="margin-top: 10px; width: 190px; height: auto; text-align: inherit">
  14.             <asp:Label ID="ProductId" runat="server" Text='<%# Eval("ProductId") %>' Style="display: none" />
  15.             <a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  16.                 <img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
  17.             </a>
  18.             <div style="width: 190px;">
  19.                 <a style="font-size: 11px; color: #068EA8; background-color: White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  20.                     <%# Eval("Product_Name") %>
  21.                 </a>
  22.                 <div>
  23.                     <span style="font-size: 12px; font-weight: bolder; color: #8B4513; background-color: White;">
  24.                         <strong>Price: </strong>
  25.                         <%# Eval("Price", "{0:c}")%>
  26.                     </span>
  27.                     <asp:TextBox ID="Txt_Quantity" Text='<%# Eval("Quantity") %>' OnTextChanged="text_change"
  28.                         AutoPostBack="true" runat="server" Style="margin: 0px,5px; width: 24px; height: 18px;"></asp:TextBox>
  29.                     <div>
  30.                         <strong>Cost:</strong>
  31.                         <%#Eval("Cost", "{0:c}")%>
  32.                     </div>
  33.                 </div>
  34.                 <div>
  35.                     <a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
  36.                         <img src='images/add_tocart.gif' alt="ADD TO CART" />
  37.                     </a>
  38.                     <br />
  39.                     <a style="font-size: 11px; color: #068EA8; background-color: White; text-decoration: underline"
  40.                         href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  41.                         Click for Details</a>
  42.                 </div>
  43.             </div>
  44.           </div>
  45.         </ItemTemplate>
  46.       </asp:DataList>
  47.     </form>
  48. </body>
  49. </html>
If you want the ASP.NET code to hook up to a C# code behind then use the following <%Page %> definition:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page  Language="C#" AutoEventWireup="true"
  2.     CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" Title="Products Page" %>
(VB.NET Page Code)
Expand|Select|Wrap|Line Numbers
  1. Partial Public Class _Default
  2.     Inherits System.Web.UI.Page
  3.     Private _products As List(Of Product)
  4.  
  5.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.         GenerateList()
  7.     End Sub
  8.  
  9.     Private Sub GenerateList()
  10.         If Session("_products") Is Nothing Then
  11.             _products = New List(Of Product)
  12.             For i As Integer = 0 To 9
  13.                 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))
  14.                 _products.Add(newProduct)
  15.             Next
  16.         Else
  17.             _products = DirectCast(Session("_products"), List(Of Product))
  18.         End If
  19.     End Sub
  20.  
  21.  
  22.     Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  23.         Session("_products") = _list
  24.         ProductDataList.DataSource = _list
  25.         ProductDataList.DataBind()
  26.     End Sub
  27.  
  28.     Protected Sub TxtChanged(ByVal sender As Object, ByVal e As EventArgs)
  29.         Dim txtBox As TextBox = TryCast(sender, TextBox)
  30.         Dim listItem As DataListItem = TryCast(txtBox.BindingContainer, DataListItem)
  31.         Dim pidLabel As Label = listItem.FindControl("ProductId")
  32.         Dim pid As Integer
  33.         Integer.TryParse(pidLabel.Text, pid)
  34.  
  35.         Dim p As Product = _list.Find(Function(item) item.ProductId = pid)
  36.         If p IsNot Nothing Then
  37.             Integer.TryParse(txtBox.Text, p.Qty)
  38.         End If
  39.     End Sub
  40. End Class
  41.  
(C# Page Code)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class _Default : System.Web.UI.Page
  11.     {
  12.         private List<Product> _products;
  13.         protected void Page_Load(object sender, EventArgs e)
  14.         {
  15.             if (IsPostBack == false)
  16.             {
  17.                 _products = new List<Product>();
  18.                 for (int i = 1; i < 11; i++)
  19.                 {
  20.                     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);
  21.                     _products.Add(newProduct);
  22.                 }
  23.  
  24.             }
  25.             else
  26.             {
  27.                 _products = (List<Product>)Session["_products"];
  28.             }
  29.         }
  30.  
  31.         protected void text_change(object sender, EventArgs e)
  32.         {
  33.             TextBox txtBox = (TextBox)sender;
  34.             DataListItem listItem = (DataListItem)txtBox.BindingContainer;
  35.             Label pidLabel = (Label) listItem.FindControl("ProductID");
  36.             int pid;
  37.             int.TryParse(pidLabel.Text, out pid);
  38.             Product p = _products.Find((ep) => ep.ProductId == pid);
  39.             if (p != null)
  40.             {   int qty;
  41.                 int.TryParse(txtBox.Text, out qty);
  42.                 p.Quantity = qty;
  43.             }
  44.  
  45.         }
  46.         protected void Page_PreRender(object sender, EventArgs e)
  47.         {
  48.             Session["_products"] = _products;
  49.             ProductDataList.DataSource = _products;
  50.             ProductDataList.DataBind();
  51.         }
  52.     }
  53.  
  54. }
  55.  
Product class used as data for the data source
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Public Class Product
  2.     Private _name As String
  3.     Private _id As Integer
  4.     Private _isbn As String
  5.     Private _qty As Integer
  6.     Private _price As Double
  7.  
  8.     Public Property Product_Name() As String
  9.         Get
  10.             Return _name
  11.         End Get
  12.         Set(ByVal value As String)
  13.             _name = value
  14.         End Set
  15.     End Property
  16.     Public Property ProductId() As Integer
  17.         Get
  18.             Return _id
  19.         End Get
  20.         Set(ByVal value As Integer)
  21.             _id = value
  22.         End Set
  23.     End Property
  24.     Public Property ISBN() As String
  25.         Get
  26.             Return _isbn
  27.         End Get
  28.         Set(ByVal value As String)
  29.             _isbn = value
  30.         End Set
  31.     End Property
  32.     Public Property Price() As Double
  33.         Get
  34.             Return _price
  35.         End Get
  36.         Set(ByVal value As Double)
  37.             _price = value
  38.         End Set
  39.     End Property
  40.     Public Property Quantity() As Integer
  41.         Get
  42.             Return _qty
  43.         End Get
  44.         Set(ByVal value As Integer)
  45.             _qty = value
  46.         End Set
  47.     End Property
  48.     Public ReadOnly Property Cost() As Double
  49.         Get
  50.             Return Price * Qty
  51.         End Get
  52.     End Property
  53.  
  54.     Public Sub New(ByVal name As String, ByVal id As Integer, ByVal price As Double)
  55.         _id = id
  56.         _name = name
  57.         _qty = 0
  58.         _price = price
  59.     End Sub
  60. End Class
(C#)
Expand|Select|Wrap|Line Numbers
  1.   public class Product
  2.     {
  3.         public string Product_Name { get; set; }
  4.         public int ProductId { get; set; }
  5.         public string ISBN { get; set; }
  6.         public double Quantity { get; set; }
  7.         public double Price { get; set; }
  8.         public  double Cost 
  9.         {
  10.             get { return this.Quantity * this.Price; } 
  11.         }
  12.         public string Product_Image { get; set; }
  13.  
  14.         public Product(string name, int id, string isbn, double price)
  15.         {
  16.             this.Quantity = 0;
  17.             this.Product_Name = name;
  18.             this.ProductId = id;
  19.             this.ISBN = isbn;
  20.             this.Price = price;
  21.         }
  22.     }
Aug 25 '11 #2
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.
Oct 3 '11 #3
Frinavale
9,735 Expert Mod 8TB
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
Oct 3 '11 #4
NeoPa
32,556 Expert Mod 16PB
@upcomingaman

Please see my PM (Private Message).
Oct 3 '11 #5
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
Oct 7 '11 #6
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 "

Expand|Select|Wrap|Line Numbers
  1. <asp:DataList ID="ProductDataList"   DataSourceID ="ProductDataSource"  RepeatColumns="3"  RepeatDirection="Horizontal" runat="server" >
  2.   <ItemTemplate>
  3.     <div >
  4.       <div style="margin-top:10px;width:190px;height:auto;text-align:inherit">
  5.         <a href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>'>
  6.           <img src='<%# Eval("Product_Image") %>' alt="product image" width="100px" height="120px" />
  7.         </a>
  8.       </div>
  9.       <div>
  10.         <div style="width:190px;height:50px" >
  11.           <a  style=" font-size:11px;color:#068EA8;background-color:White;" href='<%# Page.ResolveUrl("Productdetails.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId")) %>' >
  12.         <%# Eval("Product_Name") %>
  13.           </a>
  14.           <div>
  15.             <span style="font-size:12px;font-weight:bolder;color:#8B4513;background-color:White;" >
  16.               <strong>Price: </strong>
  17.             <%# Eval("Price", "{0:c}")%>
  18.             </span>
  19.           </div>
  20.  
  21.           <div>
  22.             <table style="margin-left:70px;" >
  23.               <tr>
  24.                 <td >      
  25.                   <asp:TextBox ID="TextBox1"  Text="1"  OnTextChanged="text_change"  AutoPostBack="true" Width="14px" Height="18px" runat="server"></asp:TextBox>
  26.                 </td>
  27.                 <td style="width:5px;">
  28.                 </td>
  29.                 <td>
  30.                   <a href='<%# Page.ResolveUrl("basket.aspx?ISBNNumber="+ Eval("ISBN")+"&ProductID="+Eval("ProductId"))%>'>
  31.                     <img  src='images/add_tocart.gif' alt="ADD TO CART"   />
  32.                   </a>
  33.                 </td>
  34.               </tr>
  35.             </table>
  36.           </div>
  37.           <div>
  38.             <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>
  39.           </div>
  40.         </div>
  41.  
  42.   </ItemTemplate>
  43. </asp:DataList>
  44.  
  45.  




Expand|Select|Wrap|Line Numbers
  1. protected void text_change(object sender, EventArgs e)
  2. {
  3.   string noofbooks = Server.HtmlEncode(((TextBox)ProductDataList.Items[Product  DataList.SelectedIndex].FindControl("TextBox1")).Text);
  4. }



Please provide me some help
Thanks in adv
Oct 28 '11 #7
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. protected void text_change(object sender, EventArgs e)
  2. {
  3.   TextBox txtBox = (TextBox)sender;
  4.   string noofbooks = txtBox.Text;
  5. }

-Frinny
Oct 28 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
0
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">
0
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...
6
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!:)...
1
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...
4
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...
3
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...
0
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) {...
6
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...
2
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...
0
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.