0

I know this is a very basic question, and I am sure there are answers for it already on the forum. I don't even know how to properly phrase it to search, so here goes. I have button that has the OnClick property set to "InsertCard" and the corresponding code of:

protected void InsertCard (object source, EventArgs e) {
SqlDataSource1.Insert();
}

This works exactly as intended. I also want to clear all the text boxes on the page after the button is clicked. If I change the OnClick property to "ClearText" and have the corresponding code of:

protected void ClearText_Click(object sender, EventArgs e)
{
    ClearControls();

}
private void ClearControls()
{
    foreach (Control c in Page.Controls)
    {
        foreach (Control ctrl in c.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Text = string.Empty;
            }
        }
    }
}

This works perfectly too.

My question is how do I combine both actions into one, so that when I click the button, I insert the data to the database AND then clear all fields? I have tried many variations but always get some type of compiler error.

Also, is there some type of guide to assist me in using the right syntax for asking questions/searching for answers?

Dkone
  • 11
  • 1
  • 2

2 Answers2

5

Why can't you just call both functions in your button click response?

protected void InsertCard (object source, EventArgs e) {
    SqlDataSource1.Insert();
    ClearControls();
}

private void ClearControls()
{
    foreach (Control c in Page.Controls)
    {
        foreach (Control ctrl in c.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Text = string.Empty;
            }
        }
    }
}

Is this what you are asking? If not you may need to be more specific.

JeremyK
  • 1,075
  • 1
  • 22
  • 45
  • Jeremy, yes I did copy and paste from other examples and the more I do so, the more I understand about C#. Your answer worked, I just didn't know the proper syntax for combing actions, now thanks to you showing me I do. Now protected and private make much more sense. I assume I can just keep adding privates and calling them from the protected section? – Dkone Feb 05 '15 at 19:39
  • @Dkone See this for an explanation of public, private, protected: http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing – JeremyK Feb 05 '15 at 19:46
  • That was very helpful, especially the table someone made up. I think I understand it a little. So if I put some given code, whether it be public/private/protected at the top of my web page, it can only be called from 'that' webpage, but if I put it in the code behind page, then any page that references that code behind page can use the code? – Dkone Feb 05 '15 at 20:27
2

If I have not miss read the question just call the other function after you have inserted your records.

protected void InsertCard (object source, EventArgs e) {
   SqlDataSource1.Insert();
   ClearControls();
}

private void ClearControls()
{
    foreach (Control c in Page.Controls)
    {
        foreach (Control ctrl in c.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Text = string.Empty;
            }
        }
    }
}
Stig
  • 1,169
  • 1
  • 9
  • 12