Solved

Datalist textbox value into a session array

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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