[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need help on ObjectDataSource control...

Posted on 2009-06-29
1
Medium Priority
?
221 Views
Last Modified: 2012-05-07
Hi,
Is there a way to programmatically call the ObjectDataSource control based on certain selection made in a dropdownlist? Please see code below. When the option with value "kickass" is selected from dropdownlist "ddlMovieCategory", I want to display another dropdownlist for user to select.  All other options will just call the ObjectDataSource (id="srcMovies").  Please provide sample code.

Thanks very much in advance!

Web Form Code:
    <form id="form1" runat="server">
	<div>
    Please select a movie category: 
    <asp:DropDownList 
    ID="ddlMovieCategory" 
    DataTextField="Name" 
    DataValueField="Id" 
    AutoPostBack="true" 
    OnSelectedIndexChanged="ddlMovieCategory_SelectedIndexChanged"
    Runat="server" /> 
 
    <asp:DataList 
        ID="dlMovies" CssClass="dataView" 
        DataSourceID="srcMovies"
        Runat="server">
        <ItemTemplate>
        <h4><%#Eval("MovieTitle")%></h4>
        <%#Eval("MovieDesc")%><br />
		<%#Eval("MoveRating")%>
        </ItemTemplate>
    </asp:DataList>        
        
    <asp:ObjectDataSource
        id="srcMovies"
        TypeName="MovieCategories"
        SelectMethod="GetMovies"
        Runat="server">
        <SelectParameters>
        <asp:ControlParameter
            Name="CategoryId"
            ControlID="ddlMovieCategory" />
       </SelectParameters>
    </asp:ObjectDataSource>
    </div>
    </form>
 
Code-Behind:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Call to the function to populate dropdownlist from xml
            PopulateDDLFromXMLFile();
        }
    }
	
    // Populates the dropdownlist from xml file
    private void PopulateDDLFromXMLFile()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(MapPath("MovieCategories.xml"));
 
        // Get the dataview of table "Category", which is default table name
        DataView dv = ds.Tables["Category"].DefaultView;
        // Or we can use:
        // DataView dv = ds.Tables[0].DefaultView;
 
        // Now sort the DataView vy column name "Name"
        dv.Sort = "Name";
 
        // Now define datatext field and datavalue field of dropdownlist
        ddlMovieCategory.DataTextField = "Name";
        ddlMovieCategory.DataValueField = "ID";
 
        // Now bind the dropdownlist to the dataview
        ddlMovieCategory.DataSource = dv;
        ddlMovieCategory.DataBind();
    }
    protected void ddlMovieCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        //do something here
    }
 
XML File That Populates Dropdown:
<?xml version="1.0" encoding="utf-8" ?>
<Categories>
  <Category>
    <ID></ID>
    <Name></Name>
  </Category>
  <Category>
    <ID>love</ID>
    <Name>Romance</Name>
  </Category>  
  <Category>
    <ID>kickass</ID>
    <Name>Action</Name>
  </Category>
  <Category>
    <ID>adventure</ID>
    <Name>Adventure</Name>
  </Category>
  <Category>
    <ID>drama</ID>
    <Name>Drama</Name>
  </Category>
  <Category>
    <ID>horror</ID>
    <Name>Horror</Name>
  </Category>
  <Category>
    <ID>scary</ID>
    <Name>Thriller</Name>
  </Category>
</Categories>

Open in new window

0
Comment
Question by:WebAppDeveloper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 2000 total points
ID: 24742779
Hi WebAppDeveloper,
>>..programmatically call the ObjectDataSource control based on certain selection made in a dropdownlist
You can call ObjectDataSource's onselected event to handle the required condition as needed.
eg:
Check the details here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.selected.aspx

<asp:ObjectDataSource
        id="srcMovies"
       OnSelected="ObjectDataSource1_Selected"
    ....>

public void ObjectDataSource1_Selected(object source, ObjectDataSourceStatusEventArgs e)
{
    if (e.ReturnValue != null)
    {
        //continue your logic here, probably re-bind a resource to another dropdownlist based on selected...
    }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question