c# - 将列表框选定项分配给 button_click
问题描述
谁能帮我这个?我在加载页面时动态创建了几个列表框、网格视图和按钮,我需要知道如何将列表框的选定项目传递给按钮单击?这是我的代码:
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;
}
解决方案
我看到您正在迭代和生成控件。如果我理解正确,您可以让按钮知道相关的 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
}
}
推荐阅读
- asp.net - ASP.NET MVC:从视图导航时如何将客户端浏览器上的 DOM Body 元素的 ID 发送到控制器
- clojure - 将布尔值映射到 clojure 中的列表
- python - 如何查找具有一个或多个负值的熊猫列
- python - pandas get.dummies API 使用
- telegram-bot - 如何从节点红色的环境变量中设置电报机器人的名称?
- javascript - 将 toUTCString 格式化为 toLocaleString
- javascript - 将 HTMLFormElement.reset() 与 Downshift 和 react-hook-form 一起使用
- mysql - 休眠保存或更新会话不起作用
- ruby-on-rails - 你如何在 Rails 中测试 flash[:danger] 和其他 flash 消息?
- sequelize.js - Sequelize - 返回标准化数据