Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Datalist textbox value into a session array

Posted on 2008-10-27
5
Medium Priority
?
1,356 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 900 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

972 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