0

我有一个 FormView,它允许将记录插入表中,然后还发送一封确认电子邮件,说明已添加记录。根据选择的部门,该电子邮件应该发送给一组特定的用户。所以我有以下。一切正常,除了我添加了一条记录,电子邮件发送,然后它带我回来,当我再次单击添加新记录按钮时,我得到'在 ControlParameter'RoleID' 中找不到控件'formViewNewItem$ddlInsertDepartment' . ' 这就像在将记录插入表后 formView 处于插入模式或其他状态。

待发送邮件的数据源:

<asp:SqlDataSource ID="dsourceToEmails" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" 
            SelectCommand="SELECT [Email] + ',' FROM [Users] WHERE [Role] = @RoleID FOR XML PATH ('')">
        </asp:SqlDataSource>

窗体视图代码:

<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>

页面加载(仅在插入模式下为电子邮件创建参数)

 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,一旦成功添加记录,发送电子邮件:

    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);
        }
4

1 回答 1

0

得到了这个问题的答案...我不得不将 SqlDataSource 移动到 InsertItemTemplate,然后使用 OnInserted 代码中的 FindControl 方法来找到我移动的 SqlDataSource,但所有这些都正常工作。

于 2013-06-11T20:18:12.047 回答