Showing posts with label inside. Show all posts
Showing posts with label inside. Show all posts

Thursday, March 29, 2012

Saturday, March 24, 2012

Passing <%= myControl.ClientID %> into Javascript function

I can successfully use <%=myControl.ClientID%> inside of a Javascript
function, but does anyone know if it is possible to pass this value to
my Javascript function? The reason I want to do this is so I can
abstract my Javascript function out of my ASP.NET user control.
Ideally, I would like to have a function like this.
function testMe(control){
document.getElementByID(control).style = ...
// do more useful things here
// I am trying not to have to use this...
// document.getElementByID('<%=btnTest.ClientID%>').style = ...
}
I would like to be able to call it like this...
<asp:LinkButton id="btnTest" runat="server" OnClientClick="return
testMe(<%=this.ClientID%> )" text="test" />
But this does not work. If I have to keep the actual control name in
the javascript file, then my decoupling is lost. Is this approach
possible?
Thanks.Alex,
why not just pass the reference to the control itself, rather than its ID:
<asp:LinkButton id="btnTest" runat="server" OnClientClick="return
testMe(this);" text="test" />
function testMe(control){
control.style = ...
}
"Alex" wrote:

> I can successfully use <%=myControl.ClientID%> inside of a Javascript
> function, but does anyone know if it is possible to pass this value to
> my Javascript function? The reason I want to do this is so I can
> abstract my Javascript function out of my ASP.NET user control.
> Ideally, I would like to have a function like this.
> function testMe(control){
> document.getElementByID(control).style = ...
> // do more useful things here
> // I am trying not to have to use this...
> // document.getElementByID('<%=btnTest.ClientID%>').style = ...
> }
> I would like to be able to call it like this...
> <asp:LinkButton id="btnTest" runat="server" OnClientClick="return
> testMe(<%=this.ClientID%> )" text="test" />
> But this does not work. If I have to keep the actual control name in
> the javascript file, then my decoupling is lost. Is this approach
> possible?
> Thanks.
>
On Jul 26, 11:56 am, Sergey Poberezovskiy
<SergeyPoberezovs...@.discussions.microsoft.com> wrote:
> Alex,
> why not just pass the reference to the control itself, rather than its ID:
> <asp:LinkButton id="btnTest" runat="server" OnClientClick="return
> testMe(this);" text="test" />
> function testMe(control){
> control.style = ...
> }
> "Alex" wrote:
>
>
>
>
>
>
hi...
Sergey Poberezovskiy's suggested method is right...
in this way you dont have to think about client id...
Thanks
Md. Masudur Rahman (Munna)
Kaz Software Ltd.
www.kaz.com.bd
http://munnacs.110mb.com
Passing in "this" did the trick. I tried that before, but for some
reason I thought I was getting the control's ID at design time, not
the actual ASP.NET client ID at run time. This works. I appreciate the
help.

Friday, March 16, 2012

Passing a value to a JavaScript function from the database

I'm trying to pass a value to initialize a JavaScript function from inside a
Form.
I haven't had any luck.
Can this be done?
I need to get a value from a database and send it to the JavaScript
function.
Thanks,
Scott Natwicklots of ways:
in the aspx page:
<script>
var myValue = ' <%= getValue() %>';
</script>
or in the code behind
RegisterHiddenField("myValue",getValue();
and have the javascript reference the hidden field. you can register a
script block. if you need it at runtime, the javascript can post to a hidden
iframe/frame.
-- bruce (sqlwork.com)
"Scott Natwick" <nospam@.yahoo.com> wrote in message
news:ncWdnVyYIZbz79LcRVn-hw@.comcast.com...
> I'm trying to pass a value to initialize a JavaScript function from inside
a
> Form.
> I haven't had any luck.
> Can this be done?
> I need to get a value from a database and send it to the JavaScript
> function.
> Thanks,
> Scott Natwick
>
Hi Scott,
Have you had a chance to check bruce's suggestions or have you got any
further ideas on this issue? If you need any further assistance, please
feel free to post here.Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Hi Steven,
Thanks for following up. I believe I have it working now, using the prior
suggestion.
On the C# side, my code looks roughly like this:
String scriptString = "<script language=JavaScript> ";
scriptString += "var ... ";
scriptString += "function FunctionName() {... document.Form1.strValue1.value
...}";
scriptString += "</script> ";
RegisterHiddenField("strValue1", "...");
RegisterHiddenField("strValue2", "...");
RegisterStartupScript("startup", scriptString);
It tests Ok, but I haven't put it into production yet.
Regards,
Scott
"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> wrote in message
news:qPlTs2UoEHA.2640@.cpmsftngxa06.phx.gbl...
> Hi Scott,
> Have you had a chance to check bruce's suggestions or have you got any
> further ideas on this issue? If you need any further assistance, please
> feel free to post here.Thanks.
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
Thanks for your followup Scott!
Anyway, if you have any problems in the future, please feel free to post
here. Thanks.
Have a good day!
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)