【问题标题】:How to get selected cell value of a GridView after filtration. ASP.Net过滤后如何获取 GridView 的选定单元格值。 ASP.NET
【发布时间】:2021-03-19 12:17:16
【问题描述】:

我有一个 GridView,通过它我可以显示一些书籍的详细信息,并且可以根据书籍属性对其进行过滤。问题是,如果我过滤一些书并尝试编辑其中一本,编辑器会选择整体行值的 id 而不是新的。 var id = Convert.ToInt32(gridview.Rows[e.NewEditIndex].Cells[1].Text);我通过这个获取索引值

 <div class="col">
            <asp:GridView OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView3_RowUpdating" OnRowCancelingEdit="GridView3_RowCancelingEdit" 
                         class="table table-striped table-bordered" ID="GridView3" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" 
              
                           HeaderStyle-BackColor="#95daa4" HeaderStyle-ForeColor="Black" >
                <Columns >
                    <asp:CommandField ShowEditButton="true" ControlStyle-Width="26px" ControlStyle-CssClass="linkGVE"/>
                    <asp:BoundField  DataField="Id"  HeaderText="ID" ReadOnly="True" SortExpression="Id">
                        <ControlStyle Font-Bold="True" CssClass="linkGVE"/>
                        <ItemStyle Font-Bold="True"/>
                    </asp:BoundField>
                    <asp:TemplateField >
                        <ItemTemplate>
                            <div class="container-fluid">
                                    <div class="col-lg-10">
                                        <div class="row">
                                            <div class="col-12">
                                                <asp:Label ID="Label1"  runat="server" class="animated bounceInRight"  Text='<%# Eval("BookName") %>' Font-Bold="True" Font-Size="18px"></asp:Label>
                                            </div>
                                        </div>
                                        <br/>
                                        <div class="row">
                                            <div class="col-12">
                                                <span>
                                                    <b>Province -</b><span>&nbsp;</span>
                                                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("Province") %>'></asp:Label>
                                                </span>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-12">
                                                <b>Subject - </b>
                                                <asp:Label ID="Label5" runat="server" Text='<%# Eval("Subject") %>'> </b></asp:Label>
                                                &nbsp;| <b>Categories -</b>
                                                <asp:Label ID="Label6" runat="server" Text='<%# Eval("Categorey") %>'></asp:Label>
                                                &nbsp;| <b>PackageName -</b>
                                                <asp:Label ID="Label7" runat="server" Text='<%# Eval("PackageName") %>'></asp:Label>

                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-12">
                                                <b>CategoricalTitle -</b>
                                                <asp:Label ID="Label12" runat="server" Text='<%# Eval("CategoricalTitle") %>'></asp:Label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-2 " style="margin-top: 25px; text-align: center;">

                                        <asp:Image class="img-fluid" BorderWidth="2px"  BorderColor="#82aeb1"   ID="Image1" Height="100px" Width="120px" runat="server" ImageUrl='<%# "http://0.0.0.0/Pictures/" + Eval("PictureURL") %>'/>
                                        <a href='<%# "http://0.0.0.0.0/PDF/" + Eval("PDFURL") %>' style="font-size: 11px;color: black">Download</a>
                                    </div>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

【问题讨论】:

    标签: asp.net sorting aspxgridview


    【解决方案1】:

    尝试查找控件比查找单元格更容易,因为如果您将任何容器放在控件之前,则会创建单元格。

    您可以尝试FindControl,而不是像下面这样读取单元格 how to find control in edit item template?

    GridView1.Rows[e.NewEditIndex].FindControl("control");
    

    并相应地进行投射。
    此外,如果它是放入gridviewdata keys 的主键并从那里获取它。示例Get DataKey values in GridView RowCommand

    还希望您将数据正确绑定到 gridview。

    【讨论】:

      【解决方案2】:

      我没有在页面重新加载时应用 ispostback 条件。一旦我应用了这个条件,我的代码就开始工作了。谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-28
        • 1970-01-01
        相关资源
        最近更新 更多