简体   繁体   English

C#asp.net为什么我的手动__doPostBack只运行一次?

[英]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. 但是当我运行它时,onclick事件仅运行一次。

For example. 例如。 I have 4 listitems. 我有4个清单项。 When I click the first item the first time the onclick event is executed. 当我第一次单击第一个项目时,将执行onclick事件。 Now I click the second item, the onclick event is also executed. 现在,我单击第二项,还将执行onclick事件。 But when I now click the first item again the onclick event is not fired. 但是,当我现在再次单击第一项时,不会触发onclick事件。

When I check the error console in FireFox or Oprah I don't get any errors. 当我在FireFox或Oprah中检查错误控制台时,没有任何错误。

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? 您在哪里向菜单添加项目,并且正在检查IsPostBack属性? Please compare html after first loading and postpack to see if _dopostback dissappear. 请在第一次加载和后包装后比较html,以查看_dopostback是否消失。

Then Try to remove IsPostBack check. 然后尝试删除IsPostBack检查。 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. 可能是UpdatePanel引起了麻烦。 I would try pulling it out of UpdatePanel and then would see(which I believe it should) if that works? 我会尝试将其从UpdatePanel中拉出,然后查看(我认为应该)是否可行?

Secondly, you can probably just populate the list items Values and Texts only, and then under mnuMainMenu_MenuItemClick(object sender, MenuEventArgs e) event; 其次,您可能只填充列表项Values和Texts,然后在mnuMainMenu_MenuItemClick(object sender,MenuEventArgs e)事件下; look for the specific item that has been clicked (e.Item.* public properties), and then probably would use Response.Redirect() to do the job; 查找已单击的特定项目(例如e.Item。*公共属性),然后可能会使用Response.Redirect()来完成工作; well, just thinking out loud... 好吧,大声思考一下...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM