当gridview buttonfield被点击时重定向到特定页面
问题描述:
我在我的gridview中有两个字段。一个是日期范围的字段,其中一个字段是buttonfield。我希望把它使得单击按钮时,它会重定向到一个特定的页面类似于它是如何当一个HyperLinkField字段被点击这样做了格式化字符串:当gridview buttonfield被点击时重定向到特定页面
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}"
然而,我不想使用其他字段的值(日期范围)。我想使用“SundayOfWeek”记录的主键。用户想要一个按钮而不是超链接。有没有办法用按钮做同样的事情?
答
当然,许多方法之一是这样的:
<columns>
<asp:templatefield headertext="Button">
<itemtemplate>
<asp:Button id="btnRedirect"
Text= "Click me"
CommandArgument='<%#Eval("SundayOfWeek")%>'
OnClick="DoRedirect"
runat="server"/>
</itemtemplate>
</asp:templatefield>
</columns>
protected void DoRedirect(object sender, EventArgs e)
{
Button theButton = sender as Button;
Response.Redirect("Newpage.aspx?ID="+theButton.CommandArgument);
}
答
如果你不想使用的日期范围的领域,你应该使用另一列DataNavigateUrlField
:
DataNavigateUrlField="KeyField"
DataTextField="DateField"
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}"
如果你不希望使用HyperLinkColumn
,你应该使用TemplateField
并添加Button
:
<asp:TemplateColumn>
<input type="button" onclick="location.href='ProductInfo.aspx?ProductID=<%#Eval("KeyField")%>'" value='<%#Eval("DateField")%>'' />
</asp:TemplateColumn>
答
尝试使用这种类型的列,而不是一个ButtonField字段的:
<Columns>
<asp:HyperLinkField
DataTextField="ProductName"
HeaderText="Product Name"
SortExpression="ProductName"
DataNavigateUrlFields="ProductID"
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}" />
</Columns>
会存在也有一种方法做同样的事情用jQuery? – Chris 2012-03-13 15:33:49
当然,但是您要如何使用jQuery来做到这一点? Tim的答案已经显示了一个JavaScript替代方案。你为什么不使用它? – Icarus 2012-03-13 15:40:30