Solved

Datalist textbox value into a session array

Posted on 2008-10-27
5
1,337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net/VSTO Excel Add-in 2 38
How we can add Dten600.dll in GAC in c#. 18 55
asp.net repeater server posting 5 38
Error in Visual Project 10 48
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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