繁体   English   中英

想要在单击网格视图中特定行的按钮字段时显示该行的数据

[英]Want to show data for a specific row in the gridview when that row's buttonfield is clicked

我看到了与此主题类似的帖子,但无法解决我的问题。 我正在使用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];
    }
}
}

如果您可以像使用“客户名称”一样用模板字段替换“ Customer Id绑定字段”,那么它将非常简单:-

<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您将必须显式隐藏数据和标头,这会使事情变得复杂。

暂无
暂无

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

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