Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to assign a csharp variable to the element of asp page using javascript. It seems the assignement is not working in my code.

Here it is:

document.getElementById('lbAccessories').innerHTML = '<%#SelectLabel%>';

In my asp code i use:

<asp:LinkButton ID="lbAccessories" runat="server" />'

I can't assign the value directly to the linkbutton using Text='<%#SelectLabel%>' because i want to make it more intelligent.

Does anyone knows how to do with that?

Thanks

Edit:

Here is my code, I've tried to use <%=lbAccessories.clientId%>, but it generates an error : lbAccessories does not exist in the context.

<script type="text/javascript">
    function function(Ref) {
        if ('<%=TextBoxClientID%>' == 'txtLink') 
        {
            document.getElementById('lbAccessories').innerHTML = '<%#SelectLabel%>';
        }
        else if ('<%=TextBoxClientID%>' == 'btnSearch') {
            document.getElementById('lbAccessories').innerHTML = '<%#ViewDetail%>';
        }
    }

 </script>

Edit:

<asp:GridView ID="gv1" AutoGenerateColumns="false" runat="server" 
    AllowPaging="true" OnPageIndexChanging="pic">
    <Columns>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:LinkButton ID="lbAccessories" runat="server" OnClientClick='<%#string.Format("passAccessory(\"{0}\");", Eval("Ref"))%>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Edit:

Thanks so much for everyone who had posted the suggestions.

I have changed the javascript code to the following:

<script type="text/javascript">
    function passAccessory(accessoryRef) {

        if ('<%=TextBoxClientID%>' == 'txtLink') {
            document.getElementById('<%= gvAccessories.FindControl("lbAccessories").ClientID %>').innerHTML = '<%#SelectLabel%>';
        }
        else if ('<%=TextBoxClientID%>' == 'btnSearch') {
            document.getElementById('<%= gvAccessories.FindControl("lbAccessories").ClientID %>').innerHTML = '<%#ViewDetail%>';
        }
    }
</script>

in *.aspx.cs file, i have added: protected LinkButton lbAccessories { get; set; }

It throws an exception: "Object reference not set to an instance of an object"

Anyone have ideas? Thanks so much

Edit:

Finally, we did it with csharp code. It's more flexable and manageable. My colleague helped me. Thank you all for your help!!!

share|improve this question
    
The javascript is executing before the link button has been added to the DOM –  cspolton Dec 1 '10 at 13:01
    
You appear to be wanting to set the text for the LinkButton when you click on it, but the button will POST back to the server when clicked. Is this what you want? –  cspolton Dec 1 '10 at 14:50
    
This is a user control. Different pages include the user control. But the view is a little different when different pages are loaded. So i want to use javascript to judge different conditions and to show different content in the LinkButton which is located in a gridview. –  charles sun Dec 1 '10 at 15:01

4 Answers 4

up vote 2 down vote accepted

Check the output HTML whether an anchor's ID is actually what you are looking for. In any case you should rather use <%= lbAccessories.ClientID %> than refering to server ID from client script.

If you for some reason don't want to (or cannot) use <%= lbAccessories.ClientID %> then check following article about jQuery plugin which allows you to get DOM element by server ID:

http://radek-stromsky.blogspot.com/2010/11/aspnet-how-to-get-client-id-with-jquery.html

EDIT:

I made several changes in your code:

<script type="text/javascript">
    function passAccessory(Ref) {
        if ('<%= TextBoxClientID %>' == 'txtLink') 
        {
            document.getElementById('<%= GridView1.FindControl("lbAccessories").ClientID %>').innerHTML = '<%# SelectLabel %>';
        }
        else if ('<%= TextBoxClientID %>' == 'btnSearch') {
            document.getElementById('<%= GridView1.FindControl("lbAccessories").ClientID %>').innerHTML = '<%#ViewDetail%>';
        }
    }
</script>

 <asp:LinkButton ID="lbAccessories" runat="server" OnClientClick='<%#string.Format("passAccessory(\"{0}\");", Eval("Ref"))%>'></asp:LinkButton>

EDIT: EDIT:

I fixed the code above. Now there is GridView1.FindControl("lbAccessories")

share|improve this answer
    
Why it always generate the error: CS0103: The name 'lbAccessories' does not exist in the current context? I think if i use lbAccessories like your example, i have declare it in the code behind. –  charles sun Dec 1 '10 at 13:10
    
Yes that's what you need to do. –  cspolton Dec 1 '10 at 13:14
    
If you define control with explicit ID, Visual Studio should generate member with such name in *.aspx.designer.cs file. Search that file and see if "lbAccessories" is defined there. –  Radek Stromský Dec 1 '10 at 13:17
    
This problem can be cause by having this block of code inside one of the FormView's Templates. In such situation, you cannot simply access this control via ID. –  Radek Stromský Dec 1 '10 at 13:21
    
I just found that lbAccessories doesn't exist in the file *.aspx.designer.cs, and i have put the LinkButton in a gridview's template. I have pasted the code in the post. Do you know how to correct it? –  charles sun Dec 1 '10 at 13:28

Assuming SelectLabel is a public or protected string member or property of your ASP.NET Page, you should be able to render the string using:

document.getElementById('<%= lbAccessories.ClientID %>').innerHTML = '<%= SelectLabel%>';

Is there a reason why the "intelligence" you require can't be server-side? If you are setting SelectLabel using C#, you should be able to dynamically set lbAccessories.Text = SelectLabel; too.

share|improve this answer
    
It assigns the value to the element.innerHTML, but it does't show in the linkbutton. Do you know why? –  charles sun Dec 1 '10 at 12:25
    
See my update, if you must use javascript, you need to output the ClientId of the LinkButton in the javascript. –  cspolton Dec 1 '10 at 12:27
    
I want to make a if judgement using javascript, the element's value changes depending on the conditions. –  charles sun Dec 1 '10 at 12:29
    
Can you show us more code? –  cspolton Dec 1 '10 at 12:42
    
I have changed the code, can you have a look at it? –  charles sun Dec 1 '10 at 12:55

add a hiddenfield with id="hid1" and set its value to the selectlabelvalue, then:

document.getElementById('lbAccessories').innerHTML

= document.getElementById('hid1').innerHTML;

JQuery:

$('#lbAccessories).html($('#hid1').html());
share|improve this answer
    
i have tried to use hiddenfield, but it doesn't work; –  charles sun Dec 1 '10 at 12:56
    
if you have a master page or working inside a user control, note that the id will change, you should use UniqueID in client script –  Ali Tarhini Dec 1 '10 at 12:57
    
So what should i use? <%= lbAccessories.ClientID %> will be ok? –  charles sun Dec 1 '10 at 13:07
    
yes............ –  Ali Tarhini Dec 1 '10 at 13:09

lbAccessories is a Server side control. When the server emits HTML for it, it assigns a unique ClientId to the control. In the JavaScript you have to access it via it's ClientId like this:

document.getElementById('<%=lbAccessories.ClientId%>').innerHTML = '<%#SelectLabel%>';

Hope this helps.

share|improve this answer
    
I tried but it generate an error: CS0103: The name 'lbAccessories' does not exist in the current context –  charles sun Dec 1 '10 at 12:35
    
The ClientId should be ClientID instead. Where in the aspx page is your JavaScript located? –  jvanrhyn Dec 1 '10 at 12:45
    
It's located in the same page; and it generates the same error –  charles sun Dec 1 '10 at 12:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.