0

I've a table in my ASP.NET app, and add rows dynamically to it, each row has some cells each including a textbox, I generate everything dynamically, for instance:

                tc = new TableCell();
                TextBox t_total = new TextBox();
                t_total.ID = "txtTotal" + dt.Rows[i]["Id"].ToString();
                tc.Controls.Add(t_total);
                tr.Cells.Add(tc);

I set an ID for my textbox, then I use this ID to access this object in a JavaScript function. How can I change this ID in my JavaScript function? for instance my textbox ID is set to "txtTotal1000" initially, then I'm going to change its ID to "txtTotal2000" in my JavaScript function so that I can access it using this new ID, how can I do so?

10
  • 4
    Why would you do that? It would inevitably break something on postback. YOu may even get errors (invalid viewstate) just because you modified the form.The runtimie would almost certainly see that as a possible attack and block postbacks. Why owuld you not just use the correct ID from your JavaScript? A MUCH better option, if you are simply having problems determining what the ID is in client script because you can't predict how the runtime will generate the ID can be found here: stackoverflow.com/questions/641280/… Commented Oct 11, 2012 at 16:26
  • Well I have a situation in which I should change my ID, I know what you say can bother me, but I'm not going to use data after postback, I will read data using javascript, is it possible at all to change ID using JavaScript? Commented Oct 11, 2012 at 16:30
  • I don't think so, but I'll wait and see if someone else has an idea. Personally, I wouldn't bother with it even if you COULD do it. It's just so much easier to use established techniques than to come up with something non-standard. Determining the ID so that you can use the auto-generated ID in side script is a solved problem. Everybody runs into the need sooner or later and the way to handle it is established. Changing the ID is not. It sure makes it easier on the maintenance programmer. But it's entirely possible someone knows how to do this. Commented Oct 11, 2012 at 16:40
  • 1
    I don't think that there is enough informaiton here to suggest a better approach based on that. It sounds to me like there's a flaw in the underlying design concept. I don't know if you're working on a screen where the customer can order the food, or a maintenance screen where the menu is maintained, or something else, but either one of these would lead to a different approach to what you want to accomplish. I don't think the question of "other approaches" is answerable with the given information, and I suspect it would be a bad fit for the site if we did have enough info. Commented Oct 11, 2012 at 16:59
  • 1
    It almost seems as if you're making what's known as a "category mistake" - you're asking the wrong question based on a misunderstanding of some basic ASP.NET concepts. Like you're asking how to make a motorcycle that can cross a river (you need a boat, not a motorcycle). Is your background in .NET, or in other lanuguages? A lot of concepts you would use in other frameworks don't readily apply to .NET. Commented Oct 11, 2012 at 17:00

1 Answer 1

1

You can use jQuery for that

$("#txtTotal1000").attr('id','txtTotal2000');

This will change the id of the element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.