如何从aspx页面绑定用户控件中存在的gridview?

时间:2012-07-09 05:27:57

标签: c# asp.net

我在aspx页面中有一个下拉列表。我在用户控件中有一个gridview。我已将用户控件放在aspx页面中。如何在下拉列表的selectIndexChanged事件中绑定gridview。我想将dropdownlist选择的索引传递给一个函数,然后绑定用户控件中的gridview。我想从aspx.cs绑定gridview。

ASPX页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" MasterPageFile="~/HomePage.master" Inherits="Main" %>

<%@ Register TagPrefix="uc" TagName="UserControl" Src="~/UserControl.ascx" %>

<%@ Register Assembly="WebControls" Namespace="WebControls" TagPrefix="cc" %>
<asp:Content ContentPlaceHolderID="mainContent" ID="mainPart" runat="server">
    <asp:Label ID="Label1" runat="server" Text="<%$ Resources:GlobalResource, EmpName %>">></asp:Label>
    <cc:CstDropDown ID="ddl" runat="server" AutoPostBack="true"  OnSelectedIndexChanged="ddl_SelectedIndexChanged">
    </cc:CstDropDown>
    <uc:UserControl ID="UsrCtrl" runat="server" />
</asp:Content>
<asp:Content ContentPlaceHolderID="subContent" ID="sub" runat="server">
</asp:Content>

ASCX页面

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControl.ascx.cs" Inherits="UserControl" %>

   <asp:GridView ID="dataGrid"  runat="server" AutoGenerateColumns="false" 
    DataKeyNames="EmpID" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" OnRowEditing="dataGrid_RowEditing"
     OnRowCancelingEdit="dataGrid_RowCancelingEdit" OnRowUpdating="dataGrid_RowUpdating"> 

1 个答案:

答案 0 :(得分:3)

在用户控件UsrCtrl中创建绑定方法public并从主页面中的下拉列表的selectedIndexChange事件中调用它。

在UsrCtrl中

public void BindMyGrid(string selectedValue)
{
     //Bind grid here
}

在aspx Main.aspx

protected void ddl_SelectedIndexChanged(object source, EventArgs e)
{
   UsrCtrl.BindMyGrid(ddl.SelectedValue);

}