任何人都可以帮我吗?我正在加载页面时动态创建几个列表框,网格视图和按钮,我需要知道如何将列表框的选定项目传递给按钮单击?这是我的代码:
private void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
SqlConnection con = new SqlConnection("Data Source=N/A;Initial Catalog=GB_COCKPIT;Integrated Security=True");
con.Open();
SqlCommand SelectServeur = new SqlCommand("SELECT NomServeur, palier, description, responsable FROM pltf_Web.[GBV1001_Inventaire_IIS_Complet] where[FONCTION DE BASE] = 'NON CLASSÉ' AND[Fonction Caracteristique etendue] IS NULL", con);
SqlCommand SelectNomServeur = new SqlCommand("SELECT NomServeur FROM pltf_Web.[GBV1001_Inventaire_IIS_Complet] where[FONCTION DE BASE] = 'NON CLASSÉ' AND[Fonction Caracteristique etendue] IS NULL", con);
SqlDataReader dr = SelectServeur.ExecuteReader();
dt.Load(dr);
SqlDataReader dr1 = SelectNomServeur.ExecuteReader();
dt1.Load(dr1);
var NBServeurs = dt.Rows.Count;
for (int i = 0; i < NBServeurs; i++)
{
var nomserveurs = dt1.Rows[i].ItemArray;
var NomServeur = nomserveurs[0];
SqlCommand SelectComposantes = new SqlCommand("SELECT b.ID_Composant as composante FROM[gb_cockpIT].[pltf_web].[GBV1001_Inventaire_IIS_Complet] as a inner join[dbo].[GBD0007_Liste_Composant] as b on a.[Composante de base] = b.Nom_Composant_FR where a.NomServeur ='" + NomServeur + "' and b.ID_Plateforme = 103", con);
SqlDataReader dr2 = SelectComposantes.ExecuteReader();
dt2.Load(dr2);
DataTable dt4 = new DataTable();
SqlCommand SelectServeurs = new SqlCommand("SELECT NomServeur, palier, [composante de base],description, responsable FROM pltf_Web.[GBV1001_Inventaire_IIS_Complet] where[FONCTION DE BASE] = 'NON CLASSÉ' AND[Fonction Caracteristique etendue] IS NULL and NomServeur ='" + NomServeur + "'", con);
SqlDataReader dr4 = SelectServeurs.ExecuteReader();
dt4.Load(dr4);
GridView NomGridView = new GridView();
NomGridView.ID = "GridView" + i.ToString();
NomGridView.Width = 900;
NomGridView.DataSource = dt4;
NomGridView.DataBind();
form1.Controls.Add(NomGridView);
form1.Controls.Add(new LiteralControl("<br />"));
DataTable dt3 = new DataTable();
var nomComposantes = dt2.Rows[i].ItemArray;
var nomComposante = nomComposantes[0];
SqlCommand GetListComposantes = new SqlCommand("DECLARE @return_value int EXEC @return_value = [dbo].[GBSP0004_Liste_Fonctions] @IDComposant = " + nomComposante + "", con);
SqlDataReader dr3 = GetListComposantes.ExecuteReader();
dt3.Load(dr3);
ListBox NomListBox = new ListBox();
NomListBox.DataValueField = dt3.Columns["code_fonction"].ToString();
NomListBox.ID = "ListBox" + i.ToString();
NomListBox.DataSource = dt3;
NomListBox.DataBind();
NomListBox.Height = 200;
form1.Controls.Add(NomListBox);
form1.Controls.Add(new LiteralControl("<br />"));
form1.Controls.Add(new LiteralControl("<br />"));
Button NouveauBouton = new Button();
NouveauBouton.ID = "Bouton" + i.ToString();
NouveauBouton.Text = "Assigner";
form1.Controls.Add(NouveauBouton);
NouveauBouton.Click += new EventHandler(this.button_Click);
form1.Controls.Add(new LiteralControl("<br />"));
form1.Controls.Add(new LiteralControl("<br />"));
form1.Controls.Add(new LiteralControl("<hr>"));
form1.Controls.Add(new LiteralControl("<br />"));
form1.Controls.Add(new LiteralControl("<br />"));
}
con.Close();
}
protected void button_Click(object sender, EventArgs e)
{
Button buttonID = sender as Button;
}
答案 0 :(得分:0)
我看到你正在迭代并生成控件。如果我理解正确,你可以让按钮知道什么是相关的ListBox。然后,在单击时,您将获得列表框并查看所选内容。
首先,您需要添加以下代码行:NouveauBouton.Attributes["ListBox"] = NomListBox.ID;
。
所以在你的Page_Load中你有:
Button NouveauBouton = new Button();
NouveauBouton.ID = "Bouton" + i.ToString();
NouveauBouton.Text = "Assigner";
form1.Controls.Add(NouveauBouton);
NouveauBouton.Attributes["ListBox"] = NomListBox.ID;
NouveauBouton.Click += new EventHandler(this.button_Click);
在你的经纪人中:
protected void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (button.Attributes["ListBox"] != null)
{
ListBox listBox = (ListBox)this.FindControl(button.Attributes["ListBox"]);
string selected = listBox.SelectedValue;
//do stuff
}
}