0

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

我试过了,但它不工作

代码:

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

有任何想法吗?提前致谢

4

3 回答 3

1

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();
}
于 2013-10-01T14:04:45.617 回答
1

我相信你应该能够检查:

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

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

于 2013-10-01T13:58:48.060 回答
0

您可以在表单/控件中声明一个 bool 来存储按钮的状态,然后使用按钮的 MouseDown 和 MouseUp 事件来设置您的 bool 值。然后检查您的 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
        }
    }
}
于 2013-10-01T14:10:40.410 回答