简体   繁体   English

如何在gridview asp.net中获取行索引

[英]how to get row index in gridview asp.net

I am developing an asp.net website I have created a template with gridview and I put a button in the template and I want to browse to another page by clicking on the button by Row ID .I need your help 我正在开发一个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">&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>

In your C# page, you could write an if statement? 在您的C#页面中,您可以编写一条if语句?

Try this - 尝试这个 -

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

您可以使用LinkBut​​ton代替按钮,并使用类似以下问题的命令: 如何从asp.net中的gridview获取选定的行字段,我认为这可以为您提供帮助。

I assume you are passing gridview row Index as parameter ,Use <%# Container.DataItemIndex%> for getting row index of your gridview,And your ButtonCode like 我假设您正在传递gridview行索引作为参数,请使用<%# Container.DataItemIndex%>获取gridview的行索引,并且您的ButtonCode像

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

Remember row index always start with 0 , use <%# Container.DataItemIndex + 1 %> if you want it from 1 请记住,行索引始终以0开头,如果要从1开始,请使用<%# Container.DataItemIndex + 1 %>

--> Add OnRowDataBound Method in GridView. ->在GridView中添加OnRowDataBound方法。
--> And Code in Your C# Page. ->然后在您的C#页面中进行编码。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 受保护的void GridView1_RowDataBound(对象发送者,GridViewRowEventArgs e)
{ {
if (e.Row.RowType == DataControlRowType.DataRow) 如果(e.Row.RowType == DataControlRowType.DataRow)
{ {
if (Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ID")) > 0) 如果(Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem,“ ID”)))> 0)
{ {
Response.Redirect(Link); Response.Redirect(Link);
} }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM