开发者

Events that arent triggered by their buttons

开发者 https://www.devze.com 2023-03-12 23:58 出处:网络
I generated two buttons and put them into a list of buttons.. List<Button> buttons = new List<Button>();

I generated two buttons and put them into a list of buttons..

       List<Button> buttons = new List<Button>();
       Button pgs=new Button
            for(int i=0;i<2;i++)
                        pgs.Width = 20;
                        pgs.Command += obtainTopicsPerPage_Click;
                        pgs.CommandName = i.ToString();
                        pgs.Text =i.ToString();

                        btns.Add(tPage.ToString());
                        buttons.Add(pgs);

I added the buttons to a placeholder, and they appear on the page.

The event that they have is the following:

   void obtainTopicsPerPage_Click(Object sender, CommandEventArgs e)
    {
        foreach (var item in tPages)
        {
            if (item.Key == e.CommandName)
            {
                foreach (var posts in item.Value)
                {
                    posts.ExecuteAll();
                }
            }
        }
         MyButtonTable();
    }

It doesn't matter what the functions do (they simply create tables and make it look like posts in a forum page)..

Now when the user clicks any one of the buttons that appear on the screen..none of the events are being triggered.. I put a breaking point inside the eventhanlder method and the web application doesn't reach there.

All I am concerned is why the buttons aren't attached to the event handling method that I gave them..why when I set the break point in the event it never triggers.

What should happen, when a button is clicked.. the event should should be triggered first, and the page should load second. But that doesn't happen..what happens is the button event being skipped, and the page load event is triggered with every postback after the button click..

Updated:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    string[] d;
    protected void Page_Load(object sender, EventArgs e)
    {
         d = new string[] { "dfadfas", "daads", "dasda", "dads" };
        Buttons();
    }
    List<Button> btns;
    public void Buttons()
    {
        btns = new List<Button>();
        for (int i = 0; i < 20; i++)
        {
            Button d = new Button();
            d.Text = "Click me";
            d.Click += Me_Click;
            btns.Add(d);

        }
       function();

    }
    public void function()
    {
        foreach (var item in btns)
        {
            PlaceHolder1.Controls.Add(item);
        }

    }
    public int i { get{object o=ViewState["i"];return (o==null)?0:(int)o;} set{ViewState["i"]=value;} }
    public void Me_Click(object sender, EventArgs e)
    {

        foreach (var item in d)
        {
            Label da= new Label();
            da.Text = "d"+i+++"<br/>";
            this.Controls.Add(da);

        }


    }
}

The working code above..

I tested by saving the buttons list with Session. Here I recreate the buttons at PreInit

 void Page_PreInit(object sender, EventArgs e)
{
    List<Button> btn = (List<Button>)Session["Buttons"];//debugging shows 2 buttons
    if (btn != null)
    {
        foreach (var item in btn)
        {
            item.开发者_C百科Width = 20;
            item.Command += obtainTopicsPerPage_Click;
            item.CommandName = tPage.ToString();
            item.Text = tPage.ToString();
        }
    }


Are you sure you are using the right event? Shouldn't you be using Button.Click ?


This pgs.Command += obtainTopicsPerPage_Click; should be pgs.Command += new CommandEventHandler(obtainTopicsPerPage_Click);

You did not add the Command Handler correctly new CommandEventHandler(obtainTopicsPerPage_Click);

Edit: Since you are adding dynamic controls, On the page load your code should executed again to fire the event.


There's nothing wrong with using Command.

Try this on a new page as a starting point and you should see the Button1_Command event throw the expected exception. I certainly do :)

    protected void Page_Load(object sender, EventArgs e)
    {
        Button pgs = new Button();
        pgs.Command += Button1_Command;
        Controls.Add(pgs);
    }

    void Button1_Command(object sender, CommandEventArgs e)
    {
        throw new NotImplementedException();
    }

I suspect a problem elsewhere in your code we're not seeing. Show us more of the code related to holding the buttons, etc., please.

And by the way, YES, it does compile and RUN (though I forgot the form1 reference ;) )

Events that arent triggered by their buttons


This works for me, and should hopefully give you an idea. I know it's very simplistic but follows on from what you've provided. When you click the buttons the page Reloads therefore recreating the buttons and setting everything up again. To avoid that happening check that Page.IsPostBack is false.

public partial class GuessNumber : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        lblResult.Text += "Written in by Page_Load <br />";
    }


    protected void LoveMyButtons_click(object sender, EventArgs e) {
        string response = txtGuess.Text;

        lblResult.Text += string.Format("LoveMybuttons " + response + " <br />");
    }

    protected void Page_PreInit(object sender, EventArgs e) {
        CreateButtons();
    }


    List<Button> btns;
    public void CreateButtons() {

        btns = new List<Button>();

        for (int i = 0; i < 6; i++) {
            Button butt = new Button();
            butt.Text = "Click me";
            butt.Click += LoveMyButtons_click;
            btns.Add(butt);

        }
        AddMyButtonsToAPlaceHolder();
    }

    public void AddMyButtonsToAPlaceHolder() {
        foreach (var item in btns) {
            plhButtonStore.Controls.Add(item);
        }

    }

}




<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtGuess" runat="server" />
    <asp:Button ID="btnPeanut" runat="server" Text="Guess" />

    <br /><br />
    <asp:Label ID="lblNumberOfGuesses" runat="server" />
    <br />
    <asp:Label ID="lblResult" runat="server" />
    <br />
    <asp:PlaceHolder ID="plhButtonStore" runat="server" />
</div>
</form>
</body>


Where are you creating the buttons? It's probably the wrong place. See here: http://msdn.microsoft.com/en-us/library/ms178472.aspx

Try it in PreInit as the documentation suggests.


Following on from what other posters have said.

You could try the following:

pgs.Click += obtainTopicsPerPage_Click;

And making sure the set up code is in the PreInit or PreRender events.

0

精彩评论

暂无评论...
验证码 换一张
取 消