Hello All,
I'm a new ASP.NET programmer and I want to create a custom control
consist of two properties (Number1) and (Number2) and both of them are
integers and default value = 0,
now I'm using callback technique so I can change those two proprties
via ChangeNumber1 and ChangeNumber2 functions.
what happens is that when I change the Number1 for to number and get
the result of the callback event and trying to change the second number
I always get the default value of Number1 (which is 0) and always the
result return 0;
I know that I can pass the two new properties value via the callback
function but I want it works like this.
anybody can help ?
================================================== ======================
My Custom Contorl code is:
[ToolboxData("<{0}:MyCalculator runat=server></{0}:MyCalculator>")]
public class MyCalculator : WebControl, ICallbackEventHandler
{
private const int _number1Default = 0;
private const string _number1DefaultViewState = "Number1";
[Bindable(true)]
[DefaultValue(_number1Default)]
[Description("The first number")]
[Localizable(true)]
public int Number1
{
get
{
object obj = ViewState[_number1DefaultViewState];
return (obj != null) ? (int)obj : 0;
}
set
{
ViewState[_number1DefaultViewState] = value;
}
}
private const int _number2Default = 0;
private const string _number2DefaultViewState = "Number2";
[Bindable(true)]
[DefaultValue(_number2Default)]
[Description("The first number")]
[Localizable(true)]
public int Number2
{
get
{
object obj = ViewState[_number2DefaultViewState];
return (obj != null) ? (int)obj : 0;
}
set
{
ViewState[_number2DefaultViewState] = value;
}
}
protected override void OnPreRender(EventArgs e)
{
ClientScriptManager csm = this.Page.ClientScript;
{ // Number1 Script Register
String cbNumber1Ref = csm.GetCallbackEventReference(this,
"eventArgument",
"GetMultiplyResult", "context");
StringBuilder sbNumber1CallbackScript = new
StringBuilder();
sbNumber1CallbackScript.Append("\n");
sbNumber1CallbackScript.Append("function
ChangeNumber1(eventArgument, context)\n");
sbNumber1CallbackScript.Append("{\n");
sbNumber1CallbackScript.Append(" " + cbNumber1Ref);
sbNumber1CallbackScript.Append(";\n}");
// Register script blocks will perform call to the server.
if (!csm.IsClientScriptBlockRegistered(this.GetType() ,
"OnChangeNumber1"))
{
csm.RegisterClientScriptBlock(this.GetType(),
"OnChangeNumber1",
sbNumber1CallbackScript.ToString(), true);
}
}
{ // Number2 Script Register
String cbNumber2Ref = csm.GetCallbackEventReference(this,
"eventArgument",
"GetMultiplyResult", "context");
StringBuilder sbNumber2CallbackScript = new
StringBuilder();
sbNumber2CallbackScript.Append("\n");
sbNumber2CallbackScript.Append("function
ChangeNumber2(eventArgument, context)\n");
sbNumber2CallbackScript.Append("{\n");
sbNumber2CallbackScript.Append(" " + cbNumber2Ref);
sbNumber2CallbackScript.Append(";\n}");
// Register script blocks will perform call to the server.
if (!csm.IsClientScriptBlockRegistered(this.GetType() ,
"OnChangeNumber2"))
{
csm.RegisterClientScriptBlock(this.GetType(),
"OnChangeNumber2",
sbNumber2CallbackScript.ToString(), true);
}
}
}
public override void RenderControl(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.B);
writer.Write("This is My Control");
writer.RenderEndTag();
}
private int _result;
void ICallbackEventHandler.RaiseCallbackEvent(string
eventArgument)
{
string[] args = eventArgument.Split(':');
switch (args[0])
{
case "Number1":
{
Number1 = int.Parse(args[1]);
}
break;
case "Number2":
{
Number2 = int.Parse(args[1]);
}
break;
}
_result = Number1 * Number2;
}
string ICallbackEventHandler.GetCallbackResult()
{
return _result.ToString();
}
}
================================================== ======================
And the page that uses it is:
================================================== ======================
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="WebControlLibrary1"
Namespace="WebControlLibrary1" TagPrefix="cc1" %>
<!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>Untitled Page</title>
<script type="text/javascript">
function OnChangeNum1()
{
var cmb1 = document.getElementById("Select1");
ChangeNumber1(cmb1.selectedIndex);
}
function OnChangeNum2()
{
var cmb2 = document.getElementById("Select2");
ChangeNumber2(cmb2.selectedIndex);
}
function GetMultiplyResult(mulResult)
{
document.getElementById("Text1").value = mulResult;
}
</script>
<script src="ClientScript.js" type="text/javascript">
// <!CDATA[
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<strong>First Number :</strong>
<select id="Select1" onchange="OnChangeNum1()" style="width:
121px" onclick="return Select1_onclick()">
<option selected="selected">0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<br />
<strong>Second Number:</strong> <select id="Select2"
onchange="OnChangeNum2()" style="width: 121px">
<option selected="selected">0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select><br />
<br />
<strong>Result:</strong>
<input id="Text1" type="text" style="width: 170px" /><br />
<br />
<cc1:MyCalculator ID="MyCalculator1" runat="server"
Width="257px" />
</div>
</form>
</body>
</html>
================================================== ======================
Thanks
Omar alani 2 2490
Try saving the values to Session state instead of ViewState.
You see, ViewState is not being persisted because it normally gets rendered
along with the page but in the case of callbacks you are not re-rendering the
page.
--
I hope this helps,
Steve C. Orr
MCSD, MVP http://Steve.Orr.net
"Alani" wrote: Hello All,
I'm a new ASP.NET programmer and I want to create a custom control consist of two properties (Number1) and (Number2) and both of them are integers and default value = 0,
now I'm using callback technique so I can change those two proprties via ChangeNumber1 and ChangeNumber2 functions.
what happens is that when I change the Number1 for to number and get the result of the callback event and trying to change the second number I always get the default value of Number1 (which is 0) and always the result return 0;
I know that I can pass the two new properties value via the callback function but I want it works like this.
anybody can help ?
================================================== ====================== My Custom Contorl code is: [ToolboxData("<{0}:MyCalculator runat=server></{0}:MyCalculator>")] public class MyCalculator : WebControl, ICallbackEventHandler { private const int _number1Default = 0; private const string _number1DefaultViewState = "Number1";
[Bindable(true)] [DefaultValue(_number1Default)] [Description("The first number")] [Localizable(true)] public int Number1 { get { object obj = ViewState[_number1DefaultViewState]; return (obj != null) ? (int)obj : 0; } set { ViewState[_number1DefaultViewState] = value; } }
private const int _number2Default = 0; private const string _number2DefaultViewState = "Number2";
[Bindable(true)] [DefaultValue(_number2Default)] [Description("The first number")] [Localizable(true)] public int Number2 { get { object obj = ViewState[_number2DefaultViewState]; return (obj != null) ? (int)obj : 0; } set { ViewState[_number2DefaultViewState] = value; } }
protected override void OnPreRender(EventArgs e) { ClientScriptManager csm = this.Page.ClientScript;
{ // Number1 Script Register String cbNumber1Ref = csm.GetCallbackEventReference(this, "eventArgument", "GetMultiplyResult", "context");
StringBuilder sbNumber1CallbackScript = new StringBuilder(); sbNumber1CallbackScript.Append("\n"); sbNumber1CallbackScript.Append("function ChangeNumber1(eventArgument, context)\n"); sbNumber1CallbackScript.Append("{\n"); sbNumber1CallbackScript.Append(" " + cbNumber1Ref); sbNumber1CallbackScript.Append(";\n}");
// Register script blocks will perform call to the server. if (!csm.IsClientScriptBlockRegistered(this.GetType() , "OnChangeNumber1")) { csm.RegisterClientScriptBlock(this.GetType(), "OnChangeNumber1", sbNumber1CallbackScript.ToString(), true); } }
{ // Number2 Script Register String cbNumber2Ref = csm.GetCallbackEventReference(this, "eventArgument", "GetMultiplyResult", "context");
StringBuilder sbNumber2CallbackScript = new StringBuilder(); sbNumber2CallbackScript.Append("\n"); sbNumber2CallbackScript.Append("function ChangeNumber2(eventArgument, context)\n"); sbNumber2CallbackScript.Append("{\n"); sbNumber2CallbackScript.Append(" " + cbNumber2Ref); sbNumber2CallbackScript.Append(";\n}");
// Register script blocks will perform call to the server. if (!csm.IsClientScriptBlockRegistered(this.GetType() , "OnChangeNumber2")) { csm.RegisterClientScriptBlock(this.GetType(), "OnChangeNumber2", sbNumber2CallbackScript.ToString(), true); } } } public override void RenderControl(HtmlTextWriter writer) { writer.RenderBeginTag(HtmlTextWriterTag.B); writer.Write("This is My Control"); writer.RenderEndTag(); }
private int _result; void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) { string[] args = eventArgument.Split(':');
switch (args[0]) { case "Number1": { Number1 = int.Parse(args[1]); } break;
case "Number2": { Number2 = int.Parse(args[1]); } break; }
_result = Number1 * Number2; }
string ICallbackEventHandler.GetCallbackResult() { return _result.ToString(); } } ================================================== ======================
And the page that uses it is: ================================================== ====================== <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="WebControlLibrary1" Namespace="WebControlLibrary1" TagPrefix="cc1" %>
<!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>Untitled Page</title> <script type="text/javascript"> function OnChangeNum1() { var cmb1 = document.getElementById("Select1"); ChangeNumber1(cmb1.selectedIndex); }
function OnChangeNum2() { var cmb2 = document.getElementById("Select2"); ChangeNumber2(cmb2.selectedIndex); }
function GetMultiplyResult(mulResult) { document.getElementById("Text1").value = mulResult; } </script> <script src="ClientScript.js" type="text/javascript"> // <!CDATA[
// ]]> </script> </head> <body> <form id="form1" runat="server"> <div> <br /> <strong>First Number :</strong> <select id="Select1" onchange="OnChangeNum1()" style="width: 121px" onclick="return Select1_onclick()"> <option selected="selected">0</option> <option>1</option> <option>2</option> <option>3</option> </select> <br /> <strong>Second Number:</strong> <select id="Select2" onchange="OnChangeNum2()" style="width: 121px"> <option selected="selected">0</option> <option>1</option> <option>2</option> <option>3</option> </select><br /> <br /> <strong>Result:</strong> <input id="Text1" type="text" style="width: 170px" /><br /> <br /> <cc1:MyCalculator ID="MyCalculator1" runat="server" Width="257px" /> </div> </form> </body> </html> ================================================== ======================
Thanks Omar alani
Thank you Steve for your reply.
your suggesstion worked correctly, BUT when I changed the class
declaration to add some initial value for the Number1 and Number2
properties, I got an exception because the Page.Session variable was
null in the set section of the property, I mean if I changed the class
declaration to be as follows:
================================================== ===================================
[ToolboxData("<{0}:MyCalculator runat=server Number1=0
Number2=0></{0}:MyCalculator>")]
and change the property to be as follows:
[Bindable(true)]
[DefaultValue(_number1Default)]
[Description("The first number")]
[Localizable(true)]
public int Number1
{
get
{
object obj = null;
if (Page.Session != null)
{
//get a property unique name by adding the clientId for
this control to the original property name
string Number1ID = string.Format("{0}_Number1",
this.ClientID);
obj = Page.Session[Number1ID];
}
return (obj != null) ? (int)obj : 0;
}
set
{
if (Page.Session != null)
{
string Number1ID = string.Format("{0}_Number1",
this.ClientID);
Page.Session[Number1ID] = value;
}
}
}
================================================== ===================================
Now, Is this what you mean or you meant something else ?
Is my code right or wrong ?
Thanks again,
Omar alani This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jim Hammond |
last post by:
It would be udeful to be able to get the current on-screen values from a
FormView that is databound to an ObjectDataSource by using a callback instead
of a postback. For example:
public void...
|
by: Sam Kong |
last post by:
Hello!
I got recently intrigued with JavaScript's prototype-based
object-orientation.
However, I still don't understand the mechanism clearly.
What's the difference between the following...
|
by: Mark Ingram |
last post by:
Hi, ive written a custom control and i would like to display the
"invalid property value" dialog box for certain properties on my form if
the user enters a value outside the allowed range (in the...
|
by: yp.yean |
last post by:
Hi,
I created a custom control, and encountered a dirty property value
persistence problem.
I created a property with a custom class type, call SQLSettings which
holds the SQL connection...
|
by: shapper |
last post by:
Hello,
I am creating a custom control and I have the following property:
<Bindable(True), Category("Appearance"), DefaultValue(""),
Localizable(True)Property Text() As String
Get
Dim s As...
|
by: Nemisis |
last post by:
Hi everyone,
I am wondering if anyone has tried the following saving values from a
page to a database using CallBack? is this possible?
Basically instead of pressing a button that contacts...
|
by: TS |
last post by:
I have a custom textbox that i need to access a label's text property. the
label and textbox are 2 separate controls that will be on my page. Inside my
custom control i want to have access to this...
|
by: balajipkn |
last post by:
Hi,
I am trying to pass parameters from WIX to C# code, but I am not able to do it properly. To code that I have written on WIX side is:
<Property Id="teststring">
<!]>
</Property>...
|
by: Fabio Mastria |
last post by:
Hi all!
In a my simple project I use callback to fill a dropdownlist with xml data
returned by a web service, based on a value which is input via another
dropdownlist.
NOTE: I can't use...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |