Solved

Datalist textbox value into a session array

Posted on 2008-10-27
5
1,332 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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