简体   繁体   English

Asp.net:点击动态生成的按钮控件后页面不会刷新

[英]Asp.net: Page won't refresh after clicking dynamically generated Button controls

Clicking on any of the dynamically generated button controls does indeed call the b_Click method and delete the given user, however, upon deleting the page does not reload the 'new' list of users.单击任何动态生成的按钮控件确实会调用 b_Click 方法并删除给定的用户,但是,在删除页面时不会重新加载“新”用户列表。

        protected void Page_Load(object sender, EventArgs e)
        {
          DbDB db = new DbDB();
          List<User> users = db.GetUsers().ExecuteTypedList<User>();

          foreach (User u in users)
          {
            Button b = new Button();
            b.Text = u.FirstName;
            b.Click += new EventHandler(b_Click);
            PlaceHolder1.Controls.Add(b);

          }

        }
       }

       void b_Click(object sender, EventArgs e)
       {

          Button b = (Button)sender;
          DbDB.User.Delete(x => x.FirstName == b.Text);

       }
protected void Page_Load(object sender, EventArgs e) {
   LoadUsers();
}

void b_Click(object sender, EventArgs e) {      
   Button button = (Button)sender;
   string firstName = button.CommandArgument;  
   DbDB.User.Delete(x => x.FirstName == firstName);

   PlaceHolder1.Controls.Remove(button);
}

void LoadUsers() {  
   DbDB db = new DbDB();
   List<User> users = db.GetUsers().ExecuteTypedList<User>();

   foreach (User user in Users) {
      Button button = new Button();         
      button.CommandArgument = user.FirstName;  // normally the user "id" to identify the user.
      button.Text = user.FirstName;
      button.Click += new EventHandler(b_Click);
      PlaceHolder1.Controls.Add(button);
   }
}

That's because the Page_Load event is called before the Click event so when you're retrieving the list of users from the database in Page_Load the user is still in there.这是因为 Page_Load 事件是在 Click 事件之前调用的,所以当您从 Page_Load 中的数据库中检索用户列表时,用户仍然在那里。 As a quick solution you could move the code from Page_Load to PreRender event.作为一种快速解决方案,您可以将代码从 Page_Load 移动到 PreRender 事件。

Have a look at this link for more info on the page life cycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx查看此链接以获取有关页面生命周期的更多信息: http://msdn.microsoft.com/en-us/library/ms178472.aspx

You don't need to select users with every post-back.您不需要每次回发 select 用户。 Also you don't need to create controls at runtime for this.此外,您不需要在运行时为此创建控件。

Following is an alternative way.以下是另一种方式。

<asp:Repeater runat="server" ID="myRepeater">
    <ItemTemplate>
        <asp:Button runat="server" OnClick="Button_Click" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />
    </ItemTemplate>
</asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        // load users
        myRepeater.DataSource = users;
        myRepeater.DataBind();
    }
}

protected void Button_Click(object sender, EventArgs e)
{
    // delete user
    Button button = sender as Button;
    button.Visible = false;
}

Write your page_load body inside在里面写下你的 page_load 正文

if(!IsPostBack)
{
   ....
}

That should work.那应该行得通。

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

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