Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

asp.net 4.0 - Can I force asp to set name the same as id

I need to process a form full of all sorts of different search controls, however these search controls are now inside a master page and so the id's were getting extra junk added in ('ct100$Body$TextBox_Postal' as opposed to 'TextBox_Postal').

I was able to fix this by setting ClientIDMode=CliendIDMode.Static, this works great as it doesn't try and include the namingcontainer in the id. I am confident that there will never be two of the same control on the page so this would work.

The problem is, when the form is posted back the controls are processed by names. The names are still of the 'ct1200$Body$..' format, so the processform function is unable to find any controls. Is there a way to get ASP to set the names in "Static" mode as well?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think there's a way to set the name of the controls appropriately but you could alternatively change their names very easily using JQuery, if that's an option for you.

Example here

Some explanation:

  • Assuming you have markup like this:

    <div>
        <asp:textbox runat="server"  id="staticid1" />
        <asp:textbox runat="server"  id="staticid2" />
        <asp:textbox runat="server"  id="staticid3" />
    </div>
    

You could automatically change all of those control names to have the same names as their ids doing something like this on window.load:

 $.each($('div').children(), function() {
      $(this).attr("name",$(this).attr("id"));
   });

All you need in order to make that work is include JQuery; you could use Google's CDN: http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...