Name Overwritten by ID when runat=server

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

I have an html input (type=text) field on an html page, with runat=server.
The field is inside a form element. It looks like this:

<FORM id="Form1" method="post" runat="server" action="mypost.aspx">
<INPUT id="Text1" type="text" Name="myname" runat="server">
</FORM>

When the form posts to another page, I want to access the input field on the
target page by the name 'myname'.

I have found that name is getting overwritten by the id, that is, both id
and name are reading as Text1. However, if I remove the runat=server on the
control, it is not overwritten.

Is this normal behaviour?

How can I prevent the id replacing the name, but still keep the
runat=server?

TIA

Charles
 
Hi

I could reproduce this behavior can you post the HTML output of your form tag

If your textbox is inside a web control then it will be renamed NameofControl_NameofTextbox

Yama
 
Hi Yama

Here is the complete content of my webform: WebForm1.aspx


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<META content="MSHTML 6.00.2800.1400" name="GENERATOR">
<META http-equiv="Content-Type" content="text/html; charset=windows-1252">
</HEAD>
<BODY ms_positioning="GridLayout">
<FORM id="Form1" method="post" runat="server" action=posted.aspx>
<input id="Text1" type="text" Name="ctl001" runat="server">
</FORM>
</BODY>
</HTML>

When I run this and select View Source from the context menu in the browser,
I get the following in Notepad:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<META content="MSHTML 6.00.2800.1400" name="GENERATOR">
<META http-equiv="Content-Type" content="text/html; charset=windows-1252">
</HEAD>
<BODY ms_positioning="GridLayout">
<form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE"
value="dDwxNzU2ODcxNTcyOzs+IpeJMZ57UrjafLGeiaE1r/3zEKI=" />

<input name="Text1" id="Text1" type="text" />
</form>
</BODY>
</HTML>

As you can see, name and id are both the same. When I post this form and try
to access the control name ctl001, it does not exist. Curiously, the action
has been removed and replaced with WebForm1.aspx as well.

Any thoughts?

Charles
 
Very interesting behavior as this did not happen with my browser (win 2K
server running IE 6.0 .NET 1.1)

You could try and assign the value for name dynamically:
Text1.Attributes.Add("Name", "ctl001")

Yama
 
I am also using IE6, Framework V1.1, and Windows XP Pro. I would be
surprised if it is the o/s.

This same symptom has also occurred on the only other machine that I have
tried it on; similar set-up.

I am not quite clear where you mean I could set the value of name
dynamically. If you mean in script on the page then it would not be
practical as the form is eventually going to be auto-generated by a Winforms
app.

Can you think of anything in settings VS.NET that could cause this behaviour
by default?

Charles
 
Hi Charles,

Here is what I did:

<body ms_positioning="GridLayout">
<form id="Form1" runat="server">
<asp:textbox id="TextBox1" style="Z-INDEX: 101; LEFT: 104px; POSITION:
absolute; TOP: 152px"
runat="server" name="TextBox2"></asp:textbox></form>
</body>

After running the web:

<input name="TextBox1" type="text" id="TextBox1" name="TextBox2"
style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 152px" /></form>
</body>

So I do not understand why this is happening to you... The name attribute is
not recognize by browser!

Oh here you go I got it... Use
<asp:textbox id="Text1" runat="server" name="ctl001"></asp:textbox>
Instead of:
<input id="Text1" type="text" Name="ctl001" runat="server">


Cheers,

Yama
 
Hi Yama

You're suggesting that I use <asp:Textbox /> as opposed to <INPUT
type="text">. Unfortunately, I am compelled to use the latter because the
control must render in a WebBrowser control (which asp controls do not).

After some searching though, it seems that it is expected that the id
attribute will be copied to the name attribute for server controls, as the
ClientID. I think that there is nothing I can do about this. My solution has
been to put my specific name (ctl001, for example) into the id attribute, so
that it becomes the name (ClientID) when the form is loaded. This way, I can
access the control by my preferred name when the page is posted.

Not my ideal solution, but it seems that this is the way it is intended to
work. C'est la vie!

Thanks for your help.

Charles
 
Back
Top