claracruz
asked on
Javascript asynchronous call help
Hello Experts,
I am using the following site as a guide to implementing Ajax in my website;- http://www.ajaxpro.info/quickguide.aspx
I have done the following;-
On my usercontrol page I have the following javascript;-
<script type="text/javascript">
function calculateCurrency()
{
Controls_CurrencyConverter .Calculate Currency(c alculateCu rrency_cal lback); // asynchronous call
}
//CalculateCurrency(calcul ateCurrenc y_callback );
// This method will be called after the method has been executed
// and the result has been sent to the client.
function calculateCurrency_callback (res)
{
theForm.lblResult2.value = res.value;
}
</script>
there is by label control and my image button;-
<input type="text" id="lblResult2" maxlength="5px" readonly="readonly" style="width:70px;" />
<img id="btnSubmit" alt="Submit" src="../images/button_conv ert.gif" onclick="calculateCurrency ();"/>
and in the codebehind of my user control I have the following;-
[AjaxPro.AjaxMethod]
public string CalculateCurrency()
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath( "../Rates. xml"));
DataRow[] rows = ds.Tables[0].Select("Curre ncy = '" + ddlConverter.SelectedItem. Text + "'");
decimal CurrencyAmount = Convert.ToDecimal(txtCurAm ount.Text) ;
decimal rate = Convert.ToDecimal(rows[0][ "Exchange" ]);
decimal amount = CurrencyAmount * rate;
return amount.ToString("f2");
}
catch (FormatException)
{
return "Error";
}
}
However, when I click submit "null" is returned rather than the correct value. what am I doing wrong?
PS: It works fine if I replace the HTML image button with an asp.net control and call it normally with the "protected void ImageButton1_Click(object sender, ImageClickEventArgs e)"
I am using the following site as a guide to implementing Ajax in my website;- http://www.ajaxpro.info/quickguide.aspx
I have done the following;-
On my usercontrol page I have the following javascript;-
<script type="text/javascript">
function calculateCurrency()
{
Controls_CurrencyConverter
}
//CalculateCurrency(calcul
// This method will be called after the method has been executed
// and the result has been sent to the client.
function calculateCurrency_callback
{
theForm.lblResult2.value = res.value;
}
</script>
there is by label control and my image button;-
<input type="text" id="lblResult2" maxlength="5px" readonly="readonly" style="width:70px;" />
<img id="btnSubmit" alt="Submit" src="../images/button_conv
and in the codebehind of my user control I have the following;-
[AjaxPro.AjaxMethod]
public string CalculateCurrency()
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(
DataRow[] rows = ds.Tables[0].Select("Curre
decimal CurrencyAmount = Convert.ToDecimal(txtCurAm
decimal rate = Convert.ToDecimal(rows[0][
decimal amount = CurrencyAmount * rate;
return amount.ToString("f2");
}
catch (FormatException)
{
return "Error";
}
}
However, when I click submit "null" is returned rather than the correct value. what am I doing wrong?
PS: It works fine if I replace the HTML image button with an asp.net control and call it normally with the "protected void ImageButton1_Click(object sender, ImageClickEventArgs e)"
ASKER
Hi orbulat,
This is not the problem, the onclick event works fine.
The probelm is that the result being reurned is "null" when I do;-
theForm.lblResult2.value = res.value;
and [Object] [object] if I do;-
theForm.lblResult2.value = res;
This is not the problem, the onclick event works fine.
The probelm is that the result being reurned is "null" when I do;-
theForm.lblResult2.value = res.value;
and [Object] [object] if I do;-
theForm.lblResult2.value = res;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for that.
Slight problem however, I am getting Object required for ddlConverterObj, I aassume because at runtime, my contrl name change to something like;-
ctl00_ctl00_BodyContent_Cu rrencyConv erter1_ddl Converter
How do I remedy this?
Thanks
Slight problem however, I am getting Object required for ddlConverterObj, I aassume because at runtime, my contrl name change to something like;-
ctl00_ctl00_BodyContent_Cu
How do I remedy this?
Thanks
yes, it is like this
it will append the "control names" level by level
if u want get the correct id of the control, u can do so in js and code behind, e.g. -->
function calculateCurrency(ddlConve rter, txtCurAmount)
{
var ddlConverterObj = document.getElementById(dd lConverter );
var selectedCur = ddlConverterObj.options[dd lConverter Obj.option s.selected Index].val ue;
var txtAmt = document.getElementById(tx tCurAmount ).value;
Controls_CurrencyConverter .Calculate Currency(t xtAmt, selectedCur, calculateCurrency_callback ); // asynchronous call
}
code behind -->
imgbutGo.Attributes.Add("o nClick","c alculateCu rrency('"+ ddlConvert er.ClientI D+"','"+tx tCurAmount .ClientID+ "');return false;);
it will append the "control names" level by level
if u want get the correct id of the control, u can do so in js and code behind, e.g. -->
function calculateCurrency(ddlConve
{
var ddlConverterObj = document.getElementById(dd
var selectedCur = ddlConverterObj.options[dd
var txtAmt = document.getElementById(tx
Controls_CurrencyConverter
}
code behind -->
imgbutGo.Attributes.Add("o
ASKER
Hi,
I had to make some changes due to errors, so I not have the following;-
btnSubmit.Attributes.Add(" onClick", "calculateCurrency('" + ddlConverter.ClientID + "','" + txtCurAmount.ClientID + "');return false;");
But it is doing a postback
I had to make some changes due to errors, so I not have the following;-
btnSubmit.Attributes.Add("
But it is doing a postback
ASKER
This is what is rendered for the button at runtime;-
<input type="image" name="ctl00$ctl00$BodyCont ent$Curren cyConverte r1$btnSubm it" id="ctl00_ctl00_BodyConten t_Currency Converter1 _btnSubmit " src="../images/button_conv ert.gif" onclick="calculateCurrency ('ctl00_ct l00_BodyCo ntent_Curr encyConver ter1_ddlCo nverter',' ctl00_ctl0 0_BodyCont ent_Curren cyConverte r1_txtCurA mount');re turn false;WebForm_DoPostBackWi thOptions( new WebForm_PostBackOptions(&q uot;ctl00$ ctl00$Body Content$Cu rrencyConv erter1$btn Submit&quo t;, "", true, "", "", false, false))" style="border-width:0px;" />
Am I doing anything wrong.
<input type="image" name="ctl00$ctl00$BodyCont
Am I doing anything wrong.
ASKER
Hi,
Sorry about the silly question, sorted all that out now.
But the inital problem still exists..
I still get [Object] [object] as my result...
I have tested the values being passed;-
alert(selectedCur);
alert(txtAmt);
And I am getting the correct values.
But the value being returned is the problem, I reckon there is something wrong with the line;-
Controls_CurrencyConverter .Calculate Currency(t xtAmt, selectedCur, calculateCurrency_callback )
particulary this bit;-
Controls_CurrencyConverter .Calculate Currency
Any ideas????
Sorry about the silly question, sorted all that out now.
But the inital problem still exists..
I still get [Object] [object] as my result...
I have tested the values being passed;-
alert(selectedCur);
alert(txtAmt);
And I am getting the correct values.
But the value being returned is the problem, I reckon there is something wrong with the line;-
Controls_CurrencyConverter
particulary this bit;-
Controls_CurrencyConverter
Any ideas????
i think it's better to test the ajax function bit by bit
1. don't pass any values to the function, and simply return a test string from the function.
ie
<script type="text/javascript">
function calculateCurrency()
{
Controls_CurrencyConverter .Calculate Currency(c alculateCu rrency_cal lback); // asynchronous call
}
function calculateCurrency_callback (res)
{
theForm.lblResult2.value = res.value;
}
</script>
codebehind
[AjaxPro.AjaxMethod]
public string CalculateCurrency()
{
return "test";
}
if this works, then u can try to pass a variable to the function (u can follow the example in ajaxpro's website)
u're getting [Object] [Object] for "res", and "null" for "res.value",
probably it's something wrong with the function in code behind, and those Text1.Text and ddlConvert.SelectedValue won't work in the function with it called by ajax.
maybe u also try with a normal server function first, to see if it returns the correct values
1. don't pass any values to the function, and simply return a test string from the function.
ie
<script type="text/javascript">
function calculateCurrency()
{
Controls_CurrencyConverter
}
function calculateCurrency_callback
{
theForm.lblResult2.value = res.value;
}
</script>
codebehind
[AjaxPro.AjaxMethod]
public string CalculateCurrency()
{
return "test";
}
if this works, then u can try to pass a variable to the function (u can follow the example in ajaxpro's website)
u're getting [Object] [Object] for "res", and "null" for "res.value",
probably it's something wrong with the function in code behind, and those Text1.Text and ddlConvert.SelectedValue won't work in the function with it called by ajax.
maybe u also try with a normal server function first, to see if it returns the correct values
imgbutGo.Attributes.Add("o
i guess you have to return false for those server side buttons, otherwise the page will be submitted.