I know there is a way to force asp web user controls (ascx) to use static ID's so they don't get appended with all the appended naming garbage...but is there way to do the same for the"name" attribute? Specifically, in this case, for DDL's? JQuery needs a specific name and I need to stop .NET from doing this.
Asked
Active
Viewed 5,837 times
5
-
2Why not just have jQuery use the ID? – Sam Sussman Apr 18 '12 at 19:52
-
I'm using the Validation plugin and it requires/used "name". – John Kinane Apr 18 '12 at 19:52
-
1http://stackoverflow.com/a/619950/284240 – Tim Schmelter Apr 18 '12 at 19:54
-
1@JohnKinane: If the link answers your question you might upvote that answer and delete your question. This is not a duplicate since you've asked something else. Apart from that i don't know a way how to force ASP.NET to generate static names. – Tim Schmelter Apr 18 '12 at 20:40
2 Answers
8
Not possible in ASP.Net for now ... I've found this combination to be an acceptable solution.
Add the ClientIDMode="Static"
to the control:
<asp:HiddenField ID="HiddenField1" runat="server" ClientIDMode="Static" />
Using JQuery, on document ready, set the name attribute on the control to the value of its id:
$(document).ready(function () {
// alert($("#HiddenField1").attr("name"));
$("#HiddenField1").attr("name", "HiddenField1");
// alert($("#HiddenField1").attr("name"));
});

Jorge Garcia
- 2,042
- 23
- 25
-
2Hmmmm if we know that Id is HiddenField1 why should we select it again ? :) – Beatles1692 Mar 10 '15 at 17:20
-
1BTW thanks for your answer. I used the same idea in one of my projects and that reminds me to up-vote your answer :) – Beatles1692 Mar 14 '15 at 08:45
-
1forgive my ignorance, but would this not break any binding? I know this would cause some issues in MVC. – Christian Cody Oct 01 '15 at 14:00
-
2@Christian Cody is right, for ASP.NET web forms it means the value of the control is not returned in the Viewstate, otherwise the solution works – Young Bob Sep 07 '17 at 11:18
-3
Try adding ClientIDMode="Static" to the control tag.
<asp:FileUpload ID="FileUpload1" runat="server" ClientIDMode="Static" />

Gary Broughton
- 3
- 3