开发者

On ASP.NET form submit it throws an Object reference not set to an instance of an object.

开发者 https://www.devze.com 2023-04-13 01:27 出处:网络
I have a simple email form written in ASP.NET with the logic in the codebehind file. It\'s all in C# (the logic that is...). Anyways, on page load I have the following:

I have a simple email form written in ASP.NET with the logic in the codebehind file. It's all in C# (the logic that is...). Anyways, on page load I have the following:

protected void Page_Load(object sender, EventArgs e)
{
    RequestorName.Text = Request.Form["UserName"].ToString();
    RequestorTitle.Text = Request.Form["JobTitle"].ToString();
    RequestorEmail.Text = Request.Form["Email"].ToString();
    RequestorPhone.Text = Request.Form["Phone"].ToString();
    RequestorAddress1.Text = Request.Form["Address"].ToString();
    RequestorAddress2.Text = Request.Form["City"].ToString() + " " + Request.Form["State"].ToString() + ", " + Request.Form["Zip"].ToString();

}

This works great as it pulls the users information into a few fields so they don't have to fill everything out by hand.

My other 2 methods in the code behind:

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

protected void SendEmail()
{
    try
    {
        //compose email and send

    }
    catch (Exception ex)
    {
        ErrorMessage.Text = "Error: " + ex.ToString();

    }
}

On my form page the button code is this:

<center>
                <asp:Button runat="server" Text="Submit" ID="Submit" OnClick="SubmitForm_Click" class="button red" />
</center>

The error occurs when I click the send button on the form that generates the email and sends it. I can remove the Page_Load code and works great but I'd like to keep it there so the user doesn't have to fill out as much information.

I've used my Google Fu and read a ton of threads/articles but can't seem to find the soluti开发者_Python百科on...

Thanks for any assistance.


Add check for IsPostBack:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack) 
  {
    RequestorName.Text = Request.Form["UserName"].ToString();
    RequestorTitle.Text = Request.Form["JobTitle"].ToString();
    RequestorEmail.Text = Request.Form["Email"].ToString();
    RequestorPhone.Text = Request.Form["Phone"].ToString();
    RequestorAddress1.Text = Request.Form["Address"].ToString();
    RequestorAddress2.Text = Request.Form["City"].ToString() + " " +    Request.Form["State"].ToString() + ", " + Request.Form["Zip"].ToString();
  }

}


Have you tried adding if (Page.IsPostBack == false) to your Page_Load event?

I assume that the Request.Form code comes from fields that the user has filled out, but without seeing the rest of your markup, I'm not sure why you'd have to re-assign values from the form to what appear to be other fields on the form.


Where specifically is the error occurring? From your code, I'm assuming that you are posting to your email form from another page and passing the parameters across.

If that's the case then assuming your .Text are the page controls then you should look at containing the control fillers in an If(!IsPostback) {...} for the first loading of the page only. Then your email code can read from the local controls.

My guess is that the "Request.Form[..." items are probably the ones kicking back error on postback.

HTH Dave

0

精彩评论

暂无评论...
验证码 换一张
取 消