我正在开发一个asp.net网站我用gridview创建了一个模板,我在模板中放了一个按钮,我想通过按行ID按钮浏览到另一个页面。我需要你的帮助
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource2" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" OnRowCommand="GridView1_RowCommand" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<AlternatingItemTemplate>
<table class="nav-justified">
<tr>
<td class="auto-style5" dir="rtl" style="text-align: justify; vertical-align: top">
<asp:Image ID="Image7" runat="server" ImageUrl='<%# "~/images/"+(string)Eval("img") %>' Width="150px" />
</td>
<td class="auto-style6" dir="rtl" style="text-align: justify; vertical-align: top"> </td>
<td dir="rtl" style="text-align: justify; vertical-align: top">
<table class="nav-justified">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# (string)Eval("onvan") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("comment").ToString().Length<=100?Eval("comment"):Eval("comment").ToString().Substring(0,100) %>'></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="auto-style5"> </td>
<td class="auto-style6"> </td>
<td dir="rtl" style="vertical-align: top; text-align: justify">
<asp:Label ID="Label3" runat="server" Text='<%# Eval("datetime") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style5">
<asp:Button ID="Button1" runat="server" CssClass="btn" Text="Button" />
</td>
<td class="auto-style6"> </td>
<td> </td>
</tr>
</table>
</AlternatingItemTemplate>
<ItemTemplate>
<table class="nav-justified">
<tr>
<td class="auto-style3" dir="rtl" style="text-align: justify; vertical-align: top">
<asp:Image ID="Image8" runat="server" ImageUrl='<%# "~/images/"+(string)Eval("img") %>' Width="150px" />
</td>
<td class="auto-style4" dir="rtl" style="text-align: justify; vertical-align: top">
</td>
<td dir="rtl" style="text-align: justify; vertical-align: top">
<table class="nav-justified">
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text='<%# (string)Eval("onvan") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("comment").ToString().Length<=100?Eval("comment"):Eval("comment").ToString().Substring(0,100) %>'></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style4"> </td>
<td dir="rtl" style="vertical-align: top; text-align: justify">
<asp:Label ID="Label4" runat="server" Text='<%# Eval("datetime") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style3">
<asp:Button ID="Button2" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>" CommandName="nav" Text="Browse" CssClass="btn" />
</td>
<td class="auto-style4"> </td>
<td> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
答案 0 :(得分:0)
在您的C#页面中,您可以编写if语句吗?
试试这个 -
if (ID == "Button2")
{
Response.Redirect("http://yourDestination?");
}
答案 1 :(得分:0)
您可以使用LinkButton而不是按钮,并使用此问题中的命令: How to get selected row field from gridview in asp.net 我想这可以帮到你。
答案 2 :(得分:0)
我假设您将gridview行Index作为参数传递
,使用<%# Container.DataItemIndex%>
获取gridview的行索引,使用ButtonCode,如
<asp:Button ID="Button2" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="nav" Text="Browse" CssClass="btn" />
记住行索引总是从0开始,如果你想从1
那么使用<%# Container.DataItemIndex + 1 %>
答案 3 :(得分:0)
- &GT;在GridView中添加OnRowDataBound方法 - &GT;和C#中的代码。
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
if(Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem,“ID”))&gt; 0)
{
的Response.Redirect(链路);
}
}
}