简体   繁体   中英

dynamically created Button not firing oncommand in a repeater asp.net C#

Here i am developing slots booking application using asp.net and C# . Poblem is my dynamically created button is not firing oncommand in a repeater control. (book button) without repeater i have created dynamic buttons and after and i recreated them using protected override void LoadViewState(object savedState).

but here am unable to get any idea how to recreate it because here i am loading checkboxes and button if it is only possible to recreate it then how to do it please help me i am scratching my head from 3days here is my code behind

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
        {

        }

    }

}

Thanks in Advance

Are you using dynamic data entities web application? If the button is inside the grid, then you can dynamically add button by using these code.

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)
        {

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM