Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:16:09+00:00 2026-05-18T10:16:09+00:00

I want to assign a csharp variable to the element of asp page using

  • 0

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!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:16:10+00:00Added an answer on May 18, 2026 at 10:16 am

    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")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
I want to assign the output of a command I run using os.system to
We are using a Delphi TServerSocket and want to assign a port automatically. How
in the following code, I want to assign a different label to the text
I want to assign a resource I already have a second name, similar to
I want to assign a class attribute via a string object - but how?
In my full text search query, I want to assign particular columns a higher
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I have created the sharepoint site . I want to assign the name as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.