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.