Learing ?

A

Aussie Rules

Hi,

I have a master page that contains a dropdownlist control, and a content
page with a datagrid on it.

When the user changes the selecteditem in the drop down, I want to refresh
the datagrid with new data (from a sql stored proc).

From the masterpage code, how can I access the content pages dataview
control ?

Thanks
 
W

Walter Wang [MSFT]

Hi Aussie,

Thank you for your post.

Based on my understanding, your question is how to refresh the DataGrid on
the content page when the DropDownList's selected value changes on the
master page. If I've misunderstood anything, please feel free to post here.

If you're using DataSource's ControlParameter to select data, you can
actually reference the DropDownList in the content page directly:

<asp:DataGrid ID="grid1" runat="server" DataSourceID="SqlDataSource1">
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [CategoryName]
FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlType" Name="CategoryID"
PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

And here's the content of the master page:

<asp:DropDownList ID="ddlType" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="CategoryName" DataValueField="CategoryID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM
[Categories]"></asp:SqlDataSource>

=====

Another approach is to define an event in the master page, raise the event
in master page when the DropDownList's selected index is changed, and
handle the event in the content page:

1) Prepare an event argument class and a delegate (I assume the
DropDownList's SelectedValue proproperty is an Int32):

public class SelectedValueChangedArgs : EventArgs
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
public SelectedValueChangedArgs(int id)
{
_id = id;
}
}
public delegate void SelectedValueChangedHandler(object sender,
SelectedValueChangedArgs e);

2) Declare an event in the master page and raise it in the DropDownList's
SelectedIndexChanged event:

public event SelectedValueChangedHandler SelectedValueChanged;

protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
if (SelectedValueChanged != null)
{
SelectedValueChanged(this, new
SelectedValueChangedArgs(Int32.Parse( ddlType.SelectedValue)));
}
}

3) In the content page's ASPX source, add an @ MasterType directive so that
we can use a strongly typed Master reference:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

4) In the content page's code-behind class, handle the master page's
SelectedValueChanged event:

protected void Page_Load(object sender, EventArgs e)
{
Master.SelectedValueChanged += new
SelectedValueChangedHandler(Master_SelectedValueChanged);
}

void Master_SelectedValueChanged(object sender,
SelectedValueChangedArgs e)
{
int id = e.ID;
}

Hope this helps. Please feel free to post here if anything is unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Aussie Rules

Hi,

Thanks for you reply, but I am a bit lost on it.

My drop down list is not bound, I populate it in the page_load event in the
code behind file.

Is there a way in the dropdownlist.selectedindexchanged event (on master
page) to call sub on the content page. The sub on the content page would do
the work and update the grid itself. (the grid is also unbound).

Thanks.

Heath



Walter Wang said:
Hi Aussie,

Thank you for your post.

Based on my understanding, your question is how to refresh the DataGrid on
the content page when the DropDownList's selected value changes on the
master page. If I've misunderstood anything, please feel free to post
here.

If you're using DataSource's ControlParameter to select data, you can
actually reference the DropDownList in the content page directly:

<asp:DataGrid ID="grid1" runat="server" DataSourceID="SqlDataSource1">
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [CategoryName]
FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlType" Name="CategoryID"
PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

And here's the content of the master page:

<asp:DropDownList ID="ddlType" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1"
DataTextField="CategoryName" DataValueField="CategoryID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM
[Categories]"></asp:SqlDataSource>

=====

Another approach is to define an event in the master page, raise the event
in master page when the DropDownList's selected index is changed, and
handle the event in the content page:

1) Prepare an event argument class and a delegate (I assume the
DropDownList's SelectedValue proproperty is an Int32):

public class SelectedValueChangedArgs : EventArgs
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
public SelectedValueChangedArgs(int id)
{
_id = id;
}
}
public delegate void SelectedValueChangedHandler(object sender,
SelectedValueChangedArgs e);

2) Declare an event in the master page and raise it in the DropDownList's
SelectedIndexChanged event:

public event SelectedValueChangedHandler SelectedValueChanged;

protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
if (SelectedValueChanged != null)
{
SelectedValueChanged(this, new
SelectedValueChangedArgs(Int32.Parse( ddlType.SelectedValue)));
}
}

3) In the content page's ASPX source, add an @ MasterType directive so
that
we can use a strongly typed Master reference:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

4) In the content page's code-behind class, handle the master page's
SelectedValueChanged event:

protected void Page_Load(object sender, EventArgs e)
{
Master.SelectedValueChanged += new
SelectedValueChangedHandler(Master_SelectedValueChanged);
}

void Master_SelectedValueChanged(object sender,
SelectedValueChangedArgs e)
{
int id = e.ID;
}

Hope this helps. Please feel free to post here if anything is unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
W

Walter Wang [MSFT]

Hi,

You can make changes only in the Content Page:

protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddlType = (DropDownList) Master.FindControl("ddlType");
ddlType.SelectedIndexChanged += new
EventHandler(ddlType_SelectedIndexChanged);
}

void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlType = (DropDownList) sender;
object o = ddlType.SelectedValue;
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Aussie Rules

Hi,

I have been able to code where an event on the content page results in a
update on the master page. My learning example is as simple as entering text
into a text box on the content page and clicking a button, then using the
find control to find a label on the master page and update its .text
property.

My problem is the reverse of this. I want a control on the master page to
update the control on the content page.

Infact I have worked out that probably the best solution, is on the
selectedindexchange event, i actually just call the content page,and pass a
parameter on the URL string, then in the content page, pickup the parameter,
and do what i need to do in the page load event.

I have worked out the page.response.redirect, and will build the parameter
up in their, but how do I get a ?genre=1 in the content page.

THanks
 
W

Walter Wang [MSFT]

Hi Aussie,

The "?genre=1" is called QueryString, you can read it in Content Page's
Page_Load:

string s = Request.QueryString["genre"];
if (s != null && s.Length > 0)
{
int genre = Int32.Parse(s);
// handle this parameter
}

However, your approach will cause one additional postback.

Another solution is to handle the DropDownList's SelectedIndexChanged event
in the Master Page directly. We can use following code to find the Grid
control on the Content Page:

private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}

return null;
}

protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
DataGrid grid = (DataGrid) FindControlRecursive(Page, "grid1");
// control this DataGrid from Master Page
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top