OK, the entire code is below. This is a base class, which the page I'm working with inherits from. That page calls EstablishCancelConfirmation in its Page_Load() method, passing the name of the "cancel" button ("btnCancel"), the text of the do-you-want-to-cancel-and-lose-your-changes
message, and the value of IsPostBack as parameters. That page also includes a hidden field defined as <asp:HiddenField id="txtIsDirty" runat="server" value="initial value" />.
The whole point of all this is that I need to be able to detect whether any control values have changed, and if so warn the user when they click the "cancel" button without saving. That functionality works fine as long as a postback hasn't yet occurred,
but once there is a postback the value of the javascript variable IsDirty is lost, and I've been absolutely tearing my hair out trying to find a way to preserve the value of that variable across postbacks. I'm really surprised, and incredibly frustrated,
that I've had to go through a two-week ordeal trying to implement what seems like a really basic piece of functionality. I've re-arranged this in dozens (if not hundreds) of different ways, and no matter what I try, I always end up either losing the value
after a postback or hanging up on some javascript error (usually "object required").
I'll be eternally grateful for any working solution to this problem.
ScriptStudios, I can't use your approach in this case, because all this is being specified in a base class, from which all my edit pages inherit. Since the base class doesn't include txtIsDirty (or any other controls), when I attempt to reference as in
your example I get a compile error.
ASilverblatt
Member
22 Points
47 Posts
lost in javascript hell ("object required" error)
Feb 25, 2010 07:40 PM|LINK
Running the following code:
<script language="JavaScript">
var DirtyField = document.getElementById('txtIsDirty.ClientID');
var IsDirty = DirtyField.value;
...
I'm getting an "Object required" error at the beginning of the "var IsDirty = DirtyField.value;" line. The code look fine to me. What am I missing?
javascipt object required
karan@dotnet
All-Star
26228 Points
4596 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:26 PM|LINK
Should be :
<script type="text/javaScript"> var DirtyField = document.getElementById('<%=txtIsDirty.ClientID%>'); var IsDirty = DirtyField.value;Karan
~ Blog ~
Remember To Mark The Post(s) That Helped You As The ANSWER
ScriptStudio...
Member
650 Points
141 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:26 PM|LINK
MakeJavascript()
{
String str = "<script type=\"text/javascript\">" +
"function SetHidden() {" +
"var DirtyField = document.getElementById('" + txtIsDirty.ClientID + "');" +
"var IsDirty = DirtyField.value;" +
"</script>";
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "DirtyText", str, false);
}
The var DirtyField line has getElementById('" <-- that's single quote, then double quote
N_EvilScott
Star
8179 Points
1466 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:31 PM|LINK
You need to enclose it in asp tags. Try this it works for me
var DirtyField = document.getElementById('<%= txtIsDirty.ClientID %>');
you can also get the value direction from that by adding value to the end. so it would look like
var IsDirty = document.getElementById('<%= txtIsDirty.ClientID %>').value;
ASilverblatt
Member
22 Points
47 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:35 PM|LINK
Karan, I made the change you suggested, and now have this:
<script language="JavaScript">
var DirtyField = document.getElementById('<%=txtIsDirty.ClientID%>');
var IsDirty = DirtyField.value;
I still get the same error at the same place.
karan@dotnet
All-Star
26228 Points
4596 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:38 PM|LINK
The code that I posted does not has any errors.
Make sure you dont have any other syntax errors in your javasript and match the ids passed with the id you have in your page.
Or else post your entire page code here
Karan
~ Blog ~
Remember To Mark The Post(s) That Helped You As The ANSWER
ASilverblatt
Member
22 Points
47 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:39 PM|LINK
N EvilScott, I tried your second suggestion, yielding this code:
<script language="JavaScript">
var IsDirty = document.getElementById('<%= txtIsDirty.ClientID %>').value;
Again, I'm getting the "Object required" error on the assignment to IsDirty.
A1ien51
All-Star
29935 Points
5821 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:44 PM|LINK
Are you calling it before the textbox is loaded?
Is it a runat="server" control?
Eric
ASilverblatt
Member
22 Points
47 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 08:59 PM|LINK
OK, the entire code is below. This is a base class, which the page I'm working with inherits from. That page calls EstablishCancelConfirmation in its Page_Load() method, passing the name of the "cancel" button ("btnCancel"), the text of the do-you-want-to-cancel-and-lose-your-changes message, and the value of IsPostBack as parameters. That page also includes a hidden field defined as <asp:HiddenField id="txtIsDirty" runat="server" value="initial value" />.
The whole point of all this is that I need to be able to detect whether any control values have changed, and if so warn the user when they click the "cancel" button without saving. That functionality works fine as long as a postback hasn't yet occurred, but once there is a postback the value of the javascript variable IsDirty is lost, and I've been absolutely tearing my hair out trying to find a way to preserve the value of that variable across postbacks. I'm really surprised, and incredibly frustrated, that I've had to go through a two-week ordeal trying to implement what seems like a really basic piece of functionality. I've re-arranged this in dozens (if not hundreds) of different ways, and no matter what I try, I always end up either losing the value after a postback or hanging up on some javascript error (usually "object required").
I'll be eternally grateful for any working solution to this problem.
------------------------------------------------------------------
public void EstablishCancelConfirmation(WebControl CancelButton, string ConfirmationMessage, bool IsPostBack)
{
const string OnChangeScriptBlockName = "on-change-script-block";
string CrLf = Environment.NewLine;
ClientScriptManager csm = this.ClientScript;
// register script
if (! csm.IsClientScriptBlockRegistered(OnChangeScriptBlockName))
{
if (IsPostBack)
{
// this script inspects both the IsDirty variable and the txtIsDirty hidden form field
csm.RegisterClientScriptBlock(this.GetType(), OnChangeScriptBlockName,
"<script language=\"JavaScript\">" + CrLf +
"var IsDirty = document.getElementById('<%= txtIsDirty.ClientID %>').value;" + CrLf +
"function AnyFieldChanges(msg) {if (IsDirty) return confirm(msg); else return true;}" +
CrLf + "</script>");
}
else
{
// this script inspects only the IsDirty variable
csm.RegisterClientScriptBlock(this.GetType(), OnChangeScriptBlockName,
"<script language=\"JavaScript\">" + CrLf + "var IsDirty = false;" + CrLf +
"function AnyFieldChanges(msg) {if (IsDirty) return confirm(msg); else return true;}" +
CrLf + "</script>");
}
}
// call MonitorAllControls to set up change monitoring on all web controls on the page
MonitorAllControls(this.Controls);
// activate confirmation dialog box for the control designnated as the cancel button
CancelButton.Attributes.Add("onclick", "return AnyFieldChanges('" + ConfirmationMessage.Replace("'", "\'") + "');");
}
public void MonitorAllControls(ControlCollection collection)
{
WebControl wc;
foreach (Control c in collection)
{
if (c is WebControl)
{
wc = (WebControl) c;
if ((wc is CheckBox) || (wc is CheckBoxList) || (wc is RadioButtonList))
{
wc.Attributes.Add("onclick", "IsDirty = true;");
}
else
{
if (wc is Button)
{
wc.Attributes.Add("onclick", "document.getElementById('txtIsDirty').value = IsDirty;");
}
else
{
wc.Attributes.Add("onchange", "IsDirty = true;");
}
}
// next two lines required as work-around to IE onchange bug when AutoComplete is enabled
wc.Attributes.Add("onfocus", "this.origValue = this.value;");
wc.Attributes.Add("onblur", "IsDirty |= this.value != this.origValue;");
}
MonitorAllControls(c.Controls);
}
}
ASilverblatt
Member
22 Points
47 Posts
Re: lost in javascript hell ("object required" error)
Feb 25, 2010 09:06 PM|LINK
ScriptStudios, I can't use your approach in this case, because all this is being specified in a base class, from which all my edit pages inherit. Since the base class doesn't include txtIsDirty (or any other controls), when I attempt to reference as in your example I get a compile error.