Solved

Datalist textbox value into a session array

Posted on 2008-10-27
5
1,333 Views
Last Modified: 2013-12-17
Hello experts!  

I have an asp.net Datalist control in which I have textboxes.  I would to add the user's input from these texboxes along with the primary key value of the item.  Each datalist item will have a button that will "add" the user input text and the datakey value.  I added the code I'm trying below but when I retrieve the values I only get 0 and 0.  What would be the best way to do this?   Thanks in advance!
ShoppingCart cart = new ShoppingCart();

        cart.ProjectID = Convert.ToInt32(DataList1.Items[0].FindControl("IDLabel.Text"));

        cart.Quantity = Convert.ToInt32(DataList1.Items[0].FindControl("QuantityTextBox.Text"));
 

        Session["Order"] = cart;

Open in new window

0
Comment
Question by:deloused
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 22818384
Hi,
I believe you have the .Textt in the worng place. Try placing it outside the quotes like so:
cart.ProjectID = Convert.ToInt32(DataList1.Items[0].FindControl("IDLabel").Text);
cart.Quantity = Convert.ToInt32(DataList1.Items[0].FindControl("QuantityTextBox").Text);

        Session["Order"] = cart;

0
 

Author Comment

by:deloused
ID: 22818846
When I place it outside of the quotes I get the error:   'System.Web.UI.Control' does not contain a definition for 'Text'.  I think you're right, it's a syntax thing but I just can't seem to get it correct.  
0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 300 total points
ID: 22818895
Hi,
I think you might need to DirectCast it to a textbox or something. so it'll need to be done in 2 steps. One convert the Control to something and then convert the text to int32.
0
 

Author Comment

by:deloused
ID: 22819192
Ok, I think I'm a little closer but I'm still fighting some syntax problems.

Here's what I have so far:
The ID kind of works, but it is always the same value, doesn't matter what datlist button I pick.  The textbox always throws the error:  "Input string was not in a correct format."  

I'm not sure what I'm doing wrong.


Label proj_ID = (Label)DataList1.Items[0].FindControl("IDLabel");

TextBox proj_Qty = (TextBox)DataList1.Items[0].FindControl("OrderQuantityTextBox"); 
 

cart.ProjectID = Convert.ToInt16(proj_ID.Text);

cart.Quantity = Convert.ToInt16(proj_Qty.Text);

Open in new window

0
 

Author Comment

by:deloused
ID: 22834732
This is what I ended up getting to work:

protected void BuyNowButton_Click(object sender, EventArgs e)
    {
        Button BuyNowButton = (Button)sender;
        DataListItem item = (DataListItem)BuyNowButton.NamingContainer;
        Label ID_Label = (Label)item.FindControl("IDLabel");
        TextBox OrderQuantity_Textbox = (TextBox)item.FindControl("OrderQuantityTextBox");
        ResultsLabel.Text += String.Format("Project: {0}, Quantity: {1}<br/>", ID_Label.Text, OrderQuantity_Textbox.Text);

        ShoppingCart cart = new ShoppingCart();
        cart.ProjectID = Convert.ToInt32(ID_Label.Text);
        cart.Quantity = Convert.ToInt32(OrderQuantity_Textbox.Text);
    }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now