0

我试图从 gridview 中选择一个值,但我返回空字符串。

<asp:GridView ID="GridViewLedger" runat="server" Width="100%" AutoGenerateColumns="False"
                        ShowFooter="True" DataKeyNames="AccountID" OnRowCommand="GridViewLedger_RowCommand"
                        CssClass="table table-hover table-striped table-bordered">
                        <Columns>


                            <asp:TemplateField HeaderText="InvoiceNo" SortExpression="InvoiceNo">
                              <ItemTemplate>
                                <asp:LinkButton ID="btnClickInvoiceNo" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="InvoiceNo" Text='<%# Eval("InvoiceNo") %>' />
                              </ItemTemplate>
                            </asp:TemplateField>

                        </Columns>
                    </asp:GridView>

我在这里删除了所有不相关的列,

protected void GridViewLedger_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        string InvoiceNo = GridViewLedger.Rows[index].Cells[4].Text;
    }
4

2 回答 2

3

您无法通过访问行单元格的 .Text 来访问 TemplateField 的 ItemTemplate 内的 LinkBut​​ton 的文本。

你能做的是

LinkButton lbInvoiceNo = GridViewLedger.Rows[index].Cells[4].FindControl("btnClickInvoiceNo");
string invoiceNo = lbInvoiceNo.Text;
于 2015-03-06T09:58:45.227 回答
0

尝试..

    if (e.CommandName == "InvoiceNo")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        string InvoiceNo = GridViewLedger.Rows[index].Cells[4].Text;
    }

尝试2:

       if (e.CommandName == "InvoiceNo")
       {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridViewLedger.Rows[index];
        string InvoiceNo=row.Cells[4].Text;
       }
于 2015-03-06T09:59:19.040 回答