我有一个GridView,其中包含一个包含电子邮件的列。我希望用户能够双击该行并激活电子邮件链接以打开电子邮件的Outlook窗口。我有双击部分,但我不知道如何从行中获取电子邮件来创建网址。我将粘贴下面的代码。
<asp:GridView ID="gvAllDOL" runat="server" Visible="False" PageSize="25" AutoGenerateColumns="False" OnDataBound="gvAllDOL_DataBound" DataSourceID="odsDOAll" OnRowDataBound="gvAllDOL_RowDataBound" DataKeyNames="sintDistrictOfficeID" OnRowCommand="gvAllDOL_RowCommand" OnSelectedIndexChanged="gvAllDOL_SelectedIndexChanged">
<Columns>
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("sintDistrictOfficeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sintDistrictOfficeID" HeaderText="id" SortExpression="sintDistrictOfficeID" />
<asp:BoundField DataField="vcharDOLOfficeName" HeaderText="DOL Office Name" SortExpression="vcharDOLOfficeName" />
<asp:BoundField DataField="vcharDOLCity" HeaderText="City" SortExpression="vcharDOLCity" />
<asp:BoundField DataField="vcharDOLState" HeaderText="State" SortExpression="vcharDOLState" />
<asp:BoundField DataField="intBatchCount" HeaderText="Number Batches" SortExpression="intBatchCount" />
<asp:BoundField DataField="intCaseCount" HeaderText="Number Cases" SortExpression="intCaseCount" />
<asp:BoundField DataField="intExamCount" HeaderText="Number Examiners" SortExpression="intExamCount" />
</Columns>
</asp:GridView>
protected void gvCE_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onMouseOver", "Highlight(this)");
e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)");
// Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
// Get the javascript which is assigned to this LinkButton
string _jsDouble =
ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this JavaScript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
}
protected void gvCE_RowCommand(object sender, GridViewCommandEventArgs e)
{
string email = ((Label)gvCE.Rows[0].Cells[1].FindControl("lblEmail")).Text; (this doesn't work)
GridView _gridView = (GridView)sender;
string _commandName = e.CommandName;
switch (_commandName)
{
case ("DoubleClick"):
Response.Redirect("<a href=mailto:" + email + ">");
break;
}
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in gvAllDOL.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl01");
}
}
foreach (GridViewRow r in gvCE.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl01");
}
}
base.Render(writer);
}
答案 0 :(得分:0)
将您的电子邮件字段转换为模板字段。添加以下代码,然后尝试使用它:
<asp:HyperLink ID="EmailLink" runat="server" Text='Email' NavigateUrl=
"mailto:" + '<%# Eval("yourBoundEmailFieldNameHere") %>'
</asp:HyperLink>
答案 1 :(得分:0)
我会使用Manul的建议通过项目模板添加电子邮件地址
<asp:HyperLink ID="EmailLink" runat="server" Text='Email' NavigateUrl=
"mailto:" + '<%# Eval("yourBoundEmailFieldNameHere") %>'
</asp:HyperLink>
通过这种方式,您可以访问电子邮件地址,人们可以看到他们将通过电子邮件发送电子邮件。 然后将此脚本添加到页面
$("table tr").dblclick(function () {
var mailto_link = $('a', $(this)).attr('href');
window = window.open(mailto_link, 'emailWindow');
if (window && window.open && !window.closed)
window.close();
});
然后删除将javascript函数添加到网格中每一行的代码,因为不再需要它。
这是一个jsfiddle链接http://jsfiddle.net/gorrilla/jEX7Y/
答案 2 :(得分:0)
您可以使用命令参数传递电子邮件地址,从而大大简化了检索地址所需的逻辑。
将Command按钮列更改为TemplateField并在其中添加asp:按钮。然后将属性CommandArgument添加到按钮。
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="dblClick" runat="server" Text="dblClick"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="dblClick" />
</ItemTemplate>
</asp:TemplateField>
然后在
背后的代码中protected void gvCE_RowCommand(object sender, GridViewCommandEventArgs e)
{
string email = e.CommandArgument;