为什么网格的单元格值返回空字符串?

时间:2015-03-06 09:55:35

标签: c# asp.net gridview asplinkbutton

我试图从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;
    }

2 个答案:

答案 0 :(得分:3)

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

你能做的是

LinkButton lbInvoiceNo = GridViewLedger.Rows[index].Cells[4].FindControl("btnClickInvoiceNo");
string invoiceNo = lbInvoiceNo.Text;

答案 1 :(得分:0)

尝试..

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

Try2:

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