Can I force asp to set name the same as id
Asked Answered
N

2

9

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?

Nonnah answered 26/10, 2011 at 0:27 Comment(3)
How are you processing the controls? Can you post some server-side code? Besides, if you know the id, why not just use them?Demonstrative
The controls are dynamically added to the form on pageload, processing the form just looks at request.Form which contains a namevaluecollection of all the search control names and their values.Nonnah
post a code snippet of how you are finding the controls.Abelmosk
T
2

Short answer is no, you will have to override the rendering of the name attribute, example below from this question: ASP.NET: how to remove 'name' attribute from server controls?

public class NoNamesTextBox : TextBox
{
    private class NoNamesHtmlTextWriter : HtmlTextWriter
    {
        public NoNamesHtmlTextWriter(TextWriter writer) : base(writer) {}

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (name.Equals("name", StringComparison.OrdinalIgnoreCase)) return;

            base.WriteAttribute(name, value, fEncode);
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        var noNamesWriter = new NoNamesHtmlTextWriter(writer);

        base.Render(noNamesWriter);
    }
}
Threesquare answered 26/10, 2011 at 1:9 Comment(0)
D
13

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

Demonstrative answered 26/10, 2011 at 1:17 Comment(2)
Textbox values are not readable in the codebehind after doing this. I get empty strings. I assume .net uses the name for the postback?Prosperity
@DanLehmann that's correct. But it's not .NET doing it, that's how HTML works. If you use this technique you will have to read the values using Rquest.Params["new_text_field_name"] as opposed to reading the Text Propery.Demonstrative
T
2

Short answer is no, you will have to override the rendering of the name attribute, example below from this question: ASP.NET: how to remove 'name' attribute from server controls?

public class NoNamesTextBox : TextBox
{
    private class NoNamesHtmlTextWriter : HtmlTextWriter
    {
        public NoNamesHtmlTextWriter(TextWriter writer) : base(writer) {}

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (name.Equals("name", StringComparison.OrdinalIgnoreCase)) return;

            base.WriteAttribute(name, value, fEncode);
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        var noNamesWriter = new NoNamesHtmlTextWriter(writer);

        base.Render(noNamesWriter);
    }
}
Threesquare answered 26/10, 2011 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.