|
-
Jan 19th, 2005, 08:08 AM
#1
Thread Starter
Frenzied Member
Fire DropDownList event in a repeater *RESOLVED*
I have two dropdownlists within a repeater (simplified code below):
Code:
<asp:Repeater id="rptProducts" Runat="Server">
<ItemTemplate>
<asp:DropDownList id="ddlColour" Runat="Server" DataSource="<%# BindColour((int)DataBinder.Eval(Container.DataItem, "ProductID")) %>" DataTextField="ColourName" DataValueField="ColourID" />
<asp:DropDownList id="ddlSize" Runat="Server" DataSource="<%# BindSize((int)DataBinder.Eval(Container.DataItem, "ProductID")) %>" DataTextField="SizeName" DataValueField="SizeID" />
</ItemTemplate>
</asp:Repeater>
The repeater is bound to a list of products. The BindColour and BindSize methods return lists according to the productID for that Item.
My query is how can I detect that the first dropdownlist (colour) has changed value and therefore databind the second dropdownlist again to reflect the selected value.
Basically the first list contains all colours for a particular product and the second dropdown displays sizes for a product AND colour.
Help would be appreciated.
DJ
Last edited by dj4uk; Jan 21st, 2005 at 05:50 AM.
Reason: Post resolved
-
Jan 21st, 2005, 05:49 AM
#2
Thread Starter
Frenzied Member
Re: Fire DropDownList event in a repeater
Sorted it!
I do the first databind within the repeater event ItemDataBound.
When one of the first dropdownlists within the repeater itemtemplate changes its selected index it fires the dropdownlists SelectedIndexChanged event. The difficulty I had was accessing the other dropdownlist within the repeater item so I can databind it according to the new selected index of the first dropdownlist.
In the end I used the Parent property of the sender argument to access the repeater item and then used the FindControls method to grab the second dropdownlist.
Code:
void rptRelatedProducts_ItemDataBound(Object sender, RepeaterItemEventArgs e) {
Label productID;
DropDownList colourList;
DropDownList sizeList;
int colourID;
// Switch to work out which item is being data bound
switch (e.Item.ItemType) {
case ListItemType.Item:
case ListItemType.AlternatingItem:
// If its an Item then databind the two dropdownlists
// lblProductID is a hidden label holding the productID
productID = (Label) e.Item.FindControl("lblProductID");
colourList = (DropDownList) e.Item.FindControl("ddlColour");
sizeList = (DropDownList) e.Item.FindControl("ddlSize");
// DataBind the colour list
colourList.DataSource = AvailableColours(Convert.ToInt32(productID.Text));
colourList.DataBind();
// Grab the current selected colourID if one isn't selected grab
// the first on the list
if (colourList.SelectedValue == String.Empty) {
if (colourList.Items.Count > 0) {
colourList.SelectedIndex = 0;
colourID = Convert.ToInt32(colourList.SelectedValue);
} else {
colourID = 0;
}
} else {
colourID = Convert.ToInt32(colourList.SelectedValue);
}
// DataBind the size list
sizeList.DataSource = AvailableSizes(Convert.ToInt32(productID.Text), colourID);
sizeList.DataBind();
// Select the first on the list
if (sizeList.SelectedValue == String.Empty) {
if (sizeList.Items.Count > 0) {
sizeList.SelectedIndex = 0;
}
}
break;
case ListItemType.Header:
break;
case ListItemType.Footer:
break;
case ListItemType.Separator:
break;
}
}
void ddlColour_SelectedIndexChanged(Object sender, EventArgs e) {
Label productID;
DropDownList colourList;
DropDownList sizeList;
int sizeID;
// Load the controls needs
colourList = (DropDownList) sender;
productID = (Label) colourList.Parent.FindControl("lblProductID");
sizeList = (DropDownList) colourList.Parent.FindControl("ddlSize");
// Grab any already selected size value
if (sizeList.SelectedValue == String.Empty) {
if (sizeList.Items.Count > 0) {
sizeID = Convert.ToInt32(sizeList.Items[0].Value);
} else {
sizeID = 0;
}
} else {
sizeID = Convert.ToInt32(sizeList.SelectedValue);
}
// Rebind the size list according to the new colour ID selected
sizeList.DataSource = AvailableSizes(Convert.ToInt32(productID.Text), Convert.ToInt32(colourList.SelectedValue));
sizeList.DataBind();
// Reselect the previous selected size ID or if there wasn't one
// select the first on the list.
if (sizeList.Items.FindByValue(sizeID.ToString()) != null) {
sizeList.SelectedValue = sizeID.ToString();
} else {
if (sizeList.Items.Count > 0) {
sizeList.SelectedIndex = 0;
}
}
}
Phew!
DJ
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|