如何从Repeater标签按钮on_click填充GridView

时间:2014-07-08 14:55:40

标签: c# asp.net

我给了我的" LabelButtons"一个值是" ID"为他们输出文字名称,以便人们看到" product1"," product2"等...但它真正由" ID"我需要接受这个" ID"单击按钮并在GridView上输出与ID值对应的数据。我怎么做到当我点击按钮它会填充gridview给我我正在寻找的信息?对不起,如果不够清楚我会在需要时提供更多细节。此外,我有我的隐藏字段试图保持按钮单击数据的值,它始终保持为null而不是取按钮单击ID的值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using TropicalServer.BAL;


namespace TropicalServer.UI
{
    public partial class Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }        
        }
        protected void BindData()
        {
            DataSet dsID = new BALGetItems().GetItemTypeData();
            rptrProductCategories.DataSource = dsID;
            rptrProductCategories.DataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hf = (HiddenField)e.Item.FindControl("hf");
            LinkButton cat = (LinkButton)e.Item.FindControl("lbPC");       
            hf.Value = Convert.ToString(e.CommandArgument);
            PopulateGrid(new BALGetItems().GetItemData(Convert.ToInt32(hf.Value)));
        }

        private void PopulateGrid(DataSet ds)
        {
            int value = 0;
            if (Cache["Data"] == null)
            {
                DataSet dsID = new BALGetItems().GetItemData(value);
                Cache["Data"] = dsID;
                gvPC.DataSource = dsID;
                gvPC.DataBind();
            }
            else
            {
                gvPC.DataSource = (DataSet)Cache["Data"];
                gvPC.DataBind();
            }  
        }
    }







 <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/TropicalServer.Master" AutoEventWireup="true" CodeBehind="Products.aspx.cs" Inherits="TropicalServer.UI.Products" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <! DOCTYPE html>`enter code here`
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head2">
        <link type="text/css" rel="stylesheet" href="~/AppThemes/TropicalStyles/Products.css" />
        <title>ServerLogin</title>
    </head>
        <body>
            <table>
                <tr>
                    <td>
                        <div>
                            <asp:Label ID="lblPC" class="productCategories" runat="server" Text="Product Categories"></asp:Label>
                        </div>
                        <div >
                            <asp:Repeater ID="rptrProductCategories" runat="server" OnItemCommand="Repeater1_ItemCommand">
                                <ItemTemplate>    
                                    <asp:LinkButton ID="lbPC" runat="server" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.ItemTypeID") %>' Text='<%#DataBinder.Eval(Container,"DataItem.ItemTypeDescription")%>'/><br />
                                </ItemTemplate>
                            </asp:Repeater>
                        </div>
                    </td>
                    <td>
                        <div class="dataGrid">
                            <asp:GridView ID="gvPC" runat="server" PagerSettings-PageButtonCount="5" AutoGenerateColumns="False" PageSize="5" AllowPaging="True" EnableSortingAndPagingCallbacks="True" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
                                <Columns>
                                    <asp:BoundField HeaderText="ItemNumber" DataField="ItemNumber"/>
                                    <asp:BoundField HeaderText="ItemDescription" DataField="ItemDescription" />
                                    <asp:BoundField HeaderText="Pre-Price" DataField="PrePrice" />
                                    <asp:BoundField HeaderText="Size" DataField="ItemUnits" />
                                </Columns>
                                <FooterStyle BackColor="White" ForeColor="#333333" />
                                <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

    <PagerSettings PageButtonCount="5"></PagerSettings>
                                <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="White" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                                <SortedAscendingCellStyle BackColor="#F7F7F7" />
                                <SortedAscendingHeaderStyle BackColor="#487575" />
                                <SortedDescendingCellStyle BackColor="#E5E5E5" />
                                <SortedDescendingHeaderStyle BackColor="#275353" />
                            </asp:GridView>
     <asp:HiddenField ID="hf" runat="server"/>
                        </div>
                    </td>
                </tr>
            </table>
        </body>
    </html>
    </asp:Content>

1 个答案:

答案 0 :(得分:0)

首先,您的LinkBut​​ton需要OnClick个事件。

<asp:LinkButton ID="lbPC" runat="server" 
     CommandArgument='<%#DataBinder.Eval(Container,"DataItem.ItemTypeID") %>' 
     Text='<%#DataBinder.Eval(Container,"DataItem.ItemTypeDescription")%>'
     OnClick="lbPC_Click"></asp:LinkButton>

然后在您的点击事件中,获取ID,检索GridView的数据,然后绑定GridView。

protected void lbPC_Click(object sender, EventArgs e)
{
    LinkButton lbPC = (LinkButton)sender;
    int id = int.Parse(lbPC.CommandArgument)

    //If for some reason you do need to hold the ID value in your hiddenfield, 
    //just call it as is. There is no need for FindControl() since it is
    //outside of both your Repeater and your GridView.
    hf.Value = id.ToString();

    gvPC.DataSource = YourDataRetrievalMethod(id);
    gvPC.DataBind();
}