简体   繁体   English

无法从后面的代码访问CSS类的链接按钮

[英]Cannot access the CSS class for a link button from Code behind

Can we include a CSS class from code behind for a link button? 我们可以在代码中包含CSS类吗?

I have been trying this for a while, but I was not able to get it done. 我已经尝试了一段时间,但无法完成。 It does not show a hyper link nor does CSS work. 它不显示超级链接,也不起作用。 Please refer to my code and see where am I doing wrong. 请参考我的代码,看看我在哪里做错了。

string link1 = "google.com"
lblclick.Text = "<p>See what our page looks like by clicking " 
+ "<asp:LinkButton CssClass="+"linkclass" + ">" 
+ link1 + "</asp:LinkButton>

if you want to add a linkbutton to a panel from codebehind you will have to create it from code. 如果要从代码后面向面板添加链接按钮,则必须从代码创建它。

LinkButton lb = new LinkButtton();
lb.cssclass="linkclass";
lb.text = "foo";
panel1.Controls.Add(lb);

You can't just add ASP.NET markup as a textproperty in your code, ASP doesn't work like that. 您不能只在代码中添加ASP.NET标记作为文本属性,ASP不能那样工作。 Create a Linkbutton btn = new LinkButton() , and add it: lblclick.Controls.Add(btn) . 创建一个Linkbutton btn = new LinkButton() ,并将其添加: lblclick.Controls.Add(btn) Then you can edit the properties of btn as you please. 然后,您可以根据需要编辑btn的属性。

Create the LinkButton in code like this: 用如下代码创建LinkButton

LinkButton linkButton = new LinkButton();
linkButton.CssClass = "linkclass";
linkButton.Text = "google.com";

If lblclick is a Label , then you can't add a asp tag like LinkButton just like that. 如果lblclick是Label ,则不能像这样添加诸如LinkBut​​ton之类的asp标签。

If you can (or if you move the LinkButton to your markup) you need to add the runat="server" to be able to set properties like CssClass on it. 如果可以(或者如果将LinkBut​​ton移至标记),则需要添加runat="server"以便能够在其上设置诸如CssClass属性。 If you just want a plain link you can add an anchor tag instead. 如果您只想使用普通链接,则可以添加锚标记。

lblclick.Text = "<p>See what our page looks like by clicking 
<a href=\"" + link + "\" class=\"linkclass\">" + link1 + "</a></p>"

Actually, if you want a link to another page you shouldn't use LinkButton at all, but rather the HyperLink class . 实际上,如果要链接到另一个页面,则根本不应该使用LinkBut​​ton,而应该使用HyperLink类 You can use the NavigateUrl property to set the url to open when clicking the link. 您可以使用NavigateUrl属性来设置单击链接时打开的URL。

If you want to add it to your markup you do it like this 如果您想将其添加到标记中,可以这样

<asp:HyperLink
     NavigateUrl="http://www.example.com"
     CssClass="linkclass"
     Text="link1"
     runat="server" />

and if you want to do it dynamically in code you add it by creating it and adding it to your Controls collection. 如果要在代码中动态地进行操作,则可以通过创建它并将其添加到Controls集合中来添加它。

HyperLink link = new HyperLink();
link.NavigateUrl = "http://www.example.com";
link.Text = "link1";
link.CssClass = "linkclass";
Controls.Add(link);

Just remember that when you add controls dynamically you should add it in your Page_Load event every time you load your page. 只需记住,当动态添加控件时,每次加载页面时都应将其添加到Page_Load事件中。 If you don't want it to display set its Visible property to false and change it to true based on an event or something. 如果您不希望它显示,则将其Visible属性设置为false ,然后根据事件或其他内容将其更改为true Perhaps not as important when using a HyperLink, but good practice nonetheless. 使用HyperLink时可能不那么重要,但还是要保持良好的操作习惯。 An example of when dynamic controls bite you if you don't is this question asked recently . 最近有人问这个问题的一个例子:动态控件是否咬你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM