Hi,
The following is a working example. Sorry if I misleaded you, your situation
is quite simpler than the other one.
Anyway, little explanation of the code:
In the handler of the OnCommand event of the ImageButtons we loop through
the item collection of the DataList until FindControl("Ad dToCartBttn")
returns a control instance that equals the event source - the first
parameter of the handler, that is. Then we get reference to the
RadioButtonList which is in the current DataListItem and that's all.
listing <test.aspx>:
<%@ Page language="C#" inherits="test_ aspx" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
</head>
<body>
<form id="form1" method="post" runat="server">
<asp:datalist id="CrossSellLi st" runat="server"
repeatdirection ="Vertical" runat="server">
<itemtemplate >
<asp:radiobutto nlist id="ProductOpti onsList"
repeatdirection ="Vertical" runat="server"
datasource='<%# DataBinder.Eval (Container.Data Item, "ARRAY")%>'/>
<br />
<asp:imagebutto n id="AddToCartBt tn"
oncommand="AddT oCartBttn_Comma nd" runat="server"
commandname='<% # DataBinder.Eval (Container.Data Item, "ID")%>'/>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist>
<br />
<br />
ID: <asp:literal id="LiteralID" runat="server"/><br />
Selected: <asp:literal id="LiteralSele cted" runat="server"/>
</form>
</body>
</html>
listing <test_aspx.cs >:
using System;
public class test_aspx : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data List CrossSellList;
protected System.Web.UI.W ebControls.Lite ral LiteralID, LiteralSelected ;
protected void Page_Load(objec t s, EventArgs e)
{
if(!IsPostBack)
{
System.Data.Dat aTable dt =
new System.Data.Dat aTable();
System.Data.Dat aRow dr;
dt.Columns.Add( "ID", typeof(string)) ;
dt.Columns.Add( "ARRAY", typeof(string[]));
dr = dt.NewRow();
dr[0] = "1";
dr[1] = new string[]
{"Option 1", "Option 2", "Option 3"};
dt.Rows.Add(dr) ;
dr = dt.NewRow();
dr[0] = "2";
dr[1] = new string[]
{"Value 1", "Value 2", "Value 3"};
dt.Rows.Add(dr) ;
CrossSellList.D ataSource = dt.DefaultView;
CrossSellList.D ataBind();
}
}
protected void AddToCartBttn_C ommand(object s,
System.Web.UI.W ebControls.Comm andEventArgs e)
{
foreach(System. Web.UI.WebContr ols.DataListIte m item
in CrossSellList.I tems)
{
if(item.ItemTyp e == System.Web.UI.W ebControls.List ItemType.Item ||
item.ItemType == System.Web.UI.W ebControls.List ItemType.Altern atingItem)
{
System.Web.UI.W ebControls.Imag eButton AddToCartBttn =
item.FindContro l("AddToCartBtt n") as
System.Web.UI.W ebControls.Imag eButton;
if(AddToCartBtt n != null && AddToCartBttn.C ommandName == e.CommandName)
{
System.Web.UI.W ebControls.Radi oButtonList _ProductOptions List =
item.FindContro l("ProductOptio nsList") as
System.Web.UI.W ebControls.Radi oButtonList;
LiteralID.Text = e.CommandName;
LiteralSelected .Text = string.Format(
@"Index: {0};Value: {1};Text: {2}",
_ProductOptions List.SelectedIn dex,
_ProductOptions List.SelectedVa lue,
_ProductOptions List.SelectedIt em.Text);
}
}
}
}
}
Hope this helps
Martin
<an*******@disc ussions.microso ft.com> wrote in message
news:24******** *************** *****@phx.gbl.. .
I'm not sure I follow you. Here is where I'm at:
The control:
<asp:datalist
id="CrossSellLi st"
onload="CrossSe llList_Load"
onitemcreated=" CrossSellList_I temCreated"
onselectedindex changed="CrossS ellList_Selecte dIndexChanged
"
repeatdirection ="Vertical"
runat="server">
<itemtemplate >
<asp:radiobutto nlist
id="ProductOpti onsList"
repeatdirection ="Vertical"
cssclass="Norma l"
runat="server"
/>
<asp:requiredfi eldvalidator
controltovalida te="ProductOpti onsList"
errormessage="Y ou must choose a product option before
adding item to shopping cart!"
enableclientscr ipt="True"
/>
<br />
<asp:imagebutto n
id="AddToCartBt tn"
onload="AddToCa rtBttn_Load"
runat="server"
/>
</td>
</tr>
</table>
</itemtemplate>
<selecteditemte mplate>
</selecteditemtem plate>
</asp:datalist>
In the code behind:
protected void CrossSellList_L oad(object
sender, System.EventArg s e)
{
CrossSellList.D ataSource =
ProductManager. GetCrossSellPro ducts();
CrossSellList.D ataBind();
}
protected void AddToCartBttn_L oad(object
sender, System.EventArg s e)
{
ImageButton bttn = (ImageButton)
sender;
bttn.ImageUrl = String.Format("/
{0}/images/buy_now.gif", this.WebRoot);
}
protected void CrossSellList_I temCreated
(object sender, DataListItemEve ntArgs e)
{
ListItemType lit =
e.Item.ItemType ;
if (lit == ListItemType.He ader ||
lit ==
ListItemType.Fo oter ||
lit ==
ListItemType.Se parator)
return;
Label titleLbl = (Label)
e.Item.FindCont rol("ProductAva ilabilityLbl");
RadioButtonList prodOpList =
(RadioButtonLis t)e.Item.FindCo ntrol("ProductO ptionsList");
if (!Page.IsPostBa ck)
{
IProduct product =
(IProduct)e.Ite m.DataItem;
IList prodOptions =
ProductManager. GetProductOptio nsByProduct(pro duct);
//populate product
options repeater
prodOpList.Data Source =
prodOptions;
prodOpList.Data TextField
= "Name";
prodOpList.Data ValueField
= "ID";
prodOpList.Data Bind();
if (prodOptions.Co unt > 0)
titleLbl.Text
= "Available in:";
Response.Write
(product.Name);
}
}
NOW - When each button is clicked, I want to access what
was chosen in each individual radiobuttonlist , and
redirect to a new page.
-----Original Message-----
Hi, V. Jenks,
Maybe my replies to the subject "Controls in
HeaderTemplate" by Yan Wangcould be helpful.
Basically this is the situation on the postback:
The handler for the ImageButton OnClick event is
executed first - you setsome flag in a private field of your page class that
this has happened.
Then the handler of the OnItemCreated event of the
DataList gets executed.Here you get gold of references to the controls you need
to work withlater - store these in private fields of the class.
Then, after the viewstate is loaded (I choose to handle
the PreRender eventof the DataList), check if there should be some
processing (the flag set inthe ImageButton OnClick handler, that is). If yes -
execute a method thatwill do the actual processing.
Hope this helps
Martin
"V. Jenks" <sp**@scientifi k.com> wrote in message
news:1f******* *************** ******@phx.gbl. .. What seems like a simple thing is apparently not so
straightforward ?
I have a datalist.
Inside of that datalist is an <itemtemplate > secion which contains other server controls such as a label, a
radiobuttonlist , etc.
I'm driving myself insane trying to figure out how to get data in and out of those controls contained in the
datalist!
For example, I want to populate the radiobutton and label being repeated for each row of data.
Also, the <itemtemplate > defines an imagebutton.
I want to be able (for each item) to choose something
from the radiobuttonlist and then click the imagebutton
to submit to another page.
I've found that I can use FindControl() in the
ItemCreated event handler for the DataList to *find* the control itself, however, that isn't my problem.
My problem is in the OnClick event handler for the
imagebutton for each item, I'm unable to access the
radiobuttonlist selected item!
How is this done? What's the cleanest, simplest way to
get access and fire events from nested controls like this?
Thanks!
.