0
votes

I have created web-part form with sendMail function.

        protected void btnSubmitForm_Click(object sender, EventArgs e)
    {
          SendEmail();
          ClearFields();
    }

But when page loads mail sends too(with the last fields data), not only by clicking button... Could someone knows solution? Thanks very much!

I have created CustomControl and added them to webpart. Web part i have added to the page layout using . Code Behind and html code is in the CustomControl:

protected void Page_Load(object sender, EventArgs e) {

        if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
        {

            foreach (BaseValidator val in Page.GetValidators("Form"))
            {
                val.Enabled = false;
            }


            btnSubmitForm.Enabled = false;

        }
    }

    protected void btnSubmitForm_Click(object sender, EventArgs e)
    {
        try
        {
            SendEmail();
        }
        catch (Exception ex)
        {
            ExceptionHelper.LogException(ex);
        }

        ClearFields();


    }
2
can you please post all the webpart code?Maks Matsveyeu
I have created CustomControl and added them to webpart. Web part i have added to the page layout using <WebPartPages:WebPartZone />.Serhio g. Lazin

2 Answers

0
votes

You should take IsPostback in consideration. Also, see this answer.

0
votes

I have found a solution. i have added the next code:

if (!String.IsNullOrEmpty(txtName.Text))
            {
                try
                {
                    SendEmail();
                }
                catch (Exception ex)
                {
                    ExceptionHelper.LogException(ex);
                }

                ClearFields();
                Response.Redirect("#");


            } 

It works for me, because after redirect to the same page, mail doesn't sends after every refresh, just by submitting the form. Maybe this is not pretty solution, but it works!

P.S You also can use this redirect Response.Redirect(Request.Url.ToString()) instead of previous