简体   繁体   中英

asp.net button/htmlbutton event will not fire

I know its been asked before but I cannot get the event to fire even if Im creating the buttons and events each OnInit. Im doing a Sharepoint 2010 web part and fill a radiobuttonlist with values and add a submit button to the web part each time OnInit gets called. The Page_Load method is empty. I also tried putting the paintQnA call in an overridden CreateChildControls with the same result.

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        ...//Get data needed to create gui

        paintQnA(dr, dt, currentRow);
    }

    void paintQnA(DataRow dr, DataTable dt, int currentRow)
    {
        Panel p = new Panel();
        p.ID = currentRow.ToString();

        Label lbl= new Label();
        lbl.Text = dr["Title"].ToString();

        RadioButtonList rbl = new RadioButtonList();
        rbl.ID = currentRow.ToString() + "1";

        HtmlButton submitBtn = new HtmlButton();
        submitBtn.ID = currentRow.ToString();
        submitBtn.InnerText = "Click";
        submitBtn.ServerClick += new EventHandler(submitBtn_Click);

        ...//fill the radiobuttonlist with data

        this.Controls.Add(p);
        p.Controls.Add(lbl);
        p.Controls.Add(rbl);
        p.Controls.Add(submitBtn);

        UpdatePanel1.ContentTemplateContainer.Controls.Add(p);
    }

    void submitBtn_Click(object sender, EventArgs e)
    {
        ...//Should display another dynamically created panel but is never reached.
    }

Found the error:

p.ID = currentRow.ToString();
submitBtn.ID = currentRow.ToString();

They have the same ID...they can't have the same ID.

In your code I can see you are assigning the same ID to Panel and submitBtn . So due to duplicate Id, your click event is not fired.

Try different IDs for Panel and Submitbutton then It should work fine!

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