1

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

low_rents
  • 4,481
  • 3
  • 27
  • 55
Tajkumar
  • 317
  • 2
  • 5
  • 16

1 Answers1

0

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

        }
temUser
  • 142
  • 8
  • here i am using repeater and i have created a button dynamically and my problem is oncommand is not firing for dynamically created button – Tajkumar Sep 08 '15 at 09:11
  • based on my code , you should initialize command inside GridView1_RowCreated. lb.Command += new CommandEventHandler(OnClick_Command); And create Onclick Command event as like this. private void OnClick_Command(object sender, CommandEventArgs e) { } – temUser Sep 08 '15 at 09:40
  • i want to access those checkboxes of that row inside a repeater how to do that in a private function. – Tajkumar Sep 08 '15 at 15:40
  • Is your check boxes inside your grid? What you want to do with check box when you click button? Please explain the scenario. – temUser Sep 09 '15 at 03:09
  • add checkbox slots to booking section – Tajkumar Sep 10 '15 at 17:38
  • I couldn't understand the scenario. If you click book button, check boxes should be added to grid rows. Is it correct? – temUser Sep 11 '15 at 04:24