Formview在添加第一条记录后不让我添加第二条记录

时间:2013-06-11 19:16:58

标签: asp.net sqldatasource formview autopostback

我有一个FormView,允许将记录插入表中,然后还发送一条确认电子邮件,说明已添加记录。根据所选部门,电子邮件应该是特定的一组用户。所以我有以下内容。一切正常,除非我添加记录后,电子邮件发送,然后它将我带回来,当我再次单击添加新记录按钮时,我得到' 无法找到控件'formViewNewItem $ ddlInsertDepartment '在ControlParameter中'RoleID'。 '就像formView在记录插入表格后留在insertmode中一样。

要发送的电子邮件的数据来源:

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

FormView代码:

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

1 个答案:

答案 0 :(得分:0)

得到了答案...我必须将SqlDataSource移动到InsertItemTemplate,然后使用OnInserted代码中的FindControl方法来查找我移动的SqlDataSource,但是所有这些都应该工作了