Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
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 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 April 3rd, 2006, 12:25 PM
Registered User
 
Join Date: Apr 2006
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................
 
Old April 3rd, 2006, 02:35 PM
Friend of Wrox
 
Join Date: Nov 2003
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.

 
Old April 3rd, 2006, 03:58 PM
Registered User
 
Join Date: Apr 2006
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 ?





 
Old April 3rd, 2006, 10:07 PM
Friend of Wrox
 
Join Date: Nov 2003
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.
 
Old April 4th, 2006, 02:58 AM
Registered User
 
Join Date: Apr 2006
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......
 
Old April 4th, 2006, 09:01 AM
Friend of Wrox
 
Join Date: Nov 2003
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







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 07: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 06:02 PM





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