Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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 Display Modes
  #1 (permalink)  
Old April 3rd, 2006, 01:25 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Binding DropDownList Dynamically

Hi there.
It's a simple procedure hovewer i couldnt accomplish it.
I would like to fill a drop down list dynamically from a data table. The metter is that, i want to do that in code-behind part.

Actually I'm filling the drop down list and binding it, hovewer it seems empty at web page.

I know a solution like adding every row's values of data table to drop down list with Item.Add method in a loop, but i didn't like it. Is there any alternative way ? (C#)
(Also i did it with reader too, but looking for solution related with datatabel)

Thanks for any consideration................
Reply With Quote
  #2 (permalink)  
Old April 3rd, 2006, 03:35 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

if you want to bind to a datatabble then you have to do it in the codebehind
otherwiese you will have to add listitems in a loop as you say.

I don't know why you are opposed to either method, but those are your choices unless you hard code the values.

Reply With Quote
  #3 (permalink)  
Old April 3rd, 2006, 04:58 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In fact, the situation is a bit complicated, at least for me.

I have a GridView and it takes its data from two tables. In the two of it's EditItemTeplates, I put DropDownLists. Second DDL data changes related to first DDL's selected item.

When the EditCommand(for GridView) event fired, both of them are filled properly, but when i change first DDL's selcted value VS gives an error that is 'there is no data' or something like that for second DDL.

Instead of using DropDownList taken from ToolBox and adding a datasource, I have tried to create a DropDownList in codebehinde part with datatable bounded out of gridview.

I made a search on Internet for such case, i didnt find a good example.

Do you have any suggesion for that situation ?





Reply With Quote
  #4 (permalink)  
Old April 3rd, 2006, 11:07 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What is the exact error?  Is it object not set to an instance of an object?

What is the code you have so far? It's a little complicated to do what you want, but not impossible.  I have done it before.  

Get the exact error and show the code behind you have so far.
Reply With Quote
  #5 (permalink)  
Old April 4th, 2006, 03:58 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
 <EditItemTemplate>
   <asp:DropDownList ID="editMarkaDdl" runat="server" DataSourceID="editMarkaDs" 
     DataTextField="marka" DataValueField="markaId" SelectedValue='<%# Bind("markaId") %>' AutoPostBack="true"  OnSelectedIndexChanged="editMarkaDdl_SelectedIndexChanged">
  </asp:DropDownList>

<asp:AccessDataSource ID="editMarkaDs" runat="server" DataFile="~/App_Data/vehiclesDb.mdb"
SelectCommand="SELECT [marka], [markaId] FROM [markaTbl] WHERE ([vehicleTypeId] = ?)">
<SelectParameters>
  <asp:ControlParameter ControlID="vehicleTypeDdl" Name="vehicleTypeId"   PropertyName="SelectedValue" Type="Int32" />

/*vehicleTypeDdl is another DDL on the page (out of the gridview), there is no trouble whit it */

</SelectParameters>
</asp:AccessDataSource>
</EditItemTemplate>

<ItemTemplate> 
<asp:Label ID="Label1" runat="server" Text='<%# Eval("marka") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
                           
<asp:TemplateField HeaderText="MODEL" SortExpression="model">
   <EditItemTemplate>
<asp:DropDownList ID="editModelDdl" runat="server" DataSourceID="editModelDs" DataTextField="model" DataValueField="model" SelectedValue='<%#Bind("model") %>'>
</asp:DropDownList>

<asp:AccessDataSource ID="editModelDs" runat="server" DataFile="~/App_Data/vehiclesDb.mdb"
SelectCommand="SELECT DISTINCT [model] FROM [modelTbl] WHERE ([markaId] = ?)">
<SelectParameters>
 <asp:ControlParameter ControlID="editMarkaDdl" Name="markaId" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:AccessDataSource>
</EditItemTemplate>
Here is the ASP code.
When I hit edit button of girdview, editMarkaDdl and editModelDdl fill properly. When I change the selected value of editMArkaDdl, it says "There is no source code available at the current location".

After opening the Disassembly there another error InvalidOperationException --> Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

I know I must take care of editMarkaDdl_SelectedIndexChanged but I don't know how.
  
I mean, at the beginnig of editcommand of gridview, it binds two DDL's properly, then it fails.  

By the way, jbenson thanks for consideration......
Reply With Quote
  #6 (permalink)  
Old April 4th, 2006, 10:01 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

The problem is that editMarkaDd1 is in a template column. You must have added the control parameter manually because it won't see a template item. You have to use the row databound event to get a reference to the ddl. Something like:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim ddl As New DropDownList
            ddl = e.Row.Cells(cell index).FindControl("editMarkaDdl")

        End If
    End Sub


Reply With Quote
Reply


Thread Tools
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
Re-Binding A Dropdownlist In A DetailsView cyber0ne BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 March 26th, 2007 08:10 PM
Binding dropdownlist to gridView mcgarry101 ASP.NET 2.0 Basics 0 February 13th, 2007 07:12 AM
Newbie Q - DropDownList Binding spuckle ASP.NET 2.0 Basics 1 March 2nd, 2006 12:23 AM
Binding MS Access data to DropDownList aleahy Classic ASP Basics 1 April 25th, 2005 07:02 PM



All times are GMT -4. The time now is 04:33 AM.


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