简体   繁体   中英

C# asp.net why does my manual __doPostBack only run once?

In my code I create the menu items dynamically:

string listClientID = BulletedList1.ClientID.Replace('_', '$');
int counter = 0;

foreach (DataRow dataRow in database.DataTable.Rows)
{
    // Add Button
    ListItem listItem = new ListItem();
    listItem.Value = "buttonItem" + Convert.ToString(dataRow["rank"]);
    listItem.Text = " " + Convert.ToString(dataRow["title"]);
    listItem.Attributes.Add("onclick", "__doPostBack('" + listClientID + "', '"+ counter.ToString() +"')");

    BulletedList1.Items.Add(listItem);

    counter++;

}

This menu is inside a update panel:

<div id="MenuItemBox">
    <asp:BulletedList 
        ID="BulletedList1" 
        runat="server"
        OnClick="MenuItem_Click"
        >
    </asp:BulletedList>
</div>

What I want is when a listitem is clicked it performs a postback. But when I run this, the onclick event is only runned once.

For example. I have 4 listitems. When I click the first item the first time the onclick event is executed. Now I click the second item, the onclick event is also executed. But when I now click the first item again the onclick event is not fired.

When I check the error console in FireFox or Oprah I don't get any errors.

So my question is: how can I fix this and what am I doing wrong?

It seems you need to rebind it after postback. Where do you add items to the menu and are you checking IsPostBack property? Please compare html after first loading and postpack to see if _dopostback dissappear.

Then Try to remove IsPostBack check. My code is working well. Here is it.

public partial class _Default : System.Web.UI.Page {
    protected void Page_Load (object sender, EventArgs e) { }

    protected override void OnInit (EventArgs e) {
        base.OnInit(e);
        //if (!IsPostBack) {
            string listClientID = BulletedList1.ClientID.Replace('_', '$');
            int counter = 0;

            List<SomeClass> items = new List<SomeClass>(){ new SomeClass() { Rank = 1, Title = "2"},
            new SomeClass () {Rank = 2, Title = "Two"}};


            foreach (var item in items) {
                // Add Button
                ListItem listItem = new ListItem();
                listItem.Value = "buttonItem" + item.Rank;
                listItem.Text = " " + item.Title;
                listItem.Attributes.Add("onclick", "__doPostBack('" + listClientID + "', '" + counter.ToString() + "')");

                BulletedList1.Items.Add(listItem);

                counter++;

            }
        //}
    }

    protected void MenuItem_Click (object sender, BulletedListEventArgs e) {
        Response.Write(e.Index);
    }

    class SomeClass {
        public int Rank;
        public string Title;
    }
}

Probably UpdatePanel is causing the trouble. I would try pulling it out of UpdatePanel and then would see(which I believe it should) if that works?

Secondly, you can probably just populate the list items Values and Texts only, and then under mnuMainMenu_MenuItemClick(object sender, MenuEventArgs e) event; look for the specific item that has been clicked (e.Item.* public properties), and then probably would use Response.Redirect() to do the job; well, just thinking out loud...

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