Chethan Babu R
Chethan Babu R

Reputation: 55

how to bind gridview present in user control from aspx page?

I have a dropdownlist in aspx page. I have a gridview in user control.I have placed the user control in aspx page.How to bind the gridview on selectIndexChanged event of the dropdownlist. I want to pass the dropdownlist selected index to a function and then bind gridview which is in user control.I want to bind the gridview from aspx.cs.

ASPX page

<%@ 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 page

<%@ 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"> 

Upvotes: 1

Views: 4572

Answers (1)

Adil
Adil

Reputation: 148120

Make the binding method in user control UsrCtrl public and call it from selectedIndexChange event of dropdown in the main page.

In UsrCtrl

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

In aspx Main.aspx

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

}

Upvotes: 3

Related Questions