asp.net HiddenField ClientId

I

imbirek8

Hi!

I would like to write something in Javascript to HiddenField:
<asp:HiddenField ID="hiddenMy" runat="server" Value="" />
And in the code behind read this value.

But this HiddenField is in user control. I use this control in many pages
and in every page this field has different value..
I don't know why, but it's error when I do:
var hidden = document.GetElementById('<%= this.gvClient.ClientID %>');

How can I find ClientId of the HiddenField ?

Thanks for help
 
A

Alberto Poblacion

imbirek8 said:
I would like to write something in Javascript to HiddenField:
<asp:HiddenField ID="hiddenMy" runat="server" Value="" />
And in the code behind read this value.

But this HiddenField is in user control. I use this control in many pages
and in every page this field has different value..
I don't know why, but it's error when I do:
var hidden = document.GetElementById('<%= this.gvClient.ClientID %>');

How can I find ClientId of the HiddenField ?

You can use hiddenMy.ClientID.Replace("_", "$")
 
A

Alberto Poblacion

imbirek8 said:
in message [...]
You can use hiddenMy.ClientID.Replace("_", "$")
[...]

I don't understant what exacly it would do..

The ClientID property returns an ID that separates its various parts
with "_", such as "ctl00_ctrlA1_rptB1_ctl00_ibB1", but the Name that is
actually rendered into the html uses "$" to separate the same parts, such as
"ctl00$ctrlA1$rptB1$ctl01$ibB1". At first I thought that you were doing a
finding the element by Name, so you would have needed the previous
replacement to find the control on the client side.

Upon rereading your message, I see that this is not the case. You mention
that you are doing document.GetElementById('<%= this.gvClient.ClientID
%>'); and that you get an error, but you don't mention what the error is.
You also don't mention wether you are writing this code in the markup of the
Page or the UserControl. If you are writing it on the Page, the "this" is
interpreted on the page class, so "this.gvClient" needs to refer to a public
field or property on the page, it won't work if gvClient is inside a
usercontrol that is inside the page, since this is not automatically visible
to the page class. You may wish to add a public property to expose the
information that you then render in the <%= ... %>.
 
I

imbirek8

in message [...]

And what about this idea. I would like to add for example label and set
style="display:none;". and in JavaScript change the the text and in code
behind read new value. I have:
<asp:Label ID="labelSelectedRow" runat="server" Text="aaa" />
and i JavaScript I do:
(document.getElementById('<%= this.labelSelectedRow.ClientID %>')).Text=
"bbb";

but nothing has changed... why ?

Thanks for help
 
A

Alberto Poblacion

imbirek8 said:
in message [...]

And what about this idea. I would like to add for example label and set
style="display:none;". and in JavaScript change the the text and in code
behind read new value. I have:
<asp:Label ID="labelSelectedRow" runat="server" Text="aaa" />
and i JavaScript I do:
(document.getElementById('<%= this.labelSelectedRow.ClientID %>')).Text=
"bbb";

but nothing has changed... why ?

It *should* change... on the display in the browser, if it weren't
hidden. But you can't read it back in the code behind because the label does
not get posted back from the browser into the server.
You can do it with a HiddenField, which is rendered as <input
type="hidden"> (or you can use a TextBox with style="display:none;"). If you
change its value in javscript, it will be available to the server after the
form gets submitted.
 
I

imbirek8

in message [...]
I do not understand anything. I have the simpliest code I can have and it
doesn't work. I can't see even alert.. in firebug I don't have any error.
Why it doesn't work ?

<body>
<script type="text/javascript">
function click()
{
alert('here1');
(document.getElementById('<%=this.txtSelectedRow.ClientID%>')).Text
= "bbb";
}
</script>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSelectedRow" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="click()" />
</div>
</form>
</body>

Thanks for help
 
A

Alberto Poblacion

Two changes:

- First, change the name of the function from "click" into something else,
such as "b_click" (click is already in use somewhere else in the hierarchy
of dhtml objects).

- Second, you should use ".value" instead of ".Text". We are using the
client-side object model, not the server-side. In other words, asp:TextBox
has a .Text property, but it gets rendered into the browser as an <input
type=text.../>, which has a .value but not a .Text.

I have copied a corrected version at the bottom.


imbirek8 said:
in message [...]
I do not understand anything. I have the simpliest code I can have and it
doesn't work. I can't see even alert.. in firebug I don't have any error.
Why it doesn't work ?

<body>
<script type="text/javascript">
function click()
{
alert('here1');
(document.getElementById('<%=this.txtSelectedRow.ClientID%>')).Text
= "bbb";
}
</script>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSelectedRow" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="click()" />
</div>
</form>
</body>

Thanks for help


<body>
<script type="text/javascript">
function b_click()
{
alert('here1');
(document.getElementById('<%=this.txtSelectedRow.ClientID%>')).value
= "bbb";
}
</script>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtSelectedRow" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="b_click();" />
</div>
</form>
</body>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top