如何在LinkButton1_Command中更改链接按钮颜色 [英] how to change link button color in LinkButton1_Command
本文介绍了如何在LinkButton1_Command中更改链接按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
如何更改链接按钮颜色
$ b链接命令事件中的$ b
Hello,
how to change the link button color
in the link command event
protected void LinkButton1_Command(object source, CommandEventArgs e)
{
if (e.CommandName.Equals("Paging"))
{
var lbtn = (LinkButton)FindControl("LinkButton1" + e.CommandArgument);
lbtn.ForeColor = System.Drawing.Color.Red;
lbtn.ForeColor = System.Drawing.Color.Blue;
}
}
i正在做但是null异常thow
如何找到链接控制
Regrads
Sadhana.R.Belge
i am doing but null exception thow
how can i find link Control
Regrads
Sadhana.R.Belge
推荐答案
只需添加到CommandArgument参数并在Click处理程序上读取它:
在ASPX中
Just add to the CommandArgument parameter and read it out on the Click handler:
In ASPX
<asp:LinkButton ID="ENameLinkBtn" runat="server"
style="font-weight: 700; font-size: 8pt;" CommandArgument="YourValueHere"
OnClick="LinkButton1_Command" >
在代码背后
In Code Behind
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument; // e.CommandArgument
if(btn != null)
{
// do what you need here
btn.ForeColor = System.Drawing.Color.Red;
}
}
这是因为lbtn
,由<$返回c $ c> FindControl 返回null。您需要找到正确的
id
作为控件。在尝试取消引用变量之前,请务必检查类似于null的变量。
另请参阅我最近的答案:对象引用未设置为对象实例 [ ^ ]。< br $>
-SA
This happens becauselbtn
, returned byFindControl
returns null. You need to find out correct
id
for the control. Always check a variable like that for null before trying to dereference a variable.
See also my recent answer: Object Reference not set to an instance of object[^].
—SA
这篇关于如何在LinkButton1_Command中更改链接按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文