Click here to Skip to main content
13,146,668 members (66,256 online)
Rate this:
Please Sign up or sign in to vote.
See more:
when i bind fontname to linkbutton on run time then following error shows

Unable to cast object of type 'System.String' to type 'System.String[]'.

my code

<asp:LinkButton ID="lnkMenu" runat="server" Font-Names='<%#Eval("FontName")%>' Font-Size='<%#Convert.ToInt32(Eval("FontSize")) %>' Text='<%#Eval("Name")%>' CommandArgument='<%#Eval("ID") %>'
                                                                    ToolTip="Select" OnClick="Repeater_Click">

help me to solve this thank u
Posted 28-Feb-12 23:30pm
RockingDev 29-Feb-12 5:49am
user problem might be with "FontName".what is data type of FontName?
Patil_Nilesh 29-Feb-12 6:13am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Take a hidden field and bind that with <%#Eval("FontName")%>
then in rowdatabind event
         LinkButton lnk=(LinkButton)....FindControl("lnkMenu");
         HtmlInputHidden fn=(HtmlInputHidden)....FindControl("hdnFontFamily");
         lnk.Style.Add("Font-Names", fn.Value);
Patil_Nilesh 29-Feb-12 6:13am
there is not only single font name
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hi all,
I solved it using ItemDataBound event
protected void reptrDisplay_ItemDataBound(object sender, RepeaterItemEventArgs e)
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Header)
            LinkButton lnkMenu = (LinkButton)e.Item.FindControl("lnkMenu");
            lnkMenu.Enabled = Modify;
            string[] detail = lnkMenu.CommandArgument.ToString().Split(';');
            lnkMenu.Font.Name = detail[1];
            lnkMenu.Font.Size = int.Parse(detail[2]);

Thnks for ur support
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

<asp:linkbutton id="lnkMenu" runat="server" fontnames="<%#Eval("FontName")%>" font-size="<%#Convert.ToInt32(Eval("FontSize"))%>" text="<%#Eval("Name")%>" commandargument="<%#Eval("ID") %>" tooltip="Select" onclick="Repeater_Click" xmlns:asp="#unknown">                                                               </asp:linkbutton>

the problem is in ur code,u are assigning collection to strings ie.font1,font2,..etc to Font Names property,so it gives error like
"Unable to cast object of type 'System.String' to type 'System.String[]'"
<asp:linkbutton id="runView1PerPage" font-names="Verdana, Arial" runat="server" xmlns:asp="#unknown"></asp:linkbutton>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 7 Mar 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100