簡體   English   中英

動態創建的按鈕不會在轉發器asp.net C#中觸發oncommand

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

在這里,我正在使用asp.net和C#開發插槽預訂應用程序。 問題是我動態創建的按鈕未在中繼器控件中觸發oncommand (書本按鈕)沒有中繼器,我創建了動態按鈕,之后,我使用受保護的重寫void LoadViewState(object savedState)重新創建了它們。

但是這里無法獲得任何關於如何重新創建它的想法,因為在這里我正在加載復選框和按鈕,如果只能重新創建它,那么該如何做,請幫助我,我從3days開始摸索,這里是我的代碼

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
        {

        }

    }

}

提前致謝

您是否正在使用動態數據實體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)
        {

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM