0

So I'm creating a contact form for my event management system at the moment.

I've got a Visual Webpart including

private string selectedEmail;

as a variable after declaring my class in code behind.

So there's a dropdown in my aspx which gets the attendees for an event on page_load and sets the string selectedEmail via SelectedIndexChanged-Event.

Here's the important part of my code:

        protected void ddlAttendees_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            selectedEmail = "";
            selectedAttendee = ddlAttendees.SelectedItem.ToString();

            attendee = //(linq-query for my actual attendees list, definitely working);

            if (attendee != null)
            {
                if (attendee["E-Mail"] != null)
                {
                    ...
                    selectedEmail = attendee["E-Mail"].ToString();
                    lblResults.Text = "Chosen E-Mail Adress: " + selectedEmail;
                }
                else
                {
                    ErrorLabel.Text = "[errormessage, no mail]";
                }
            }
            else
            {
                ...
                lblResults.Text = "errormessage, nothing chosen";
            }
        }
        catch (Exception ex)
        {
            ...
        }
    }

So, my lblResults shows me the actual E-Mailadress of the attendee I chose like "chosen E-Mail address: [email protected]". Yay! But... Now I have a "send"-Button with the following click method in my form, too:

protected void sendButton_Click(object sender, EventArgs e)
    {
        try
        {
            ...
            if (selectedEmail != null)
            {
                SendMailOverSharePoint(selectedEmail, subjectBox.Text, messageBox.Text);
                lblResults.Text = "E-Mail to" + selectedAttendee + " sent succesfully.";

            }
            else 
            {
                lblResults.Text = "Sending not possible. No Mail-Address in List?!";
            }
        }
        catch (Exception ex)
        {
            ...
        }
    }

So this is my problem: I get the address in the SelectedIndexChanged-method, but when I want to use it in my click-method, it seems my string is null. Tried it via !string.isnullorempty(selectedEmail) too, but same behaviour. I'm out of ideas for the moment, I searched my whole code, but these are the only occurences of the variable so far. Hope someone could help!

P.S.: The Mailadress is definitely in my attendee-List. For Bulk-Email I get all attendees' Mailadresses for an event and send mails to them, and this works.

2
  • is there any postback occuring and have you debugged line by line to see what happens to selectedEmail?
    – Ali Jafer
    Commented Jan 7, 2013 at 17:21
  • the ddlAttendees-dropdown has autopostback="True" set. I'll debug tomorrow, it's nearly 7pm here in germany and i'm tired ;) Good evening.
    – Dominik
    Commented Jan 7, 2013 at 17:25

2 Answers 2

1

In my experience variables in code like that do not maintain their value across postbacks. When the page is initally posted back through your SelectedIndexChanged event, you set the variable's value (and use it to provide data to your label), but as soon as the page is rendered and sent back to the client, poof!, the executing code is gone.

The way I see it, you have two easy options (maybe more, but these are the two approaches that popped into my head first):

1 - include a hidden field in your web part to store the email address. When the page posts back from the SelectedItemChanged event, store the email address in the hidden field, so that it will travel with the page back to the client, and when the user clicks the Send button (causing another postback), you can pull the value from the hidden field in your click event handler.

2 - in the click event handler, repeat the process of finding out the email address of the drop-down list's selected item, as the value of the selected item will not have changed:

selectedEmail = "";
selectedAttendee = ddlAttendees.SelectedItem.ToString();

attendee = //(linq-query for my actual attendees list, definitely working);

if (attendee != null)
{
    if (attendee["E-Mail"] != null)
    {
        ...
        selectedEmail = attendee["E-Mail"].ToString();

        // send the email
        // etc.

Personally I think option 1 is easier, since you won't have to re-run the LINQ query, etc.

By the way, I think this is technically an ASP.NET question, and not a SharePoint question, per se... ;)

1

Once page is posted back and when you click on button the page life cycle starts from first event. My suggestion is to use viewstate variable. It will maintain your stored value.

same thing as you did for string selectedEmail. Instead of string use ViewState.

e.g.

 ViewState["selectedEmail"]=Convert.ToString(attendee["E-Mail"]);

now you can use stored value of ViewState variable like

 string UserEmail =Convert.ToString(ViewState["selectedEmail"]);
 SendMailOverSharePoint(UserEmail, subjectBox.Text, messageBox.Text);

Thanks!!

5
  • Seems a better solution. could you perhaps give me advice how to save my selectedEmail to viewstate the right way? I didn't find a good tutorial yet (but still searching)
    – Dominik
    Commented Jan 8, 2013 at 13:40
  • i have updated my answer pls refer it. Commented Jan 8, 2013 at 13:57
  • Just as a clarification, the first method I described in my answer, using a hidden field, is essentially using viewstate. All input controls (a hidden field is technically an input control) are automatically stored in viewstate by ASP. Commented Jan 8, 2013 at 15:39
  • Yes you are right Cristy. I just suggested it bcz it can directly be used without defining hidden filed...Thanks :D Commented Jan 9, 2013 at 5:22
  • i used the hiddenfield approach from the first answer now, and it works like a charm, so I won't change to viewstate. but nice to know ;)
    – Dominik
    Commented Jan 10, 2013 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.