EventHandlers没有为DropDownList和按钮触发

时间:2011-11-09 15:46:13

标签: c# asp.net drop-down-menu

同事和我无法弄清楚为什么以下代码中的EventHandlers没有被解雇。在加载第一页之前调用该函数两次,并将AutoPostBack设置为true。 lnkbtn按钮和ddlDose DropDownList是未被触发的按钮。它回发,但它不会调用事件处理程序。这是一些代码......任何人都可以看到任何内在的错误吗?

的Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
  if (IsPostBack)
  {
    tblMedOrdering = (Table)Session["tblMedOrdering"];
    List<string>medNames = (List<string>)Session["medNamesList"];
    populateMeds(medNames); 
  }
}

PopulateMeds:

protected void populateMeds(List<string> medNames)
{
        if (medNames.Count == 0)
        {
            tblMedOrdering = (Table)Session["tblMedOrdering"];
        }
        else
        {
            DataSet ds = new DataSet();
            string strConn = Application["dbconn"].ToString();
            using (SqlConnection dbconn = new SqlConnection(strConn))
            using (SqlCommand medDbcmd = dbconn.CreateCommand())
            {
                dbconn.Open();

                for (int i = 0; i < medNames.Count; i++)
                {
                    DropDownList ddlDose = new DropDownList();
                    DropDownList ddlSig = new DropDownList();

                    int flag = 0;

                    if ((Table)Session["tblMedOrdering"] != null)
                    {
                        for (int j = 0; j < tblMedOrdering.Rows.Count; j++)
                        {
                            if (tblMedOrdering.Rows[j].Cells[3].Text == medNames[i].ToString())
                            {
                                flag = 1;
                                ddlSig = (DropDownList)tblMedOrdering.Rows[i].Cells[2].FindControl(medNames[i].ToString() + "ddlSig");
                                if (ddlSig == null)
                                {
                                    ddlSig.ID = medNames[i].ToString() + "ddlSig";
                                    ListItem liDefaultSig = new ListItem();
                                    liDefaultSig.Value = "0";
                                    liDefaultSig.Text = "Select . . .";
                                    ddlSig.Items.Add(liDefaultSig);
                                }
                                break;
                            }
                        }
                    }

                    if (flag != 1)
                    {
                        ListItem liDefault = new ListItem();
                        liDefault.Value = "0";
                        liDefault.Text = "Select . . .";

                        ddlDose.Items.Add(liDefault);
                        ddlSig.Items.Add(liDefault);

                        ddlDose.ID = medNames[i].ToString() + "ddlDose";
                        ddlSig.ID = medNames[i].ToString() + "ddlSig";

                        ddlDose.AutoPostBack = true;

                        ddlDose.TextChanged += new EventHandler(ddlDose_SelectedIndexChanged);

                        medDbcmd.CommandText = "Select distinct medorderid, dose from ordertablemeds where medordername = '" + medNames[i].ToString() + "'";

                        SqlDataReader dr = medDbcmd.ExecuteReader();

                        if (dr != null)
                        {
                            while (dr.Read())
                            {
                                ListItem liDose = new ListItem();

                                liDose.Value = dr["medorderid"].ToString();
                                liDose.Text = dr["dose"].ToString();

                                ddlDose.Items.Add(liDose);
                            }
                            dr.Close();
                        }

                        TableRow tr = new TableRow();
                        tr.ID = medNames[i].ToString() + "TableRow";

                        TableCell tcDose = new TableCell();
                        TableCell tcSig = new TableCell();
                        TableCell tcRemove = new TableCell();
                        TableCell tcMedName = new TableCell();


                        tcDose.Controls.Add(ddlDose);

                        tcSig.Controls.Add(ddlSig);
                        tcMedName.Text = medNames[i].ToString();
                        tcMedName.ID = medNames[i].ToString() + "medname";

                        LinkButton lnkbtn = new LinkButton();
                        lnkbtn.Text = "X";
                        lnkbtn.ForeColor = System.Drawing.Color.Red;
                        lnkbtn.ID = medNames[i].ToString() + "_lnkbtn" + tblMedOrdering.Rows.Count;
                        lnkbtn.Click += new EventHandler(Reset_Click);

                        tcRemove.Controls.Add(lnkbtn);

                        tr.Cells.Add(tcRemove);
                        tr.Cells.Add(tcDose);
                        tr.Cells.Add(tcSig);
                        tr.Cells.Add(tcMedName);

                        tblMedOrdering.Rows.Add(tr);

                    }
                }
                tblMedOrdering.DataBind();
                dbconn.Close();
            }
        }

        Session["tblMedOrdering"] = tblMedOrdering;

        if (medNames.Count == 0)
        {
            cpeMeds.Collapsed = true;
            cpeMeds.ClientState = "True";
        }

        List<string> medNamesList = new List<string>();
        DropDownList ddl = new DropDownList();
        for (int k = 0; k < tblMedOrdering.Rows.Count; k++)
        {
            ddl = (DropDownList)tblMedOrdering.Rows[k].Cells[1].FindControl(tblMedOrdering.Rows[k].Cells[3].Text +"ddlDose");
            ddl.SelectedIndexChanged += new EventHandler(ddlDose_SelectedIndexChanged);
            medNamesList.Add(tblMedOrdering.Rows[k].Cells[3].Text);
        }
        Session["medNamesList"] = medNamesList;
    }

PopulateMeds只是一个在代码隐藏中调用的函数,并且给出了一个字符串名称列表,因此它不需要args就好像它是一个控件(对象发送者,EventArgs e)。

1 个答案:

答案 0 :(得分:2)

将该逻辑移至OnInit并且事件处理程序应该正常运行,假设其他一切都正确。我还会检查以确保您没有验证干扰回发。