Bobby X
asked on
Need help on ObjectDataSource control...
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!
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.