[asp.net]想要在 gridview 中显示特定行的数据,单击该行的 buttonfield 时

标签: Asp.net GridView C#
发布时间: 2016/7/7 20:20:31
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我看到一些类似的帖子,到此主题,但不是能解决我的问题。我使用 access 数据库。所以此页将数据从数据库加载,但是它不会显示任何数据,直到该特定行按钮。现在它显示所有数据。这里是我的代码,请建议我的东西。

    <columns>
      <asp:buttonfield buttontype="Button" 
        commandname="Select"
        headertext="Select Customer" 
        text="Select"/>
        <asp:boundfield datafield="Customer ID" readonly="true" headertext="Customer ID"/>

            <asp:TemplateField HeaderText="First Name" AccessibleHeaderText="FirstName">
             <ItemTemplate>
              <asp:Label ID ="txtEmployeeName" runat ="server" Text='<%# Eval("firstName") %>'></asp:Label>
             </ItemTemplate>
            </asp:TemplateField>
    </columns>

public partial class DisplayCustomer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    DataLayer.DataConnector dat = new DataLayer.DataConnector(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ovich\Documents\Visual Studio 2013\WebSites\WebSite3\App_Data\Customer.accdb");
    DataTable dt = dat.DataSelect("SELECT* from Customer");
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
}

public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{

    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if (e.CommandName == "Select")
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);

        // Get the last name of the selected author from the appropriate
        // cell in the GridView control.
        GridViewRow selectedRow = GridView1.Rows[index];
    }
}
}

解决方法 1:

你若安好,替换你 Customer Id BoundField 与模板字段,就像您的客户名称的样子然后它将会非常简单:-

<columns>
   <asp:Buttonfield buttontype="Button" commandname="Select" headertext="Select Customer" 
        text="Select"/>
   <asp:TemplateField HeaderText="Employee Id" AccessibleHeaderText="FirstName">
        <ItemTemplate>
           <asp:Label ID ="lblEmployeeId" runat ="server" 
                    Text='<%# Eval("EmployeeId") %>' Visible="false"></asp:Label>
        </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField HeaderText="First Name" AccessibleHeaderText="FirstName">
        <ItemTemplate>
            <asp:Label ID ="txtEmployeeName" runat ="server" 
                       Text='<%# Eval("firstName") %>' Visible="false"></asp:Label>
        </ItemTemplate>
   </asp:TemplateField>
</columns>

你可以简单的套到控件的可见性 false 和我一样,在 RowCommand 事件显示在按钮上单击:-

protected void CustomersGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        int index = Convert.ToInt32(e.CommandArgument);
       ((Label)grdCustomer.Rows[index].FindControl("lblEmployeeId")).Visible = true;  
       ((Label)grdCustomer.Rows[index].FindControl("txtEmployeeName")).Visible = true;
    }
}

BoundControls 你将不得不显式隐藏数据和页眉和它将事情复杂化。

赞助商