Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2004, 03:37 PM
Authorized User
 
Join Date: Jul 2004
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.


 
Old July 11th, 2004, 08:02 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old July 12th, 2004, 02:04 PM
Authorized User
 
Join Date: Jul 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Peter. I got the stuff working.
 
Old July 22nd, 2004, 01:27 AM
Friend of Wrox
 
Join Date: Jun 2004
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.

 
Old July 22nd, 2004, 02:35 AM
Friend of Wrox
 
Join Date: Aug 2003
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

 
Old July 22nd, 2004, 02:42 AM
Friend of Wrox
 
Join Date: Aug 2003
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")

 
Old July 22nd, 2004, 03:59 AM
Friend of Wrox
 
Join Date: Jun 2004
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.

 
Old July 22nd, 2004, 05:10 AM
Authorized User
 
Join Date: Mar 2004
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)

 
Old July 23rd, 2004, 02:27 AM
Friend of Wrox
 
Join Date: Aug 2003
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...?)

 
Old December 24th, 2004, 06:18 AM
Registered User
 
Join Date: Dec 2004
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.