首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将列表框选定项分配给button_click

将列表框选定项分配给button_click
EN

Stack Overflow用户
提问于 2018-06-06 03:11:44
回答 1查看 52关注 0票数 0

有人能帮我吗?当我的页面加载时,我动态地创建了几个列表框、网格视图和按钮,我需要知道如何将列表框的选定项传递给按钮单击?这是我的代码:

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;
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-06 03:50:43

我看到你在迭代和生成控件。如果我没理解错的话,你可以让按钮知道什么是相关的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
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50707359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档