簡體   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