|
How can I access a method on a child page from its master page ?
I have a Datagrid on my masterpage. Is acts as filter for the content and it´s pages below.
As I have the Selection Event after the PageLoad Event (Master and Child), I wana call the Content Page Comand directly from the masterpage event.
Thanks.
Guilherme
|
|
|
|
|
You can't really call a content page from the master page. The master page doesn't know what page is using it, that's the point. You can, however, create an event on the master page and have a content page subscribe to it.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
The Page property gives access to the content page.
|
|
|
|
|
True enough but the page object would need to be cast to an object that had the method needed, either as a concrete class or interface. The master page would need to know too much in my view.
It would also limit the ability to change the master page if required. I like the flexibility of subscribing to an event.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: It would also limit the ability to change the master page if required
Not if you put one method per interface.
|
|
|
|
|
So adding more and more interfaces and implementations when more functionality is needed? Rube Goldberg would be proud.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Events do have at least one distinct advantage. They are a public part of the class, which makes it more obvious that the functionality is available.
|
|
|
|
|
I will assume you are using C#. Use this.Page .
First, you will create an interface that defines the methods you want to call (I put this code in INamedObject.cs):
namespace WebApplication70
{
public interface INamedObject
{
string GetName();
}
}
Then, your Primary.Master will access this.Page and cast it as an INamedObject to call GetName() :
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Primary.master.cs" Inherits="WebApplication70.Primary" %>
<%@ Import Namespace="WebApplication70" %>
<html>
<head runat="server">
<title>Master/Page Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1><asp:Label runat="server" ID="lblPageName" /></h1>
<asp:ContentPlaceHolder ID="body" runat="server" />
</div>
</form>
</body>
</html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string pageName;
if (this.Page is INamedObject)
{
pageName = (this.Page as INamedObject).GetName();
}
else
{
pageName = "Unknown Page";
}
lblPageName.Text = pageName;
}
</script>
Create a page, Default.aspx, that makes use of the masterpage:
<%@ Page Language="C#" MasterPageFile="~/Primary.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication70.Default" %>
<asp:Content ContentPlaceHolderID="body" runat="server">
<p>This is the default page.</p>
</asp:Content>
In the code behind (Default.aspx.cs), you will implement the interface:
namespace WebApplication70
{
public partial class Default : System.Web.UI.Page, INamedObject
{
public string GetName()
{
return "Default.aspx";
}
}
}
|
|
|
|
|
I'd avoid doing this though, as far as possible. By implementing in this way you increase the coupling between the master page and this particular page. If this pattern were to be repeated it would result in a maintenance nightmare.
Depending on the reason the OP wants to pass information up to the MasterPage, there are normally other mechanisms.
|
|
|
|
|
Often times, the purpose of the masterpage is to reuse content, and coupling isn't a factor. Besides, my example isn't tightly coupled... it makes use of an interface, which allows any page to implement the desired behavior (or not).
Still, an event driven technique (observer pattern) may be cleaner, though I'm not sure because I can't tell exactly what the OP wants to do.
|
|
|
|
|
Oh I know what a MasterPage is for, and what you suggest breaks the intended pattern which spells trouble. You class mightn't be tightlight coupled, but it is more tightly coupled and this is a factor in maintainability. The eventing mechanism is just plain cleaner.
|
|
|
|
|
Keith Barrow wrote: The eventing mechanism is just plain cleaner.
Maybe, depending on the OP's needs. Could you give a clear code example of what you are envisioning?
|
|
|
|
|
I'm not going to code it (1am here & I don't need the head-fry), but the process is simple enough:
1. Add an event to the masterpage, DatagridChanged or somesuch
2. The child subscribes to the event
3. When the datagrid on the masterpage is changed , the the event is raised and the subscribed handler does the work on the child.
Note the MasterPage is totally agnostic of the the Child, giving pure decoupling in that direction and the child can access any data it might need from the master page. You can even probably hook into the datagrid's own events, even neater.
[edit]
Also note I have corrected what I originally said: I got it totally wrong, it was 1:20am after all. now I'm off to the land of nod.
|
|
|
|
|
through means of below code i add radiobutton to my gridview.
grdFeedback.DataSource = ds;
grdFeedback.DataBind();
foreach (GridViewRow row in grdFeedback.Rows)
{
if (row.Cells[3].Text == "" && row.Cells[4].Text == "" && row.Cells[5].Text == "" && row.Cells[6].Text == "" && row.Cells[7].Text == "")
{
TextBox txtAns = new TextBox();
txtAns.ID = "txtAns";
row.Cells[3].Controls.Add(txtAns);
}
else
{
for (int c = 3; c <= 7; c++)
{
if (row.Cells[c].Text != " ")
{
RadioButton rbtn = new RadioButton();
string rbt = "rbtngrp";
string opt = row.Cells[c].Text;
rbtn.ID = rbt + row.RowIndex + c;
rbtn.Text = opt.ToString();
rbtn.GroupName = rbt + row.RowIndex;
row.Cells[c].Controls.Add(rbtn);
}
}
}
}
how to check it whether the button is checked or not in an button click?. The below code i used. but its error : 'grdFeedback.Rows[0].Cells[4].Controls[2]' threw an exception of 'System.ArgumentOutOfRangeException' bool {System.ArgumentOutOfRangeException}
((CheckBox)(grdFeedback.Rows[0].Cells[4].Controls[2])).Checked
|
|
|
|
|
First of all, you shouldn't be finding controls by indices. For one thing, it is confusing. Moreover, if you changed the order of your columns, you'll end up breaking your code.
Instead, use the row.FindControl method.
Another thing to check for the row type, since a grid may have several row types (header/footer/data/..)
So while you checkbox exists in the DataRow, it doesn't exist in the header row for example, which in turn probably causes your exception.
So you should do something like this:
foreach (GridViewRow row in grdFeedback.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
RadioButton btn=row.FindControl("RadioButton_ID_Goes_Here") as RadioButton;
bool isChecked = btn.Checked;
}
}
|
|
|
|
|
thanks for ur immediate reply roman.
but the this i have implemeted. but the same error.
((RadioButton)grdFeedback.Rows[0].Cells[3].FindControl("rbtn03")).Checked
but it returns error : ((System.Web.UI.WebControls.CheckBox)(((System.Web.UI.WebControls.RadioButton)(grdFeedback.Rows[0].Cells[3].FindControl("rbtn03"))))) is null
Also i dont know y its showing "System.Web.UI.WebControls.CheckBox" in error message.
plz help me
Thanks in advance
|
|
|
|
|
I'm not sure how you set your controls in the datagrid, but
((RadioButton)grdFeedback.Rows[0]<big>.Cells[3]</big>.FindControl("rbtn03")).Checked shouldn't be needed.
Try this instead
((RadioButton)grdFeedback.Rows[0].FindControl("rbtn03")).Checked
As a rule of thumb, you shouldn't rely on indexes to fetch controls.
Also you need to make sure that the row is a data row, as the header row won't have the radiobutton
|
|
|
|
|
guys;
i have a CustomValidator on a page that doesn't call its ClientValidationFunction.
can any one tell me what can cause this;
Help people,so poeple can help you.
|
|
|
|
|
I had to set ValidateEmtyText to true.
I knew that when i found this code in some web resources downloaded from the server:
function CustomValidatorEvaluateIsValid(val) {
var value = "";
if (typeof(val.controltovalidate) == "string") {
value = ValidatorGetValue(val.controltovalidate);
if ((ValidatorTrim(value).length == 0) &&
((typeof(val.validateemptytext) != "string") || (val.validateemptytext != "true"))) {
return true;
}
}
var args = { Value:value, IsValid:true };
if (typeof(val.clientvalidationfunction) == "string") {
eval(val.clientvalidationfunction + "(val, args) ;");
}
return args.IsValid;
}
100
Help people,so poeple can help you.
|
|
|
|
|
Hi,
If I created page and inherit master page in it then i need to add JavaScript code to this page in the head tag , how I can make it because the head tag can not appear in the web page if is it inherit the master page on it .
thanks .
|
|
|
|
|
You can put your javascript code script tag in the Content page also.
Its not necessary that you keep your javascript code in head tag.
Search google. you will get a lot of samples.
|
|
|
|
|
In master page you can put contentplaceholder inside head tag,
and in the content pages add the script tag to the contentplaceholder,
in this way the script will go to head tag at runtime
cheers
============================================
The grass is always greener on the other side of the fence
|
|
|
|
|
can you give me example please ???
|
|
|
|
|
|
over my head, sir;
100
Help people,so poeple can help you.
|
|
|
|