我创建了一个动态创建的FromView,其中包含用户详细信息列表。
<asp:Panel ID="DetailsPanel" runat="server" CssClass="formview">
<br />
<asp:FormView ID="FormView1" runat="server" DataSourceID="DetailsDataSource" RenderOuterTable="false"
OnPreRender="FormView1_PreRender" OnModeChanging="FormView1_ModeChanging" OnItemUpdated="FormView1_ItemUpdated"
OnItemInserted="FormView1_ItemInserted" OnItemDeleted="FormView1_ItemDeleted" OnItemCommand="FormView1_ItemCommand">
<HeaderTemplate>
<table id="detailsTable" class="DDDetailsTable" cellpadding="6" />
</HeaderTemplate>
<ItemTemplate>
<tr class="td">
<td class="DDLightHeader">Order No</td>
<td><asp:DynamicControl ID="OrderNo" runat="server" DataField="OrderNo" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">User ID</td>
<td><asp:DynamicControl runat="server" DataField="UserID" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Display Name</td>
<td><asp:DynamicControl runat="server" DataField="DisplayName" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Email Address</td>
<td><asp:DynamicControl ID="EmailAddress" runat="server" DataField="EmailAddress" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Project Code</td>
<td><asp:DynamicControl runat="server" DataField="ProjectCode" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Event Description</td>
<td><asp:DynamicControl runat="server" DataField="EventDesc" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Delivery Date</td>
<td><asp:DynamicControl runat="server" DataField="DeliveryDate" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Date Ordered</td>
<td><asp:DynamicControl runat="server" DataField="DateOrdered" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Site</td>
<td><asp:DynamicControl runat="server" DataField="Site" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Time</td>
<td><asp:DynamicControl runat="server" DataField="Time" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Time Room is Available</td>
<td><asp:DynamicControl ID="DynamicControl1" runat="server" DataField="TimeRoomAvailable" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Time Room to be Cleared</td>
<td><asp:DynamicControl ID="DynamicControl2" runat="server" DataField="TimeCleared" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Location</td>
<td><asp:DynamicControl runat="server" DataField="Location" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Additional Information</td>
<td><asp:DynamicControl runat="server" ID="AdditionalInfo" DataField="AdditionalInfo" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Item Costs</td>
<td><asp:DynamicControl runat="server" ID="ItemCosts" DataField="ItemCosts" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">AdditionalCosts</td>
<td><asp:DynamicControl runat="server" ID="AdditionalCosts" DataField="AdditionalCosts" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Total Costs</td>
<td><asp:DynamicControl runat="server" DataField="TotalCosts" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">More Information Required</td>
<td><asp:DynamicControl runat="server" DataField="MoreInformationRequired" UIHint="MultilineText" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Reason Rejected</td>
<td><asp:DynamicControl runat="server" DataField="ReasonRejected" UIHint="MultilineText" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Order Progress</td>
<td><asp:DynamicControl runat="server" DataField="OrderProgress" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Imported Into Dream</td>
<td><asp:DynamicControl runat="server" DataField="ImportedIntoDream" /></td>
</tr>
<tr class="td">
<td colspan="2">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete"
OnClientClick='return confirm("Are you sure you want to delete this item?");' />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr class="td">
<td class="DDLightHeader">Order No</td>
<td><asp:DynamicControl ID="OrderNo" runat="server" DataField="OrderNo" Mode="ReadOnly"/></td>
</tr>
<tr class="td">
<td class="DDLightHeader">User ID</td>
<td><asp:DynamicControl runat="server" DataField="UserID" Mode="ReadOnly"/></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Display Name</td>
<td><asp:DynamicControl runat="server" DataField="DisplayName" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Email Address</td>
<td><asp:DynamicControl runat="server" DataField="EmailAddress" ID="Email"/></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Project Code</td>
<td><asp:DynamicControl runat="server" DataField="ProjectCode" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Event Description</td>
<td><asp:DynamicControl runat="server" DataField="EventDesc" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Delivery Date</td>
<td><asp:DynamicControl runat="server" DataField="DeliveryDate" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Date Ordered</td>
<td><asp:DynamicControl runat="server" DataField="DateOrdered" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Site</td>
<td><asp:DynamicControl runat="server" DataField="Site" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Time</td>
<td><asp:DynamicControl runat="server" DataField="Time" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Time Room is Available</td>
<td><asp:DynamicControl ID="DynamicControl1" runat="server" DataField="TimeRoomAvailable" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Time Room to be Cleared</td>
<td><asp:DynamicControl ID="DynamicControl2" runat="server" DataField="TimeCleared" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Location</td>
<td><asp:DynamicControl runat="server" DataField="Location" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Additional Information</td>
<td><asp:DynamicControl runat="server" DataField="AdditionalInfo" Mode="ReadOnly" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Item Costs</td>
<td><asp:DynamicControl runat="server" ID="ItemCosts" DataField="ItemCosts" Mode="Edit" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Additional Costs</td>
<td><asp:DynamicControl runat="server" ID="AdditionalCosts" DataField="AdditionalCosts" Mode="Edit" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Total Costs</td>
<td><asp:DynamicControl runat="server" ID="TotalCosts" DataField="TotalCosts" Mode="Edit" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">More Information Required</td>
<td><asp:DynamicControl runat="server" DataField="MoreInformationRequired" Mode="Edit" UIHint="MultilineText" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Reason Rejected</td>
<td><asp:DynamicControl runat="server" DataField="ReasonRejected" Mode="Edit" UIHint="MultilineText" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Order Progress</td>
<td><asp:DynamicControl runat="server" DataField="OrderProgress" Mode="Edit" UIHint="OrderProgressDropDown" /></td>
</tr>
<tr class="td">
<td class="DDLightHeader">Imported Into Dream</td>
<td><asp:DynamicControl runat="server" DataField="ImportedIntoDream" Mode="Edit" /></td>
</tr>
<tr class="td">
<td colspan="2">
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DynamicEntity ID="DynamicEntity3" runat="server" Mode="Insert" />
<tr class="td">
<td colspan="2">
<asp:LinkButton ID="LinkButton6" runat="server" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="LinkButton7" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
</td>
</tr>
</InsertItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:FormView>
当我点击更新时,我希望能够根据表单视图中的电子邮件地址向用户发送电子邮件。
我在后面的代码中修改了FormView1_Updated,如下所示:
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
string id = e.Keys["OrderNo"].ToString();
// I have tried this
//string email = FormView1.FindControl("EmailAddress").ToString();
//I have also tried this
//DynamicControl emailadd = ((DynamicControl)FormView1.FindControl("Email"));
//string email = emailadd.ToString();
GridView1.DataBind();
MailMessage objMail = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
objMail.To.Add(email);
objMail.From = new MailAddress("xxxx@xxxxxxx.xx.xx");
objMail.Subject = "Catering Request";
objMail.IsBodyHtml = true;
objMail.Body = "<html><body>" +
"<p><font face='Verdana' color='#000080' font size='2'>Your catering request, order number " + id + " has been updated by catering. Please view the " +
"<a href='http://dev-catering.bathspa.ac.uk/'>Catering Request System</a> for the details.</font></p>";
smtpClient.Port = xxxx;
smtpClient.Host = "xxxx.xxxxxxxxx.com";
NetworkCredential nc = new NetworkCredential("xxxxxx", "xxxxxxxxxxxxxxxx");
//SmtpClient smtpClient = new SmtpClient("xxxxxxxxxx.xxxxxxxxxx.xx.xx");
smtpClient.Credentials = nc;
smtpClient.Send(objMail);
}
如果我直接在objMail.To.Add(电子邮件)字段中输入电子邮件地址,这可以正常工作。我的问题是我无法在FormView中捕获电子邮件地址的值。我一直收到一个空错误。在“ItemUpdated”之前,电子邮件地址的价值是否已被销毁?被称为。
或者我没有找到价值,我只是找到了控件。如果是这样,我如何找到动态创建的控件的值。
非常感谢 克莱尔