1

在这里,我正在使用 asp.net 和 C# 开发插槽预订应用程序。问题是我动态创建的按钮没有在转发器控件中触发oncommand。(书按钮)没有中继器我创建了动态按钮,之后我使用受保护的覆盖无效LoadViewState(对象已保存状态)重新创建了它们。

但是这里我不知道如何重新创建它,因为在这里我正在加载复选框和按钮,如果只能重新创建它,那么如何做到这一点请帮助我,我从 3 天开始挠头这是我的代码背后

protected void grounds_ItemCommand(object source, RepeaterCommandEventArgs   e)
{
    if (e.CommandName == "btn")
    {
        Panel pl = (Panel)e.Item.FindControl("slotspanel");
        Button book = new Button();
       // Panel pl2 = (Panel)e.Item.FindControl("backgroundpanel");
        LinkButton lb = new LinkButton();
        LiteralControl lineBreak = new LiteralControl("<br/>");
        int listItemIds = 1;
        HtmlGenericControl ul = new HtmlGenericControl("ul");
        DataSet ds = new DataSet();
        ds = obj.getslots(1, "2014-10-10");
        if (ds != null)
        {

            if (ds.Tables[1].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                {
                    HtmlGenericControl li = new HtmlGenericControl("li");
                    CheckBox chk = new CheckBox();
                    HiddenField hd = new HiddenField();
                    // LinkButton lnk = new LinkButton();

                    chk.ID = "chk" + listItemIds;
                    hd.ID = "hd" + listItemIds;
                    chk.Text = ds.Tables[1].Rows[i]["slottimings"].ToString();
                    hd.Value = ds.Tables[1].Rows[i]["Type"].ToString();
                    // lnk.Click += Clicked;
                    //lnk.Command += new CommandEventHandler(lnkColourAlternative_Click);
                    //lnk.Click 
                    li.Controls.Add(chk);

                    ul.Controls.Add(li);
                    listItemIds++;
                }
            }
        }
        HtmlGenericControl li2 = new HtmlGenericControl("li");
        book.ID = "bookbutton";
        book.Text = "Book";
        book.CommandName = "Book";
        book.EnableViewState = true;
        book.UseSubmitBehavior = false;
        HiddenField count = new HiddenField();
        count.Value = (listItemIds-1).ToString();
        count.ID = "hiddencount";
        li2.Controls.Add(book);
        ul.Controls.Add(li2);
        pl.Controls.Add(ul);
        pl.Controls.Add(lineBreak);

        //pl2.Visible = true;

    }
    if (e.CommandName == "book")
    {
        if (Session["emailid"] != null)
        {

            HiddenField hd = (HiddenField)e.Item.FindControl("hiddencount");
            int r = Convert.ToInt16(hd.Value);
            int cost = 0;
            string slots = string.Empty;
            DataSet ds = new DataSet();
            ds = obj.getgrounddetails(1, Session["sportsname"].ToString());
            for (int i = 1; i <= r; i++)
            {
                CheckBox chk = (CheckBox)e.Item.FindControl("chk" + i);
                HiddenField hhd = (HiddenField)e.Item.FindControl("hd" + i);
                if (chk.Checked)
                {

                    //type = hhd.Value;
                    cost = cost + Convert.ToInt16(ds.Tables[1].Rows[i][hhd.Value].ToString());
                    slots = slots + chk.Text;

                }
            }

            Session["cost"] = cost.ToString();
            Session["slots"] = slots;
            Response.Redirect("payment/payment.aspx");
        }
        else
        {

        }

    }

}

提前致谢

4

1 回答 1

0

您正在使用动态数据实体 Web 应用程序吗?如果按钮在网格内,那么您可以使用这些代码动态添加按钮。

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = new LinkButton();
                lb.Text = "Book";
                lb.ID = "bookbutton";
                lb.CommandName = "Book";
                lb.Visible = true;
                var firstCell = e.Row.Cells[0];
                firstCell.Controls.Add(lb);
                lb.Command += new CommandEventHandler(OnClick_Command);
            }

        }

private void OnClick_Command(object sender, CommandEventArgs e)
        {

        }
于 2015-09-08T08:06:58.040 回答