我试图从下拉列表中获取所选值,该列表位于webform的面板中以执行某些计算。我已经创建了一些动态生成的下拉列表,现在我必须从这些下拉列表中检索所选值以计算总数,然后确保它不超过100.总数应该在文本框中显示。你可以帮帮我吗?
protected void btn_AddObjectives_Click(object sender, EventArgs e)
{
int rowCount = 0;
//initialize a session.
rowCount = Convert.ToInt32(Session["clicks"]);
rowCount++;
//In each button click save the numbers into the session.
Session["clicks"] = rowCount;
//Create the textboxes and labels each time the button is clicked.
for (int i = 0; i < rowCount; i++)
{
TextBox TxtBoxO = new TextBox();
TxtBoxO.TextMode = TextBoxMode.MultiLine;
DropDownList DDLW = new DropDownList();
DropDownList DDLA = new DropDownList();
Label lblO = new Label();
Label lblW = new Label();
Label lblA = new Label();
TxtBoxO.ID = "TextBoxU" + i.ToString();
DDLW.ID = "DDLW" + i.ToString();
DDLA.ID = "DDLA" + i.ToString();
TxtBoxO.Width = 325;
DDLW.Height = 25;
DDLA.Height = 25;
DDLA.ID = "ddl_Weight";
DDLA.Items.Add("Select");
DDLA.Items.Add("5");
DDLA.Items.Add("10");
DDLA.Items.Add("15");
DDLA.Items.Add("20");
DDLA.Items.Add("25");
DDLA.Items.Add("30");
DDLA.Items.Add("35");
DDLA.Items.Add("40");
DDLA.Items.Add("45");
DDLA.Items.Add("50");
DDLA.Items.Add("55");
DDLA.Items.Add("60");
DDLA.Items.Add("65");
DDLA.Items.Add("70");
DDLA.Items.Add("75");
DDLA.Items.Add("80");
DDLA.Items.Add("85");
DDLA.Items.Add("90");
DDLA.Items.Add("95");
DDLA.Items.Add("100");
DDLW.ID = "ddl_Achieved";
DDLW.Items.Add("Select");
DDLW.Items.Add("5");
DDLW.Items.Add("10");
DDLW.Items.Add("15");
DDLW.Items.Add("20");
DDLW.Items.Add("25");
DDLW.Items.Add("30");
DDLW.Items.Add("35");
DDLW.Items.Add("40");
DDLW.Items.Add("45");
DDLW.Items.Add("50");
DDLW.Items.Add("55");
DDLW.Items.Add("60");
DDLW.Items.Add("65");
DDLW.Items.Add("70");
DDLW.Items.Add("75");
DDLW.Items.Add("80");
DDLW.Items.Add("85");
DDLW.Items.Add("90");
DDLW.Items.Add("95");
DDLW.Items.Add("100");
lblO.ID = "LabelO" + i.ToString();
lblW.Text = "LabelW" + i.ToString();
lblA.ID = "LabelA" + i.ToString();
lblO.Text = "Objective " + " " + (i + 1).ToString() + " : ";
lblW.Text = " Weightage" + " " + (i + 1).ToString() + " : ";
lblA.Text = " Achieved " + " " + (i + 1).ToString() + " : ";
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(lblO);
Panel1.Controls.Add(TxtBoxO);
Panel1.Controls.Add(lblW);
Panel1.Controls.Add(DDLW);
Panel1.Controls.Add(lblA);
Panel1.Controls.Add(DDLA);
}
}
答案 0 :(得分:0)
将事件处理程序附加到for循环中的所有下拉列表中,如此
DDLA.AutoPostBack = true;
DDLA.SelectedIndexChanged += DDLA_SelectedIndexChanged;
DDLW.AutoPostBack = true;
DDLW.SelectedIndexChanged += DDLW_SelectedIndexChanged;
然后
事件中的
protected void DDLA_SelectedIndexChanged(object sender, EventArgs e)
{
// typecast sender as dropdown list and logic to calculate selected value
}
protected void DDLW_SelectedIndexChanged(object sender, EventArgs e)
{
// typecast sender as dropdown list and logic to calculate selected value
}
希望这有帮助