473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting a Value From a Datagrid

Hello,

I have the following code placed at my Page_Load event and after the
Datagrid is binded, so it is not empty. The problem is that the variable
isclass is always returning null and I am sure that the Category
ItemTemplate is there and that it is previouly populated. Am I missing
something? Here's the code:

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack == false)
{

// Calculate end-user's shopping cart ID
ShoppingCartDB cart = new ShoppingCartDB();
String cartId = cart.GetShoppingCartId();

// Populate datagrid with shopping cart data
MyDataGrid.DataSource = cart.GetItems(cartId);
MyDataGrid.DataBind();

// I am trying to get the value from my ItemTemplate TextBox here:
TextBox isclass = (TextBox)MyDataGrid.FindControl("Category");

if (isclass.Text == "Classes" ) // I am sure this value is there - it is
not finding the control
{
cart.AddItem(cartId, 2, 1);
}

}
}
.....

Thanks in advance!
Nov 15 '05 #1
1 2236
Hi Juba:

If you are using an ItemTemplate in your grid, something like

<ItemTemplate>
<asp:TextBox Runat="server" id="mytextbox">x</asp:TextBox>
</ItemTemplate>

then click View Source in IE, you'll see ASP.NET prefixed the ID of
each textbox, something like:

<input name="DataGrid1:_ctl4:mytextbox" type="text" value="ABBA"
id="DataGrid1__ctl4_mytextbox" />

Basically, child controls receive a naming prefix from ASP.NET so it
will not render multiple TextBox controls all with the ID of
"Category".

What I have trouble understanding is this. If you have a DataGrid with
multiple rows, how do you know which one to pull data from? If you
have a "Selected" row , you can use FindControl on the parent of the
text box to find a value, i.e:

textbox = (TextBox)MyDataGrid.SelectedItem.FindControl("Cate gory");

Using FindControl on the DataGrid will search with the proper prefix
for the control in question.

Hope this gets you started in the right direction,

--
Scott
http://www.OdeToCode.com

On Fri, 2 Jan 2004 23:05:48 -0500, "Juba" <ju*******@hotmail.com>
wrote:
Hello,

I have the following code placed at my Page_Load event and after the
Datagrid is binded, so it is not empty. The problem is that the variable
isclass is always returning null and I am sure that the Category
ItemTemplate is there and that it is previouly populated. Am I missing
something? Here's the code:

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack == false)
{

// Calculate end-user's shopping cart ID
ShoppingCartDB cart = new ShoppingCartDB();
String cartId = cart.GetShoppingCartId();

// Populate datagrid with shopping cart data
MyDataGrid.DataSource = cart.GetItems(cartId);
MyDataGrid.DataBind();

// I am trying to get the value from my ItemTemplate TextBox here:
TextBox isclass = (TextBox)MyDataGrid.FindControl("Category");

if (isclass.Text == "Classes" ) // I am sure this value is there - it is
not finding the control
{
cart.AddItem(cartId, 2, 1);
}

}
}
....

Thanks in advance!


Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Red Green | last post by:
I have a form with a datagrid and a dataset bound to it. I want to launch a second form and pass it a value from whichever record the user has selected. I can't find an example anywhere that shows...
1
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I have a problem with capturing the changed value of a text box in a datagrid. The datagrid is populated based on a dataset and I am using template...
4
by: kscdavefl | last post by:
I ahve a datagrid on a web form. I need to change the value in column 3 as follows. If the value in column 3 reads 0, I want to change it to read YES. How can I accomplish this task. ...
3
by: ALPO | last post by:
We have a datagrid built using template columns. Many are text boxes. These text boxes are bound to a datasource as follows: <ItemTemplate> <asp:TextBox id="txtPeriod1" runat="server"...
6
by: Teep | last post by:
Below is my code for dropdownlist that is populated from a SQL table. After a selection from the ddl, a datagrid is suppose to come up pertaining to the information selected, but I keep getting a...
5
by: Jason | last post by:
I've been trying to figure out a good way to do this but haven't had much luck, any input would be greatly appreciated. Basically, after a datagrid is sorted, how can I get the primary key value...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
2
by: niceguy | last post by:
Hi I was wondering if its possible to get the exact displayed text from a cell in a datagrid. I need to get the displayed text and not any 'real' database value or anything that the cell...
1
by: g.o.atkins | last post by:
Hi, I've bound an ArrayList to a datagrid and succesfully managed to display the data via an ItemTemplate column and using <%# Container.DataItem %>. The problem I have however is trying to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.