Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Master page: site.master

site.master
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" >

<Columns>
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
</Columns>
</asp:GridView>


site.master.cs

i have function here which select record from database and binding to gridview. But i am calling function from child page.


C#
puplic void Bind_Master_ProductsGrid
{
string sqlconn="i took connectionstring"
SqlConnection conlist = new SqlConnection(sqlconn);
conlist.open();

SqlCommand cmd2 = new SqlCommand("SELECT ProductName, UnitPrice FROM productseventhandler", conlist);
SqlDataAdapter adap1 = new SqlDataAdapter(cmd2);
DataTable dt = new DataTable();
adap1.Fill(dt);
Gridview1.DataSource = dt;
Gridview1.DataBind(); // here i am facing problem. i have rows in dt and also Gridview1.DataSource But Gridview1.DataBind() is not binding records when i test F+11(debugger)
conlist.close();


}


Child page: product.aspx

<%@ MasterType VirtualPath="~/Site.master" %> // i added this to access master page function.

in code behind of product.aspx.cs,
// i am inserting to DB

Then, i called that master page function to select record from and need to bind into gridview.

C#
Site myMasterPage = Page.Master as Site;
myMasterpage.Bind_Master_ProductsGrid();


i have rows in dt and also Gridview1.DataSource But Gridview1.DataBind() is not binding records. how to solve this problem?
Posted
Updated 25-Jun-14 8:45am
v3
Comments
goathik 25-Jun-14 10:25am    
i will simulate here and let you know in a minute
christhuxavier 25-Jun-14 13:07pm    
okay pls take a look
pradiprenushe 25-Jun-14 14:49pm    
Are you using update panel?
pradiprenushe 25-Jun-14 14:53pm    
Or call to show data is not at proper level in event cycle? Can you see data after page refresh?

Please keep in mind that if you are calling the method in the child page load event handler it will first call the method then, it will hit the page load event of master page again. So if you are expecting something in master load event handler it will not work for you before the method call.

Here i have tested your code with sample data and it works fine for me when i am calling the master method from child page to bind the grid in master page. Please check with this code first and let me know if it works for you or not :

C#
public void Bind_Master_ProductsGrid()
{
  if (!dt.Columns.Contains("ProductName"))
     dt.Columns.Add("ProductName");
  if (!dt.Columns.Contains("UnitPrices"))
    dt.Columns.Add("UnitPrices");
 
  DataRow dr1 = dt.NewRow();
  dr1["ProductName"] = "Product1";
  dr1["UnitPrice"] = "10.00";
  dt.Rows.Add(dr1);
  DataRow dr2 = dt.NewRow();
  dr2["ProductName"] = "Product2";
  dr2["UnitPrice"] = "20.00";
  dt.Rows.Add(dr2);
 
  Gridview1.DataSource = dt;
  Gridview1.DataBind();
}


Hope this will also work for you.
 
Share this answer
 
Comments
christhuxavier 26-Jun-14 3:17am    
Yes You are correct. But i am calling master's function from inside the button click envnt of child page not page load event handler.

for ex:

product.aspx:

consider update_cart_click button click event.
protected void update_cart_click(object sender , EventArgs e)
{
..//binding gridview cart and inserted into data base which is in child page.

when you click update_cart_click compiler will call page load then will go to master page then will go update_cart_click event.

Now i need to update same thing to master page function from here i need to call master page function. Normally from here will not go master page right?
how to do that?

That is why i added this to access master page function in product.aspx
<%@ MasterType VirtualPath="~/Site.master" %>
then i called inside the click event like below.
Site myMasterPage = Page.Master as Site;
myMasterpage.Bind_Master_ProductsGrid();

ya now that function calls successfully and i have the records Gridview1.DataSource = dt; but Gridview1.DataBind() is not binding so girdview shows empty.
}
SRS(The Coder) 26-Jun-14 3:32am    
Mine directives used are as below :-

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="BindGridOnMasterPage.aspx.cs" Inherits="TestApp.BindGridOnMasterPage" %>
<%@ MasterType VirtualPath="~/Site.Master" %>
SRS(The Coder) 26-Jun-14 3:37am    
Please have a look into it -
http://stackoverflow.com/questions/8946742/why-we-use-master-type

Can check with
this.Master.Bind_Master_ProductsGrid();
christhuxavier 26-Jun-14 4:49am    
I tried like this too

his.Master.Bind_Master_ProductsGrid();

It calls master's funtion and also has records in Gridview1.DataSource but Gridview1.DataBind() is not binding i guess as still i am getting nothing in gridview
hi

use following code for finding the control on master page

this.Master.Bind_Master_ProductsGrid();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900