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?

Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115
  • 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: http://stackoverflow.com/questions/641280/reference-asp-net-control-by-id-in-javascript – David Oct 11 '12 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? – Ali_dotNet Oct 11 '12 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. – David Oct 11 '12 at 16:40
  • Why exactly do you need to change the ID? – Shmiddty Oct 11 '12 at 16:44
  • I display some foods in a restaurant menu, each food has an ID which I use it as ID of my text boxes (each food is in a row and has text boxes for count, price), user can change foods in a combobox, when user changes food, I should be able to change ID of my textboxes so that I can access my textboxes accordingly, can you suggest me another approach? – Ali_dotNet Oct 11 '12 at 16:49
  • 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. – David Oct 11 '12 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. – David Oct 11 '12 at 17:00
  • well David, in fact I've done a lot in .NET, I'm an ASP.NET developer, and I've done a lot in JavaScript, but I thing my concepts have gone wrong! I should try to correct my underlying concept then I can correct it in code, thanks for your advices – Ali_dotNet Oct 11 '12 at 17:06
  • OK. I hope you didn't take any of that as negative. I was trying to be constructive, honest, and helpful. I didn't mean any of that negatively and I admit I may have been off-base. I wish you luck. – David Oct 11 '12 at 17:16
  • Thanks dear David, in fact what you said about possible postback problems alerted me about my wrong approach – Ali_dotNet Oct 11 '12 at 17:20

1 Answers1

1

You can use jQuery for that

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

This will change the id of the element.

G . R
  • 543
  • 1
  • 6
  • 12