问题描述
我是一个新的ASP.NET开发人员,我开发我公司基于网络的建议箱计划,使员工可以提交他们有任何的安全建议。现在,我在做这个系统的管理部分。
I am a new ASP.NET developer and I am developing a web-based suggestions box program for my company where the employees can submit any safety suggestions they have. Now, I am working on the Administration part of this system.
管理员将能够看到与店主的用户名的GridView控件中列出的所有建议。在GridView的最后一栏,状态会被列出。当这些建议,新的弹出窗口(asp.net阿贾克斯ModalPopUpExtender)之一的地位管理员点击将上市,如所有可能的状态来出现了:付诸行动,批准...等当管理员选择的这些状态之一,建议的状态将在数据库中更新。
一切工作正常。我想现在要做的就是当用户更新的建议,任何人的状态,电子邮件notfication将关于他的建议的状态更新发送给主人。 我已经写了邮件功能,但我不知道如何获得所选择建议,它的地位已经被更新的用户名。谁能帮我这个?
The Admin will be able to see all suggestions listed in a GridView control with the username of the owner. In the last column of the GridView, the status will be listed there. When the Admin clicks on the status of one of these suggestion, a new pop-up window (asp.net ajax ModalPopUpExtender) will be appeared with listing all the possible status such as: actioned, approved... etc. And when the Admin selects one of these status, the status of the suggestion will be updated in the database.Everything works fine. What I want to do now is when the user updates the status of anyone of the suggestions, an email notfication will be sent to the owner regarding the update of status of his suggestion. I already wrote the Mail function but I don't know how to get the username of that selected suggestion that its status has been updated. Could anyone help me with this?
我真的获得该更新的建议的用户名苦苦挣扎。
I am really struggling with getting the username of that updated suggestion.
仅供参考,我有以下的数据库设计:
FYI, I have the following database design:
Employee Table: Username, Name...
SafetySuggestionsLog: ID, Title, Description, Username, StatusID
SafetySuggestionsStatus: ID, Status
ASP.NET code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
width="900px" CssClass="mGrid"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
<HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="DivisionShortcut"
SortExpression="DivisionShortcut" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
OnClick="lnkSuggestionStatus_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:HyperLinkField HeaderText="Status"
SortExpression="Status" />--%>
</Columns>
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:Button runat="server" ID="btnModalPopUp" style="display:none" />
<AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
runat="server"
TargetControlID="btnModalPopUp"
PopupControlID="pnlPopUp"
BackgroundCssClass="popUpStyle"
PopupDragHandleControlID="panelDragHandle"
OkControlID="OKButton">
</AjaxToolkit:ModalPopUpExtender>
<asp:Panel runat="server" ID="pnlPopUp">
<asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
RepeatLayout="Table" TextAlign="Left" DataSourceID="SuggestionStatusDataSource"
DataTextField="Status" DataValueField="ID">
<asp:ListItem id="option1" runat="server" Value="ACTIONED" />
<asp:ListItem id="option2" runat="server" Value="APPROVED" />
<asp:ListItem id="option3" runat="server" Value="PENDING" />
<asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
</asp:RadioButtonList>
<asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>
<asp:Button ID="confirmButton" runat="server" Text="Confirm"
OnClientClick="javascript:return confirm('Are you sure you want to send an email notification about the safety suggestion to the owner?')"
OnClick="btnSendStatus_Click" />
<asp:Button ID="OKButton" runat="server" Text="Close" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
code-背后:
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
{
LinkButton lnkSuggestionStatus = sender as LinkButton;
//var safetySuggestionsId =
//get reference to the row selected
GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;
//set the selected index to the selected row so that the selected row will be highlighted
GridView1.SelectedIndex = gvrow.RowIndex;
//This HiddenField used to store the value of the ID
HiddenField1.Value = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
//show the modalPopUp
modalPopUpExtender1.Show();
}
public void btnSendStatus_Click(object sender, EventArgs e) {
var statusID = StatusList.SelectedValue;
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
//For updating the status of the safety suggestion
string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
{
cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
cmd.ExecuteNonQuery();
}
//reset the value of hiddenfield
HiddenField1.Value = "-1";
}
GridView1.DataBind();
SendSuggestionStatusToUser(statusID);
}
protected void SendStatusByEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("MAIL.Aramco.com");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]", "PMOD Safety Services Portal (PSSP)");
// In case the mail system doesn't like no to recipients. This could be removed
//msg.To.Add("[email protected]");
msg.Bcc.Add(toAddresses);
msg.Subject = MailSubject;
msg.Body = MessageBody;
msg.IsBodyHtml = isBodyHtml;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
}
protected void SendSuggestionStatusToUser(string status)
{
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
var sbEmailAddresses = new System.Text.StringBuilder(2000);
string statusID = status;
// Open DB connection.
conn.Open();
string cmdText2 = "SELECT Username FROM dbo.SafetySuggestionsLog";
using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
var sName = reader.GetString(0);
if (!string.IsNullOrEmpty(sName))
{
if (sbEmailAddresses.Length != 0)
{
sbEmailAddresses.Append(",");
}
// Just use the ordinal position for the user name since there is only 1 column
sbEmailAddresses.Append(sName).Append("@aramco.com");
}
}
}
reader.Close();
}
string cmdText3 = "UPDATE dbo.SafetySuggestionsStatus SET ID ..........";
using (SqlCommand cmd = new SqlCommand(cmdText3, conn))
{
// Add the parameter to the command
var oParameter = cmd.Parameters.Add("statusID", SqlDbType.Int);
var sEMailAddresses = sbEmailAddresses.ToString();
string description = "SELECT Description FROM dbo.SafetySuggestionsLog";
string body = @"Good day, <br /><br />
<b> We just would like to notify you that your following safety suggestion: </b>"
+ description +
@"<br /><br />
has been.
<br /> <br /><br /> <br />
This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal (PSSP) </a>.
Please do not reply to this email.
";
int sendCount = 0;
List<string> addressList = new List<string>(sEMailAddresses.Split(','));
StringBuilder addressesToSend = new StringBuilder();
if (!string.IsNullOrEmpty(statusID))
{
SendStatusByEmail(addressesToSend.ToString(), "", "Notification of Your Safety Suggestion", body, true);
addressesToSend.Clear();
}
}
conn.Close();
}
}
注:我知道我不应该在这里发表长篇code,但因为我想向你解释我的工作,并得到您的帮助。
Note: I know that I should not post a lengthy code here, but because I want to explain to you my work and to get your help.
推荐答案
从表SafetySuggestionsLog名获取用户
Get the user by username from table SafetySuggestionsLog
string sqlGetLog = string.format("select * from SafetySuggestionsLog where SafetySuggestionsID= {0}", Convert.ToInt32(HiddenField1.Value));
然后你得到了SafetySuggestionsLog项/记录后,你把用户名,并用它来获取用户对象
then after you got the SafetySuggestionsLog item/record you take the username and use it to get the user object
string userName = SafetySuggestionsLog.UserName; (just get the name from your returned record)
string sqlGetUser = string.format("select * from Employee where UserName= '{0}'", userName);
这篇关于如何发送有关更新的建议的地位给其所有者的电子邮件通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!