473,431 Members | 1,822 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,431 software developers and data experts.

DataGrid ItemTemplate naming questions


Hi,

I have a DataGrid that contains an ItemTemplate. Within the
ItemTemplate I have a DropDownList. The relevant code looks like this:

<asp:datagrid id="DataGrid1" runat="server">
................
<Columns>
<asp:TemplateColumn HeaderText="Division">
<ItemTemplate>
<asp:DropDownList runat="server" id="Dropdownlist1"
DataTextField="Name" DataValueField="DepartmentID" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
..............................

When the page runs my DropDownList morphs into an html <select> tag, as I'd
expect. However the ID of the tag is a little strange: what happens is the
DataGrid prepends some information to it, so that instead of <select
ID='Dropdownlist1'..." I get "<select
ID='DataGrid1:_ctl2:Dropdownlist1'...."

I understand why the DataGrid does this --not good having a batch <select>
tags on the page with the same ID-- but I'm worried about how consistent the
names will be. For instance, if I add additional ItemTemplates (with other
controls) to this DataGrid will my existing DropDownList IDs change? I read
these <select> values on postback, using the Request object, and if their
IDs change unexpectedly I'll be hurting....I just want to see what my risks
are. Thanks for your advice.

-Jim
Nov 19 '05 #1
3 1197
Hi Jim:

You'll have to code assuming they will change.

Instead of using the Request object, you'l find some of the techniques
in these articles a little more robust:

DropDownList Controls In an ASP.Net DataGrid
http://odetocode.com/Articles/231.aspx

In Search Of ASP.Net Controls
http://odetocode.com/Articles/116.aspx

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

On Wed, 29 Dec 2004 11:26:55 -0800, "Jim Bancroft"
<as******@nowhere.com> wrote:

Hi,

I have a DataGrid that contains an ItemTemplate. Within the
ItemTemplate I have a DropDownList. The relevant code looks like this:

<asp:datagrid id="DataGrid1" runat="server">
................
<Columns>
<asp:TemplateColumn HeaderText="Division">
<ItemTemplate>
<asp:DropDownList runat="server" id="Dropdownlist1"
DataTextField="Name" DataValueField="DepartmentID" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
.............................

When the page runs my DropDownList morphs into an html <select> tag, as I'd
expect. However the ID of the tag is a little strange: what happens is the
DataGrid prepends some information to it, so that instead of <select
ID='Dropdownlist1'..." I get "<select
ID='DataGrid1:_ctl2:Dropdownlist1'...."

I understand why the DataGrid does this --not good having a batch <select>
tags on the page with the same ID-- but I'm worried about how consistent the
names will be. For instance, if I add additional ItemTemplates (with other
controls) to this DataGrid will my existing DropDownList IDs change? I read
these <select> values on postback, using the Request object, and if their
IDs change unexpectedly I'll be hurting....I just want to see what my risks
are. Thanks for your advice.

-Jim


Nov 19 '05 #2

Hey, thanks for those links Scott-- very handy.

I think what I'm trying to do is discussed at the bottom of your second
article: I have a submit button at the bottom of my page, unattached to my
DataGrid. After clicking and submitting the page I need to loop through the
DataGrid's rows and extract the DropDown values. Can I do something like
this?

private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem item in DataGrid1.Items)
{
DropDownList ddl = (DropDownList) item.FindControl("Dropdownlist1");
//do something here
}
}

Does this sound like an okay plan, or is there a better way?


"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:pb********************************@4ax.com...
Hi Jim:

You'll have to code assuming they will change.

Instead of using the Request object, you'l find some of the techniques
in these articles a little more robust:

DropDownList Controls In an ASP.Net DataGrid
http://odetocode.com/Articles/231.aspx

In Search Of ASP.Net Controls
http://odetocode.com/Articles/116.aspx

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

On Wed, 29 Dec 2004 11:26:55 -0800, "Jim Bancroft"
<as******@nowhere.com> wrote:

Hi,

I have a DataGrid that contains an ItemTemplate. Within the
ItemTemplate I have a DropDownList. The relevant code looks like this:

<asp:datagrid id="DataGrid1" runat="server">
................
<Columns>
<asp:TemplateColumn HeaderText="Division">
<ItemTemplate>
<asp:DropDownList runat="server" id="Dropdownlist1"
DataTextField="Name" DataValueField="DepartmentID" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
.............................

When the page runs my DropDownList morphs into an html <select> tag, as
I'd
expect. However the ID of the tag is a little strange: what happens is
the
DataGrid prepends some information to it, so that instead of <select
ID='Dropdownlist1'..." I get "<select
ID='DataGrid1:_ctl2:Dropdownlist1'...."

I understand why the DataGrid does this --not good having a batch <select>
tags on the page with the same ID-- but I'm worried about how consistent
the
names will be. For instance, if I add additional ItemTemplates (with
other
controls) to this DataGrid will my existing DropDownList IDs change? I
read
these <select> values on postback, using the Request object, and if their
IDs change unexpectedly I'll be hurting....I just want to see what my
risks
are. Thanks for your advice.

-Jim

Nov 19 '05 #3
That looks like a good approach, Jim. With this approach there is no
need to worry about what the ClientID happens to be, the code just
needs the Control ID which you have complete control over.

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

On Wed, 29 Dec 2004 14:49:13 -0800, "Jim Bancroft"
<as******@nowhere.com> wrote:

Hey, thanks for those links Scott-- very handy.

I think what I'm trying to do is discussed at the bottom of your second
article: I have a submit button at the bottom of my page, unattached to my
DataGrid. After clicking and submitting the page I need to loop through the
DataGrid's rows and extract the DropDown values. Can I do something like
this?

private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem item in DataGrid1.Items)
{
DropDownList ddl = (DropDownList) item.FindControl("Dropdownlist1");
//do something here
}
}

Does this sound like an okay plan, or is there a better way?


Nov 19 '05 #4

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

Similar topics

1
by: Juba | last post by:
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...
2
by: Celine | last post by:
I am building a website using C#, asp.net and SqlServer. I need to store a lot of images in my database. I need to store thumbnails but also larger pictures(when user clicks thumbnails a larger...
1
by: aylin_sk | last post by:
I'm trying to list my data into my datagrid creating stored procedures in oracle db..My data includes questions for a survey....It's simple but i cant see my datagrid in my web form..I'm now going...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
4
by: Jim Heavey | last post by:
Hello, I am starting to learn how to use the Datagrid and I have a couple of questions. My datagrid as a checkbox in it. It looks like the following in the datagrid... <asp:TemplateColumn...
2
by: localhost | last post by:
When I go into Edit mode on a datagrid, I would like to add a single column to the edited row and display a hyperlink. If I use the code below, I get a delete linkbutton in every row, when I only...
3
by: Raja | last post by:
I have a datagrid, it has dropdown box as a column and i have one more column that has hyperlink. The NavigateURL for the hyperlink is to open a new window with a query stirng parameter as the...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
1
by: Steven Van Eyck | last post by:
Hi there There are probably lots of questions being posted about datagrids, but I can't resist... ;-) Here's my issue: I have an aspx page with a datagrid in which I manually declare the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.