private void dbind(int id)
{
string[] strings = new string[3];
if (id == 1)
{
strings[0] = "a";
strings[1] = "b";
strings[2] = "c";
}
if (id ==2)
{
strings[0] = "d";
strings[1] = "e";
strings[2] = "f";
}
if (id == 3)
{
strings[0] = "g";
strings[1] = "h";
strings[2] = "i";
}
var image = from x in strings
select new
{
Letter = x,
};
GridView1.DataSource = image;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Will use this bit to get the items id in gridview but for now just checking for return value
Label2.Text = Master.dropBox.SelectedValue;
}
}
Now my problem is when you select a new value in the dropdowlist I want it to change the data displayed in the gridview based on the selected value. Now I can get that to work fine if I remove the
IsPostBack function out of the page_load. But then I can’t get the RowCommand to work no more.
I can’t seem to get both things working at the same time.
Have you looked into the MasterPage's and the Page's life cycle? Do you get any errors? What happens when you run it through a debugger?
I believe the MasterPage is inserted into the ASP.NET Control Tree after the Page has been loaded - this could mean is that your Page.Master.FindControl is finding an empty control or no control at all.
I wonder if you could adopt a different method of page-to-page communication, have you looked into
EventBubbling or
Delegates?
Now my problem is when you select a new value in the dropdowlist I want it to change the data displayed in the gridview based on the selected value. Now I can get that to work fine if I remove the
IsPostBack function out of the page_load. But then I can’t get the RowCommand to work no more.
If you want to access the dropdownlist that is on the master page like this:
Label2.Text = Master.dropBox.SelectedValue;
You should add the @ MasterType directive in our content page.
chrismogz
Member
15 Points
17 Posts
Master page postback result with gridview rowcommand
Feb 06, 2008 08:13 AM|LINK
Can i pick some ones brain on a problem with some code.
Basically am using a MASTER page that contains dropdownlist:
public partial class Site : System.Web.UI.MasterPage
{
public DropDownList dropBox
{
get
{
return this.DropDownList1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Now on another page linked to the master page am trying to get the product ID stored in the dropdownlist1 value on the master page see code below:
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){
if (Page.IsPostBack)
{
DropDownList DDL_id = (DropDownList)Page.Master.FindControl("DropDownList1");
dbind(int.Parse(DDL_id.SelectedValue));
}
else
{
if (PreviousPage != null && PreviousPage.IsPostBack)
{
DropDownList DDL_id = (DropDownList)PreviousPage.Master.FindControl("DropDownList1");
dbind(int.Parse(DDL_id.SelectedValue));
}
}
}
private void dbind(int id)
{
string[] strings = new string[3];
if (id == 1)
{
strings[0] = "a";
strings[1] = "b";
strings[2] = "c";
}
if (id ==2)
{
strings[0] = "d";
strings[1] = "e";
strings[2] = "f";
}
if (id == 3)
{
strings[0] = "g";
strings[1] = "h";
strings[2] = "i";
}
var image = from x in strings
select new
{
Letter = x,
};
GridView1.DataSource = image;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Will use this bit to get the items id in gridview but for now just checking for return value
Label2.Text = Master.dropBox.SelectedValue;
}
}
Now my problem is when you select a new value in the dropdowlist I want it to change the data displayed in the gridview based on the selected value. Now I can get that to work fine if I remove the IsPostBack function out of the page_load. But then I can’t get the RowCommand to work no more.
I can’t seem to get both things working at the same time.
Thanks for your time
Chris
Adam.Kahtava
Contributor
4775 Points
927 Posts
Re: Master page postback result with gridview rowcommand
Feb 06, 2008 06:58 PM|LINK
Have you looked into the MasterPage's and the Page's life cycle? Do you get any errors? What happens when you run it through a debugger?
I believe the MasterPage is inserted into the ASP.NET Control Tree after the Page has been loaded - this could mean is that your Page.Master.FindControl is finding an empty control or no control at all.
I wonder if you could adopt a different method of page-to-page communication, have you looked into EventBubbling or Delegates?
Here's an article that describes the MasterPage life cycle fairly well: ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Master page postback result with gridview rowcommand
Feb 08, 2008 04:16 AM|LINK
Hi,
If you want to access the dropdownlist that is on the master page like this: Label2.Text = Master.dropBox.SelectedValue;
You should add the @ MasterType directive in our content page.
For example:
<%@ Page Language="VB" MasterPageFile="~/Master1.master"
AutoEventWireup="true" %>
<%@ MasterType VirtualPath="~/Master1.master" %>
If you do not add the mastertype in the content page, you can use the findcontrol method to get the object of the manster page controls.
Hope it helps.
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.