PC Review


Reply
Thread Tools Rating: Thread Rating: 4 votes, 1.00 average.

asp.net HiddenField ClientId

 
 
imbirek8
Guest
Posts: n/a
 
      22nd Oct 2008
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


 
Reply With Quote
 
 
 
 
Alberto Poblacion
Guest
Posts: n/a
 
      22nd Oct 2008
"imbirek8" <(E-Mail Removed)> wrote in message
news:gdnu8q$sp5$(E-Mail Removed)...
> 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("_", "$")


 
Reply With Quote
 
imbirek8
Guest
Posts: n/a
 
      22nd Oct 2008
"Alberto Poblacion" <earthling-(E-Mail Removed)> wrote
in message news:en4dW%(E-Mail Removed)...
[...]
>> How can I find ClientId of the HiddenField ?

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

[...]

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

 
Reply With Quote
 
Alberto Poblacion
Guest
Posts: n/a
 
      23rd Oct 2008

"imbirek8" <(E-Mail Removed)> wrote in message
news:gdo2et$dmu$(E-Mail Removed)...
> "Alberto Poblacion" <earthling-(E-Mail Removed)> wrote
> in message news:en4dW%(E-Mail Removed)...
> [...]
>>> How can I find ClientId of the HiddenField ?

>> 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 <%= ... %>.

 
Reply With Quote
 
imbirek8
Guest
Posts: n/a
 
      27th Oct 2008
"Alberto Poblacion" <earthling-(E-Mail Removed)> wrote
in message news:%(E-Mail Removed)...
[...]

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

 
Reply With Quote
 
Alberto Poblacion
Guest
Posts: n/a
 
      27th Oct 2008
"imbirek8" <(E-Mail Removed)> wrote in message
news:ge35p4$l91$(E-Mail Removed)...
> "Alberto Poblacion" <earthling-(E-Mail Removed)> wrote
> in message news:%(E-Mail Removed)...
> [...]
>
> 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.

 
Reply With Quote
 
imbirek8
Guest
Posts: n/a
 
      27th Oct 2008
"Alberto Poblacion" <earthling-(E-Mail Removed)> wrote
in message news:(E-Mail Removed)...
[...]
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

 
Reply With Quote
 
Alberto Poblacion
Guest
Posts: n/a
 
      28th Oct 2008
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" <(E-Mail Removed)> wrote in message
news:ge5c0o$bqb$(E-Mail Removed)...
> "Alberto Poblacion" <earthling-(E-Mail Removed)> wrote
> in message news:(E-Mail Removed)...
> [...]
> 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>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiddenfield in formview win Microsoft ASP .NET 1 21st May 2008 01:18 PM
HiddenField hharry Microsoft ASP .NET 5 17th Jul 2007 05:32 PM
Setting HiddenField .Value Efi Merdler Microsoft ASP .NET 8 4th May 2007 01:56 PM
Help getting at value of HiddenField in a UserControl (.Net 1.1) RSH Microsoft ASP .NET 1 10th Apr 2007 10:55 PM
Need help getting the value in a HiddenField! Jeff Microsoft ASP .NET 3 18th Oct 2006 05:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:32 AM.