Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 11th, 2004, 03:37 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default dropdownlist inside datagrid

Hello,

I am trying to include dropdown list in one of the columns in a datagrid and it will be displayed in every row not the edit command row. I don't have a edit command column type. All columns in datagrid are either "boundcolumn" or "templatecolumn". Here is the code I am trying.

<asp:templatecolumn headertext="Region">
<headerstyle wrap="True" width="50"></headerstyle>
<itemstyle cssclass="griditem"></itemstyle>
<itemtemplate>
    <asp:dropdownlist id="drpRegion" height="18px" width="100px" runat="server" autopostback="False">
            <asp:listitem value="1">Region1</asp:listitem>
            <asp:listitem value="2">Region2</asp:listitem>
        <asp:listitem value="3">Region3</asp:listitem>
        <asp:listitem value="4">Region4</asp:listitem>
        <asp:listitem value="5">Region5</asp:listitem>
    </asp:dropdownlist>
</itemtemplate>
</asp:templatecolumn>

In this code I need to select the one of the item in the dropdownlist based on the data returned from the database. ASP.NET doesn't allow to add databind code inside the <asp:listitem> to set the selected=True property. I cannot think of any other way. Any help will be appreciated.

Thanks,
Yunus.


Reply With Quote
  #2 (permalink)  
Old July 11th, 2004, 08:02 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

In the code behind for the grid, you need to tie into the grid's ItemDataBound event. This event fires for every Item that is databound in the grid. Within the handler for the event, you can access the data item of the current item being bound. This is done thru e which is the event's eventargs object. e.Item will give you the datagrid item itself. Off that you can access the actual data item that the grid item is being bound to: e.Item.DataItem. This will return the data item (object, datarow, etc). You'll need to convert that to the applicable data type and you can than access whatever you need, such as the column for Region.

Then you'll need to use the DataGridItem's "FindControl()" method to find the DDL control that you put in the template. Once you have the DDL control you can set its selected item in the normal way.

Keep in mind that you have to do some testing in this event handler because ALL items of the datagrid are bound: Items, AlternateItems, Header, Footer, etc. You need to test e.Item.ItemType for only the item types you want (Item, AlternateItem) because the DDL control (and the Region column value for that matter) won't exist in the other items.

Peter
-------------------------
Work smarter, not harder
Reply With Quote
  #3 (permalink)  
Old July 12th, 2004, 02:04 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Peter. I got the stuff working.
Reply With Quote
  #4 (permalink)  
Old July 22nd, 2004, 01:27 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Hyderabad, , India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How can i redirect to other page after selection of the index from the dropdown list.How to use selection index changed event in the dropdown list which is inside the datagrid.

Reply With Quote
  #5 (permalink)  
Old July 22nd, 2004, 02:35 AM
Friend of Wrox
 
Join Date: Aug 2003
Location: , , .
Posts: 108
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to life_s Ng Send a message via MSN to life_s Ng
Default

Hi Lily,

have u tried out the usual way to run the ddl selected index changed event? Remember set the AutopostBack to true for the ddl. I never tried that before n now, im using others com... cant really tried it out for u. kindly let me know the out come k?

Hope this help
life's Ng

Reply With Quote
  #6 (permalink)  
Old July 22nd, 2004, 02:42 AM
Friend of Wrox
 
Join Date: Aug 2003
Location: , , .
Posts: 108
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to life_s Ng Send a message via MSN to life_s Ng
Default

oops, left something..
put this in ur ddl event

response.redirect("urpage.aspx")

Reply With Quote
  #7 (permalink)  
Old July 22nd, 2004, 03:59 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Hyderabad, , India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
If there are 6 items in the dropdown list and with each item selection i want to redierect to different page, then i cant write in ddl's event. With each selection this will go to different page.How to do this one.

Reply With Quote
  #8 (permalink)  
Old July 22nd, 2004, 05:10 AM
Authorized User
 
Join Date: Mar 2004
Location: Rawang, Selangor, Malaysia.
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Lily,
ddl has 2 type of value that we can use. the 'selecteditem.text' and the 'selecteditem.value'. selecteditem.text used to display the message to user. and selecteditem.value is used in your function. so, why don't you just use this in your rendering function?

.....blah blah blah
Response.Redirect(ddl.selecteditem.value)
.....

where your ddl.selecteditem.value contains the webpage url (sample.aspx)

Reply With Quote
  #9 (permalink)  
Old July 23rd, 2004, 02:27 AM
Friend of Wrox
 
Join Date: Aug 2003
Location: , , .
Posts: 108
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to life_s Ng Send a message via MSN to life_s Ng
Default

"With each selection this will go to different page.How to do this one"

use "if" or "case" statement

if ddl.selecteditem.text="abc"
   'do this
   'direct to A page
else if ddl.selecteditem.text="def"
   'do this
   'direct to B page
.
.
.
.
end if

Write the codes above in ur ddl's event.

hope this help

Life's Ng
(where do u stay...? r u mm...?)

Reply With Quote
  #10 (permalink)  
Old December 24th, 2004, 06:18 AM
Registered User
 
Join Date: Dec 2004
Location: hyderabad, andhra pradesh, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to suresh_kasilanka
Default

hi,
   hello every body i what to know the how to give the datasource to dropdown list in datagrid.

regards,
suresh.


Suresh
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList Inside a datagrid @shish ASP.NET 1.0 and 1.1 Professional 3 January 29th, 2007 07:24 PM
DropDownList inside a GridView aidoco ASP.NET 2.0 Basics 0 October 13th, 2006 04:24 AM
catch event of dropdownlist inside datagrid alyeng2000 ASP.NET 1.0 and 1.1 Professional 3 August 24th, 2004 10:44 AM
dropdownlist inside datagrid shaileshmark General .NET 5 June 27th, 2004 05:18 AM



All times are GMT -4. The time now is 09:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.