2016-06-10 184 views
0

我开发一个asp.net网站,我已经创建了GridView的模板,我把一个按钮的模板,我想通过点击按钮浏览到另一个页面行ID。我需要你的帮助如何让行索引中的GridView asp.net

<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">&nbsp;</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">&nbsp;</td> 
            <td class="auto-style6">&nbsp;</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">&nbsp;</td> 
            <td>&nbsp;</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"> 
             &nbsp;</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">&nbsp;</td> 
            <td class="auto-style4">&nbsp;</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">&nbsp;</td> 
            <td>&nbsp;</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

在你的C#页面,你可以写一个if语句吗?

试试这个 -

if (ID == "Button2") { Response.Redirect("http://yourDestination?"); }

+0

在我能做到什么方法????? – user6440832

+0

在源代码上,右键单击并查看代码 - 这会将您带到C#区域(假设您使用C#而不是基本的Visual Basic编写),您可以在其中编写许多函数以与源代码中的元素进行连接!我已经使用了视觉工作室已经有一段时间了,希望它有一点帮助。 – phppaddy

0

我假设你逝去的GridView行指数作为参数 ,使用<%# Container.DataItemIndex%>为获得你的GridView的行索引,而你ButtonCode像

<asp:Button ID="Button2" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="nav" Text="Browse" CssClass="btn" /> 

记住排索引始终以0开始,使用<%# Container.DataItemIndex + 1 %>如果你想它从1

0

- >在GridView中添加OnRowDataBound方法。
- >和你的C#页面中的代码。

保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
      {
           如果(e.Row.RowType == DataControlRowType.DataRow)
                  {
                       如果(Convert.ToDecimal(DataBinder.Eval方法(e.Row.DataItem, “ID”))> 0)
                              {
                                   响应。重定向(链路);
                             }

} 
}