繁体   English   中英

无法从gridview按钮字段获取价值

[英]Can't get value from gridview button field

尝试从gridview的单元格获取值时遇到困难。 每当我询问该值时,它都会转到NullExcaption,因此我询问该行本身是否为null,是否为null。 这是ASP代码:

<asp:SqlDataSource ID="SqlDataSourceFlats" runat="server" ConnectionString="<%$ ConnectionStrings:EflatsConnectionString %>" SelectCommand="SELECT [Rent], [Address], [FlatId] FROM [Flat_Main]"></asp:SqlDataSource>
                      <asp:GridView ID="GridView1"  onrowcommand="GridView1_RowCommand" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="FlatId" DataSourceID="SqlDataSourceFlats" ForeColor="#333333" GridLines="None" Width="100%"  >
                          <AlternatingRowStyle BackColor="White" />
                          <Columns>
                              <asp:BoundField DataField="FlatId" HeaderText="FlatId" InsertVisible="False" ReadOnly="True" SortExpression="FlatId" />
                              <asp:BoundField DataField="Rent" HeaderText="Rent" SortExpression="Rent" />
                              <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address">
                              <ControlStyle Width="60%" />
                              </asp:BoundField>


                               <asp:ButtonField Text="Add"  CommandName="Apply" ButtonType="Button" />
                              <asp:ButtonField Text="Remove" ButtonType="Button" />
                          </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>

和按钮代码:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Apply")
    {
        //  string c = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
        //  Response.Write(GridView1.SelectedRow.Cells[2].Text);
             if (GridView1.SelectedRow == null)
            {
                 Response.Write("Gridview is failing me :(");
             }


    //    string celltext = this.GridView1.SelectedRow.Cells[2].Text;
    //    Response.Write(celltext);
    }
}

基本上,我想单击按钮并从所选行中获取FlatId值。

OnRowCommand事件似乎没有几个错误。

1.) GridView1.SelectedRow ::

您实际上尚未选择任何行,因此该行始终为null

2.) GridView1.Rows[e.NewEditIndex] ::

同样,您不编辑任何行,那么您期望e.NewEditIndex的值是e.NewEditIndex

如何在GridView控件中处理ButtonField事件

  1. 将按钮的CommandName属性设置为标识其功能的字符串,例如“选择”,“编辑”等。
  2. 若要确定记录的索引RowIndex,请使用事件参数的CommandArgument属性,该参数传递给数据绑定控件的命令事件。 ButtonField类自动使用适当的行索引值填充CommandArgument属性。

下面是一个选择记录并处理OnRowCommand事件的OnRowCommand

标记:

<columns>
      <asp:buttonfield buttontype="Button" commandname="Select"
                headertext="Select This Record" text="Select"/>
</columns>

OnRowCommand事件:

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    if(e.CommandName=="Select")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int _rowIndex = Convert.ToInt32(e.CommandArgument);    

      GridViewRow selectedRow = CustomersGridView.Rows[_rowIndex];  
      string cellText = selectedRow.Cells[2].Text;  
    }
  }

暂无
暂无

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

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