0

我有三个下拉列表,根据列表中选择的值,我相应地创建动态控件。一切都正确显示,但是当我单击动态创建的按钮时,事件没有触发。我正在使用 Page_LoadComplete 创建动态控件,因为我需要来自 DDL 的值。这是我的按钮事件没有触发的原因吗?

        protected void Page_LoadComplete(object sender, EventArgs e)
    {
        queries = new clsFormQueries();

        if (HttpContext.Current.User.IsInRole("Admin"))
        {
            if (!Page.IsPostBack)
            {
                List<Sport> sports = ComboBoxOptions.getSports();
                DropDownList temp = (DropDownList)loginView2.FindControl("Sport");
                temp.DataSource = sports;
                temp.DataTextField = "Name";
                temp.DataValueField = "id";
                temp.DataBind();

                DropDownList teamsDD = (DropDownList)loginView2.FindControl("Team");

                List<Team> teamsList = ComboBoxOptions.getTeamsBySportId(Convert.ToInt32(temp.SelectedValue));
                teamsDD.DataSource = teamsList;
                teamsDD.DataTextField = "fullName";
                teamsDD.DataValueField = "teamId";
                teamsDD.DataBind();

                DropDownList existingPlayers = (DropDownList)loginView2.FindControl("ExistingPlayers");

                List<Player> players = ComboBoxOptions.getPlayersBySport(Convert.ToInt32(temp.SelectedValue), Convert.ToInt32(teamsDD.SelectedValue));
                existingPlayers.DataSource = players;
                existingPlayers.DataTextField = "fullName";
                existingPlayers.DataValueField = "playerid";
                existingPlayers.DataBind();


                //if (existingPlayers.SelectedValue != "" && temp.SelectedValue != "")
                //{

                //    DataTable updates = queries.GetPlayerUpdate(Convert.ToInt32(existingPlayers.SelectedValue), Convert.ToInt32(temp.SelectedValue));
                //    if (updates.Rows.Count > 0)
                //        CreateUpdatesHTML(updates);
                //}

            }

            DropDownList temp2 = (DropDownList)loginView2.FindControl("Sport");
            DropDownList existingPlayers2 = (DropDownList)loginView2.FindControl("ExistingPlayers");
            if (existingPlayers2.SelectedValue != "" && temp2.SelectedValue != "")
            {

                DataTable updates = queries.GetPlayerUpdate(Convert.ToInt32(existingPlayers2.SelectedValue), Convert.ToInt32(temp2.SelectedValue));
                if (updates.Rows.Count > 0)
                    CreateUpdatesHTML(updates);
            }

        }



    }


private void CreateUpdatesHTML(DataTable updates)
    {

        foreach (DataRow update in updates.Rows)
        {
            int playerUpdateId = (int)update["playerUpdateId"];
            string updateText = update["PlayerUpdate"].ToString();

            TextBox tb = new TextBox();
            tb.ID = "UpdateText" + playerUpdateId;
            tb.TextMode = TextBoxMode.MultiLine;
            tb.Rows = 5;
            tb.Text = updateText;
            tb.CssClass = "span5";

            Button btnDelete = new Button();
            btnDelete.Click += new EventHandler(btnDelete_Click);
            btnDelete.ID = "Delete"+playerUpdateId.ToString();
            btnDelete.Text = "Delete";
            btnDelete.CssClass = "btn btn-info";

            Button btnUpdate = new Button();
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            btnUpdate.ID = "Update"+playerUpdateId.ToString();
            btnUpdate.Text = "Update";
            btnUpdate.CssClass = "btn btn-info";

            HtmlGenericControl div2 = new HtmlGenericControl("div");
            div2.Attributes.Add("class", "pull-right span5");

            div2.Controls.Add(btnDelete);
            div2.Controls.Add(btnUpdate);

            HtmlGenericControl hr = new HtmlGenericControl("hr");
            HtmlGenericControl br = new HtmlGenericControl("br");           
            existingUpdates.Controls.Add(tb);
            existingUpdates.Controls.Add(div2);
            existingUpdates.Controls.Add(br);
            existingUpdates.Controls.Add(hr);

        }
    }
4

1 回答 1

0

我的猜测是,由于您已将 Page_LoadComplete 事件的逻辑包装在 if(!Page.IsPostBack) 中,因此由于单击按钮会导致回发,因此您的动态创建的控件不会正确重新创建(阅读:没有单击事件处理已接线)。您需要在每次调用 Page_LoadComplete(非回发或回发)时都有逻辑,以重新创建动态控件并连接它们的事件。

于 2013-06-17T03:12:39.440 回答