I have a FormView which allows inserting of records into a table, and then also sends out a confirmation email that a record has been added. The email is supposed to go a particular set of users, based on a department selected. So I have the below. All works well, except after I add a record, and the email sends, and then it takes me back, and when I click the Add new record button again I get 'Could not find control 'formViewNewItem$ddlInsertDepartment' in ControlParameter 'RoleID'.' It's like the formView is staying in insertmode or something after a record is inserted into the table.
Data source for emails to be sent:
<asp:SqlDataSource ID="dsourceToEmails" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT [Email] + ',' FROM [Users] WHERE [Role] = @RoleID FOR XML PATH ('')">
</asp:SqlDataSource>
FormView code:
<asp:FormView ID="formViewNewItem" runat="server" DataKeyNames="Test_ID"
DataSourceID="testDataSource" OnDataBound="formViewNewItem_DataBound"
OnItemInserted="testRecord_Inserted">
<InsertItemTemplate>
<asp:DropDownList ID="ddlInsertDepartment" runat="server" ClientIDMode="Static">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Accounting</asp:ListItem>
<asp:ListItem>Marketing</asp:ListItem>
<asp:ListItem>Programming</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Button ID="btnAddNewRecord" runat="server" CausesValidation="False"
CommandName="New" Font-Bold="True" Text="Add New"
onclick="btnAddNewRecord_Click" />
</ItemTemplate>
</asp:FormView>
Page Load (only create parameter for email if in insert mode)
protected void Page_Load(object sender, EventArgs e)
{
if (formViewNewItem.CurrentMode == FormViewMode.Insert)
{
// Create your ControlParameter
ControlParameter deptParam = new ControlParameter();
deptParam.ControlID = formViewNewItem.FindControl("ddlInsertDepartment").UniqueID;
deptParam.PropertyName = "SelectedValue";
deptParam.Name = "RoleID";
deptParam.Type = TypeCode.String;
// Add it to your SelectParameters collection
dsourceToEmails.SelectParameters.Add(deptParam);
}
OnInserted, once record is successfully added, send email:
protected void testRecord_Inserted(object sender, EventArgs e)
{
DataView dvToEmails = (DataView)dsourceToEmails.Select(DataSourceSelectArguments.Empty);
string emailsTo = (string)dvToEmails.Table.Rows[0][0];
emailsTo = emailsTo.TrimEnd(',');
string networkUserName = Page.User.Identity.Name;
string emailUserName = networkUserName.Substring(12);
DropDownList departmentSelection = (DropDownList)formViewNewItem.FindControl("ddlInsertDepartment");
var message = new MailMessage();
var client = new SmtpClient();
message.From = new MailAddress("test@abc.com", "Task Tracker");
message.To.Add(emailsTo);
message.Subject = "New Project assigned for " + departmentSelection.SelectedValue;
message.Body = "A new task has been assigned.
<br /><br /><b>Entered by:</b> " + emailUserName;
message.IsBodyHtml = true;
client.Send(message);
}
Got the answer to this...I had to move the SqlDataSource to the InsertItemTemplate, and then use the FindControl method in the OnInserted code to find the SqlDataSource that I had moved, but all worked as it should after all of that.