C#asp.net为什么我的手动__doPostBack只运行一次? [英] C# asp.net why does my manual __doPostBack only run once?

查看:264
本文介绍了C#asp.net为什么我的手动__doPostBack只运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我动态创建菜单项:

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>

我想要的是单击列表项时执行回发.但是,当我运行此命令时,onclick事件仅运行一次.

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.

例如.我有4个清单项.当我第一次单击第一个项目时,将执行onclick事件.现在,我单击第二项,onclick事件也将执行.但是当我现在再次单击第一项时,不会触发onclick事件.

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.

当我在FireFox或Oprah中检查错误控制台时,我没有得到任何错误.

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?

推荐答案

似乎您需要在回发后重新绑定它.您在哪里向菜单添加项目,是否正在检查IsPostBack属性?请在第一次加载和打包后比较html,以查看_dopostback是否消失.

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.

然后尝试删除IsPostBack检查.我的代码运行良好.就是这个.

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;
    }
}

这篇关于C#asp.net为什么我的手动__doPostBack只运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆