开发者

How to assign C# variable to asp element using javascript

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").Cli开发者_如何学GoentID %>').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!!!


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


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.


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());


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜