Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
Does FindControl returns correct control, check it? – Nil Jun 6 at 9: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 6 at 9:40

5 Answers

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

share|improve this answer
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 6 at 9:23

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.

share|improve this answer
It still the same. – Panadol Chong Jun 6 at 9:25

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);

}

share|improve this answer
It still the same. – Panadol Chong Jun 6 at 9:24
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. – rusticbit Jun 6 at 9: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 6 at 9: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 – rusticbit Jun 6 at 9: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 6 at 9:45
show 1 more comment

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.

share|improve this answer
No overload for method 'RegisterStartupScript' takes 5 arguments I get this error >< – Panadol Chong Jun 6 at 9:59
@PanadolChong but this one takes 4 parameters NOT 5, what is wrong? – pawlakppp Jun 6 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 6 at 10:05
@PanadolChong how about that element is that something static on the page or you add this dynamically ? – pawlakppp Jun 6 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 7 at 1:50

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?

share|improve this answer
: 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 7 at 2:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.