user2500094
user2500094

Reputation: 1103

How to check if button is clicked or not in method?

I want to check if button is clicked or not in method

I tried but it's not working

Code:

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

Any ideas? Thanks in advance

Upvotes: 0

Views: 8698

Answers (3)

Scope Creep
Scope Creep

Reputation: 565

You could declare a bool in your form/control to store the state of the button then use the MouseDown and MouseUp events of the button to set your bool value. Then check the value of the bool in your LoadViewState method.

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
        }
    }
}

Upvotes: 0

Sean Airey
Sean Airey

Reputation: 6372

The btnAddExperience_Click event will be executed well after the LoadViewState event (perhaps reading this article would be beneficial).

I would suggest using a form field to indicate that this button has been clicked:

<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" />

Then in code behind:

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

    bool btnAddExperienceClicked = bool.Parse(Request.Form["hidButtonClicked"]);

    if (btnAddExperienceClicked == true)   
    {
        GenerateControls();
    } 

    GenerateControls1();
}

Upvotes: 1

Brian Mains
Brian Mains

Reputation: 50728

I believe you should be able to check:

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

To do that, which the target has the unique ID of the control, and the argument has the event args. I've used that before; however, not everything may set these values. Also, button click event won't fire until after LoadViewState, so you can't put something in there to set a boolean value.

Upvotes: 1

Related Questions