|
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.
|
|
|
|
|
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
</script>
Hope this can help you.
|
|
|
|
|
Hello,
We need to store like an audit of certain tables in a database, which will have very sensitive information. We need to log all the users that opened this certain table, if modified store this information that has been changed and by whom.
I was planning to have trigger on the table, and write to another separate history table, when users access open the table.
Would this be the only way achieve this?
Are there any softwares that already do this?
Thanks so much for your time.
|
|
|
|
|
Restrict access to the tables via database security to only dba's. Create a separate user/users accounts for stored procedures. Add logging in the stored procedures.
Ideally, you should use Windows Security from start to finish but I constantly get overruled on using Integrated Security down to db layer so someone may know something that I don't ... actually it is because most domains have Authentication Delegation disabled on the Domain and it has to be setup to successful delegate credentials using kerberos. Sys admins don't like changing things, it seems when accounts have delegation enabled programmers can write malicious programs that act as other users just like pilot's have to go through metal detectors.
|
|
|
|
|
Hi,
I have a few textbox on my form and would like to validate it through Java script. I know there is RequiredFieldValidator in asp.net but I would like to do that by Javascript.
this is my code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="WebApplication1.Presentation.Register" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language= "javascript" type="text/javascript"></script>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
}
.style3
{
width: 153px;
height: 17px;
}
.style5
{
width: 151px;
}
.style6
{
width: 91px;
}
.style7
{
height: 23px;
}
.style8
{
height: 26px;
}
.style9
{
height: 25px;
}
.style10
{
width: 151px;
height: 23px;
}
.style11
{
width: 91px;
height: 23px;
}
</style>
<script language="javascript" type="text/javascript">
function StId_txt_onclick() {
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="4" class="style7">
<asp:Label ID="PrsnInfo_lbl" runat="server" Text="Personal Information"></asp:Label>
</td>
</tr>
<tr>
<td class="style9">
<asp:Label ID="StId_lbl" runat="server" Text="Student ID" Font-Names="Arial"
Font-Size="Smaller"></asp:Label>
</td>
<td class="style9" colspan="3">
<input id="StId_txt" runat="server"
style="border: 0.3px inset #000000; height: 20px; width: 137px;" type="text" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate= "StId_txt" ForeColor ="red"
ErrorMessage="Srudent ID is required!" Font-Names="Arial" Font-Size="Smaller"></asp:RequiredFieldValidator>
</td>
</table>
</div>
</form>
</body>
</html>
do you have any idea or sample please?
|
|
|
|
|
FWIW: While client-side validation may improve the user experience, you should always have validation on the server-side just to be safe.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I agree that server side validation is safe. still if you wish to proceed then please find below example to validate textbox.
var txtValue = document.getElementById(<"%txt1.ClientID%">).value;
if(txtValue==null || txtValue='')
{
alert('Please enter value in textbox.'); //message should be depends on requirement.
return false;
}
Let me know still if you have any doubts.
Regards
Keyur Satyadev
|
|
|
|
|
Hi,
I want to check if the string ends with '-0000' when all the remaining characters are numbers.
I tried Regex.IsMatch(mystring, @"\d*(0){4}") but it returns true even when there is no '-'
which regular expression to check with for a string which ends with '-0000'
|
|
|
|