0

I have a javascript function that is working fine in default.aspx when click on a link button. However, when I call this function from code behind, it cant work.

Here is part of my code in default.aspx :

function loadAdditionalInfoDialog(qtyId)
{
    alert(qtyId);
    var qty = document.getElementById(qtyId).value;
    alert(qty);
}

Here is part of my code in code behind (default.aspx.cs) when click on a button:

protected void btnRedeemAll_Click(object sender, EventArgs e){
    TextBox txtQty = (TextBox)itm.FindControl("txtQty");

    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script>loadAdditionalInfoDialog(" + txtQty.ClientID + ")</script>", false);
}

The alert(qtyId) is working for both side and print out the same word. (default page and code behind). But code behind fail to alert(qty). Anyone know what is my problem?

Noted that the qtyId is a text box id inside a repeater in default.aspx.

  • Does FindControl returns correct control, check it? – Nilesh Thakkar Jun 06 '13 at 09:32
  • I Response.Write(txtQty) and Response.Write(txtQty.ClientID). I found that the Response.Write(txtQty.ClientID) is actually same as the word that I do alert(qtyId) in the javascript function. Feel sorry if I doing stupid things. I am just a new to c# and programming. Do you means check like this? – Panadol Chong Jun 06 '13 at 09:40

5 Answers5

0

probably you are passing the wrong ClientID.

Use a java-script debugger (such as FireBug) and check if var qty = document.getElementById(qtyId); is not null (it will probably be).

Have also a look here

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • I use FireBug to check if var qty = document.getElementById(qtyId) is null or not. and I find out that it is null when I call loadAdditionalInfoDialog() in code behind. But it not null when call this function in default.aspx. But I not understand why it still can alert(qtyId), then come to next line, the var qty = document.getElementById(qtyId) become null. Sorry if I asking a stupid question, I just new to C# and programming. – Panadol Chong Jun 06 '13 at 09:23
0

Could you try below?

protected void btnRedeemAll_Click(object sender, EventArgs e){
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "  <script>loadAdditionalInfoDialog('" + txtQty.ClientID + "')</script>", false);
}

It seems you're missing single quotes around clientid being passed.

Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
0

Are you missing the quotes in the argument '" + txtQty.ClientID + "'

protected void btnRedeemAll_Click(object sender, EventArgs e){
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp"," <script>loadAdditionalInfoDialog('" + txtQty.ClientID + "')</script>", false);

}

Tapan Nallan
  • 1,762
  • 3
  • 17
  • 37
  • Look in the source and check whether the actual ID of the element is the same as that you are getting in the first alert. – Tapan Nallan Jun 06 '13 at 09:27
  • In the loadAdditionalInfoDialog(qtyId), the alert(qtyId) is working fine in both side, means it alert the same word when I call loadAdditionalInfoDialog() from code behind, and normal javascript function call in default.aspx. both alert the same word, which is : bodyContent_ucSearchGifts_repGiftResults_txtQty_0 But only code behind can perform the next line code. Suppose it can perform like normal? Sorry if I am asking a stupid question. I am just a new to c# and programming. – Panadol Chong Jun 06 '13 at 09:33
  • If you are getting the correct id in the first alert, it has nothing to do with the code-behind then, something is wrong in the javascript. If you could post the actual code, we could help you better – Tapan Nallan Jun 06 '13 at 09:39
  • Actually it is the actual code. I not sure what is the wrong with the javascript. But when I call the javascript function like normal (not calling from code behind), it is work. As in my mind, if it is work, suppose it also can work when call this function from code behind right? – Panadol Chong Jun 06 '13 at 09:45
  • Yes, it should work. There is nothing wrong in the code you posted. Check if you have overlooked something – Tapan Nallan Jun 06 '13 at 10:09
0

Try that:

Page.ClientScript.RegisterStartupScript(this.GetType(), "temp", "<script>loadAdditionalInfoDialog('" + txtQty.ClientID + "')</script>", false);

This is method for scripts that should be bound during initial page rendering.

el vis
  • 1,302
  • 2
  • 16
  • 32
  • No overload for method 'RegisterStartupScript' takes 5 arguments I get this error > – Panadol Chong Jun 06 '13 at 09:59
  • @PanadolChong but this one takes 4 parameters NOT 5, what is wrong? – el vis Jun 06 '13 at 10:01
  • Sory, just now got some mistake. I use back your code now, but the problem still same. TypeError: document.getElementById(...) is null var qty = document.getElementById(qtyId).value; – Panadol Chong Jun 06 '13 at 10:05
  • @PanadolChong how about that element is that something static on the page or you add this dynamically ? – el vis Jun 06 '13 at 10:29
  • I am new to programming, not sure how to consider it is dynamic or static. Basically it is as follow : There is a division, inside the division, there is a repeater, the repeater will display a table in website. the table get data from database table. If my database table have 9 row of data, then the repeater will display 9 row of data, if my database table have 10 rows, then the repeater will display 10 rows of data. – Panadol Chong Jun 07 '13 at 01:50
0

My guess is the problem is caused by that you're creating the control txtQty on the button click event handler.

This is a dynamically-created control and might not exist in HTML generated by the ASP.NET engine.

Try using a control that is on the page and that you added in design view and check if you get the same issue.

Update: Apologies. In the button click event handler you're trying to find the control. Why are you doing that? Is the control a dynamically-created control created somewhere on another part of the application?

Musa K
  • 1
  • 5
  • : There is a division, inside the division, there is a repeater, the repeater will display a table in website. the table get data from database table. If my database table have 9 row of data, then the repeater will display 9 row of data, if my database table have 10 rows, then the repeater will display 10 rows of data. Thus, those id are in the repeater, I only can use FindControl() to get control on the elements. – Panadol Chong Jun 07 '13 at 02:00