单击gridview按钮字段时,重定向到特定页面

时间:2012-03-13 15:12:43

标签: asp.net

我的gridview中有两个字段。一个是具有日期范围的字段,其中一个是按钮字段。我想这样做,以便当点击一个按钮时,它会将它们重定向到一个特定的页面,其格式化的字符串类似于点击超链接字段时的方式:

DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}"

但是,我不想使用其他字段的值(日期范围)。我想使用记录的主键“SundayOfWeek”。用户想要一个按钮而不是超链接。有没有办法用按钮做同样的事情?

3 个答案:

答案 0 :(得分:1)

尝试使用此类型的列而不是ButtonField:

<Columns>
   <asp:HyperLinkField 
     DataTextField="ProductName" 
     HeaderText="Product Name" 
     SortExpression="ProductName" 
     DataNavigateUrlFields="ProductID" 
     DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}" />
</Columns>

答案 1 :(得分:0)

当然,其中一种方法是:

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

答案 2 :(得分:0)

如果您不想使用日期范围字段,则应使用另一列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>