ecco tutto il codice:
namespace TestItems
{
/// <summary>
/// Descrizione di riepilogo per WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid test;
protected System.Web.UI.WebControls.Label label;
private DataSet myDs;
private void Page_Load(object sender, System.EventArgs e)
{
myDs = new DataSet();
DataTable myDt = new DataTable();
myDt.Columns.Add("lingua", System.Type.GetType("System.String"));
myDt.Columns.Add("languageID", System.Type.GetType("System.Int32"));
myDt.Rows.Add(myDt.NewRow());
myDt.Rows.Add(myDt.NewRow());
myDt.Rows.Add(myDt.NewRow());
myDt.Rows.Add(myDt.NewRow());
myDt.Rows[0][0] = "italiano";
myDt.Rows[0][1] = 1;
myDt.Rows[1][0] = "inglese";
myDt.Rows[1][1] = 2;
myDt.Rows[2][0] = "spagnolo";
myDt.Rows[2][1] = 3;
myDt.Rows[3][0] = "tedesco";
myDt.Rows[3][1] = 4;
myDs.Tables.Add(myDt);
if( !Page.IsPostBack )
{
test.DataSource = myDs;
test.DataBind();
}
}
private void test_ItemDataBound (object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList _drop = (DropDownList)e.Item.FindControl("drop");
_drop.DataSource = myDs;
_drop.DataTextField = "lingua";
_drop.DataValueField = "languageID";
_drop.DataBind();
}
}
private void test_ItemCreated (object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList _drop = (DropDownList)e.Item.FindControl("drop");
_drop.SelectedIndexChanged += new EventHandler(_drop_SelectedIndexChanged);
}
}
private void _drop_SelectedIndexChanged(object sender, EventArgs e)
{
label.Text = ((DropDownList)sender).ClientID;
}
#region Codice generato da Progettazione Web Form
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.test.ItemDataBound += new DataGridItemEventHandler(test_ItemDataBound);
this.test.ItemCreated += new DataGridItemEventHandler(test_ItemCreated);
}
#endregion
}
}
nella pagina
<form id="Form1" method="post" runat="server">
<asp:DataGrid ID="test" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList Runat="server" ID="drop" AutoPostBack="True"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Label Runat="server" id="label"></asp:Label>
</form>
mi scuso per la lunghezza eccessiva
ciao marco