如何检查是否在方法中单击按钮

本文关键字:方法 单击 按钮 是否 何检查 检查 | 更新日期: 2023-09-27 18:33:23

我想检查是否在方法中单击了按钮

我试过了,但它不起作用

法典:

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    //Here If (btnAddExperience_Click()  is clicked)   
    {
    GenerateControls();
    } 
    GenerateControls1();
}

private void GenerateControls()
{
    foreach (string i in NoOfControls)
    {
        VisaUserControl ctrl = (VisaUserControl)Page.LoadControl("VisaUserControl.ascx");
        ctrl.ID = i;
        this.rpt1.Controls.Add(ctrl);
        rpt1.Controls.Add(new LiteralControl("<BR>"));
    }
}
protected void btnAddVisa_Click(object sender, EventArgs e)
{
    Button thisButton = (Button)sender;
    List<string> temp = null;
    var uc = (VisaUserControl)this.LoadControl(@"VisaUserControl.ascx");
    string id = Guid.NewGuid().ToString();
    uc.ID = id;
    temp = NoOfControls;
    temp.Add(id);
    NoOfControls = temp;
    rpt1.Controls.Add(uc);
    rpt1.Controls.Add(new LiteralControl("<BR>"));
}
private void GenerateControls1()
{
    foreach (string i in NoOfControls)
    {
        ExperienceUserControl ctrl = (ExperienceUserControl)Page.LoadControl("ExperienceUserControl.ascx");
        ctrl.ID = i;
        this.rpt1.Controls.Add(ctrl);
        rpt2.Controls.Add(new LiteralControl("<BR>"));
    }
}
protected void btnAddExperience_Click(object sender, EventArgs e)
{
    List<string> temp = null;
    var uc = (ExperienceUserControl)this.LoadControl(@"ExperienceUserControl.ascx");
    string id = Guid.NewGuid().ToString();
    uc.ID = id;
    temp = NoOfControls;
    temp.Add(id);
    NoOfControls = temp;
    rpt2.Controls.Add(uc);
    rpt2.Controls.Add(new LiteralControl("<BR>"));
}

有什么想法吗?提前感谢

如何检查是否在方法中单击按钮

我相信你应该能够检查:

Request.Form.Get("__EVENTTARGET")
Request.Form.Get("__EVENTARGUMENT")

为此,目标具有控件的唯一 ID,参数具有事件参数。 我以前用过;但是,并非所有内容都可以设置这些值。 此外,按钮单击事件在 LoadViewState 之后才会触发,因此您不能在其中放置一些东西来设置布尔值。

btnAddExperience_Click 事件将在LoadViewState事件之后执行(也许阅读本文会有所帮助)。

我建议使用表单字段来指示已单击此按钮:

<script type="text/javascript">
    function setButtonClickedField()
    {
        document.getElementById("hidButtonClicked").value = "true";
    }
    function resetButtonClickedField()
    {
        document.getElementById("hidButtonClicked").value = "false";
    }
    // use this or a cross-browser attach event to attach the reset to the window/document onload event
    var tempFunction;
    if (window.onload)
    {
        tempFunction = window.onload;
    }
    window.onload = function()
    {
        if (tempFunction)
        {
            tempFunction();
        }
        resetButtonClickedField();
    }
</script>
<input type="hidden" id="hidButtonClicked" name="hidButtonClicked" value="false" />
<asp:Button runat="server" ID="btnAddExperience" OnClick="btnAddExperience_Click" OnClientClick="setButtonClickedField" Text="Add Experience" />

然后在代码隐藏中:

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    bool btnAddExperienceClicked = bool.Parse(Request.Form["hidButtonClicked"]);
    if (btnAddExperienceClicked == true)   
    {
        GenerateControls();
    } 
    GenerateControls1();
}

可以在窗体/控件中声明一个布尔值来存储按钮的状态,然后使用按钮的 MouseDown 和 MouseUp 事件来设置布尔值。 然后在 LoadViewState 方法中检查布尔值。

public partial class Form1 : Form
{
    bool buttonClicked = false;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        buttonClicked = true;
    }
    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        buttonClicked = false;
    }
    private void LoadViewState()
    {
        if (buttonClicked)
        {
            //Do something
        }
    }
}