简体   繁体   English

按钮不以编程方式触发ASP.NET控件上的事件

[英]Button not firing event on ASP.NET control Programmatically

I'm creating a Button on a ComboxChanged Event, 我正在ComboxChanged事件上创建一个Button,

    Button btn = new Button();
    btn.ID = "btnNum1";
    btn.Text = "Edit";
    btn.Click += btnTest_Click;        
    pnl.Controls.Add(btn);

The event code is as followed 事件代码如下

 public void btnTest_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    Response.Write(btn.ID);        
    Response.Redirect("Page2.aspx");
}

I have tried, both Response.Write and Response.Redirect . 我试过了Response.WriteResponse.Redirect None of them, works. 他们都没有,有效。 Same page gets refreshed. 同一页面刷新。 Does any one has any idea. 有没有人有任何想法。

Button has to be created Dynamically, hence I can't try the Page_Init 必须动态创建Button,因此我无法尝试Page_Init

I have also tried the CommandArgument event that also didn't work. 我也尝试过CommandArgument事件也没有用。 Any idea. 任何想法。

As Mt. 作为山 Schneiders has mentioned in his comments under your question - if you dynamically add a control to a page, you must re-add it on the post-back. Schneiders在他的评论中提到了你的问题 - 如果你动态地向页面添加一个控件,你必须在回发后重新添加它。 Just because you've added it as part of the event handler, does not mean that ASP.NET automatically creates the control on the post-back. 仅仅因为您已将其添加为事件处理程序的一部分,并不意味着ASP.NET会自动在回发后创建控件。

You need to store in the page the fact that the control was created - my personal preference would be to put the code in it's own function and set a ViewState value... 您需要在页面中存储控件创建的事实 - 我个人的偏好是将代码放入其自己的函数中并设置ViewState值...

private void CreateButton()
{
  Button btn = new Button();
  btn.ID = "btnNum1";
  btn.Text = "Edit";
  btn.Click += btnTest_Click;        
  pnl.Controls.Add(btn);
  ViewState["buttonAdded"] = true;
}

And call the function from your ComboxChanged event. 并从您的ComboxChanged事件中调用该函数。

Then, on the Page_Load , you need to check to see if the button was added previously... 然后,在Page_Load ,您需要检查以前是否添加了按钮...

protected void Page_Load(object sender, EventArgs e)
{
  if(Page.IsPostBack && ViewState["buttonAdded"] != null)
  {
    CreateButton()
  }
}

(Note, I've stated Page_Load rather than Page_Init because the ViewState is not created at the Page_Init stage of the ASP.NET page life cycle ) (注意,我已经声明了Page_Load而不是Page_Init因为ViewState不是在ASP.NET页面生命周期Page_Init阶段创建的)

when u added ur dynamic event handler it will store in particular control view state so when the page post back happened it will lose that event handler so u can implement load view state method which will come with page events code should be like 当你添加了你的动态事件处理程序时,它会存储特定的控件视图状态,所以当页面发布回来时它将丢失该事件处理程序,所以你可以实现加载视图状态方法,它将带有页面事件代码应该像

protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);

    } 

Try this btn.Click += new EventHandler(btnTest_Click); 试试这个btn.Click += new EventHandler(btnTest_Click); .

Button btn = new Button();
btn.ID = "btnNum1";
btn.Text = "Edit";
btn.Click += new EventHandler(btnTest_Click);     
pnl.Controls.Add(btn);

Assume this is the code that create the button : 假设这是创建按钮的代码:

 If ddl.SelectedIndex > 0 Then
        Dim b As New Button
        b.ID = "mybutton"
        b.Text = "i'm a button"
        panel.Controls.Add(b)
        AddHandler b.Click, AddressOf Button_Click

    End If

Note the AddHandler ok it is exactly what you're looking for then 注意AddHandler确定它正是你正在寻找的

  Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim b As Button = CType(sender, Button)
    Dim l As Label = CType(Me.FindControl("testlabel"), Label)
    l.Text = b.Text & " my id is " & b.ID.ToString

End Sub

that's all :) 就这样 :)

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

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