Button控件不触发 [英] Button control not firing

查看:189
本文介绍了Button控件不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到的问题IM有点复杂,解释,所以请多多包涵。我有2个按钮控件。在页面加载,我想知道是什么按钮创建了页面加载回发。通过研究我发现下面这个片断和它的作品如预期。因此,这里是我的发生,当点击该按钮。

事件的场景

  1.单击按钮做了回传
   2.运行下面的功能,并告诉我该按钮的id
   3.运行该按钮点击事件处理程序     保护小组btnCalc_Click(BYVAL发件人为对象,BYVAL E上EventArgs的)把手btnCalc.Click
          ' 做这个
     结束小组

问题出现时,我点击第二个按钮,它确实步骤1和2,但从来不3.通过测试,我有它只做1,2和3的第一个按钮点击。我不知道为什么会这样?

 功能GetPostBackControlName()作为字符串      昏暗控制,控制=无
      昏暗ctrlname的String = Page.Request.Params(__ EVENTTARGET)    如果ctrlname<>没有AndAlso运算ctrlname<> [字符串] .Empty然后
        控制= Page.FindControl(ctrlname)
    其他
        昏暗ctrlStr作为字符串= [字符串] .Empty
        尺寸C作为控制=无        对于每个CTL作为字符串在Page.Request.Form            C = Page.FindControl(CTL)           如果TypeOf运算C是System.Web.UI.WebControls.Button然后
                控制= C
                对于出口
           万一        下一个    万一    尝试
        返回control.ID.ToString
    抓住
        返回
    结束Try    结束功能


解决方案

您知道,您可以添加多个句柄的程序?

 保护小组MultipleButtons_Click(BYVAL发件人为对象,BYVAL E上的EventArgs)_
   处理btnCalc.Click,btnOtherCalc.Click
   ' 做这个
结束小组

这样你就可以运行多个事件在同一code和使用发送者确定用了那个按钮。如果他们需要动态构建,您可以添加在运行时事件处理器,但是那是一个不同的问题。

The problem im having is a little complicated to explain, so please bear with me. I have 2 button controls. In the page load, I wanted to know what button created a postback on the page load. Through research I have found this snippet below and it works as expected. So here is my scenario of events that occur when click on the button.

   1. Click the button does a postback
   2. Runs the function below and tell me the id of the button
   3. Runs the clicked event handlers for that button

     Protected Sub btnCalc_Click(ByVal sender As Object, ByVal e As EventArgs) Handles  btnCalc.Click
          ' Do this
     End Sub

The problem comes in when I click the second button, it does steps 1 and 2 but NEVER DOES 3. Through testing, I have that it only does 1, 2, and 3 on the first button clicked. I have no clue why this is happening?

     Function GetPostBackControlName() As String

      Dim control As Control = Nothing
      Dim ctrlname As String = Page.Request.Params("__EVENTTARGET")

    If ctrlname <> Nothing AndAlso ctrlname <> [String].Empty Then
        control = Page.FindControl(ctrlname)
    Else
        Dim ctrlStr As String = [String].Empty
        Dim c As Control = Nothing

        For Each ctl As String In Page.Request.Form

            c = Page.FindControl(ctl) 

           If TypeOf c Is System.Web.UI.WebControls.Button Then
                control = c
                Exit For
           End If

        Next

    End If

    Try
        Return control.ID.ToString
    Catch
        Return ""
    End Try

    End Function

解决方案

Are you aware that you can add multiple Handles to a procedure?

Protected Sub MultipleButtons_Click(ByVal sender As Object, ByVal e As EventArgs) _
   Handles  btnCalc.Click, btnOtherCalc.Click
   ' Do this 
End Sub 

This way you can run the same code from multiple events and use the sender to determine which button was used. If they need to be built dynamically you could add eventhandlers at run time but thats a different question.

这篇关于Button控件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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