禁用 DataGrid TemplateColumn 中的 LinkButton
我正在使用 Visual Studio 2008 和 .NET Framework 3.5。我有一个 DataGrid,在 TemplateColumn 内有一个 LinkButton。我试图弄清楚如何禁用单击 LinkButton 后单击它的功能。我的 DataGrid 有 6 列,其中 LinkButton 列显示年份,其他列显示这些年份的年末数据。单击某一年时,DataGrid 会按月显示该年数据的详细信息。当 DataGrid 显示按月细分时,我仍然需要年份列可见,但无法单击。我还有一个按钮和一个图表,默认情况下“可见性”设置为“假”,但选择一年后,“可见性”设置为“真”,该按钮可以关闭按月细分并返回到年份结束崩溃。除了禁用 LinkButton 之外,一切都正常。
这是我的 DataGrid 的 TemplateColumn 的代码:
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
我尝试了以下操作:
使用后面的代码的尝试 1:
protected void Page_Load(object sender, EventArgs e)
{
LinkButton lb = ((LinkButton) FindControl("lbYear"));
lb.Attributes.Add("onClick", "return false;");
}
使用 Javascript 的尝试 2:
function disableLinkButton() {
var lb = document.getElementById("lbYear");
if (lb.disabled != true) { lb.disabled = true; return true; }
}
else { return false; }
}
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" OnClientClick="disableLinkButton()" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
-- 第三次尝试很接近,它使 LinkButtons 变灰,但没有禁用单击它们的功能 尝试 3 使用“Enabled”属性:
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" Enabled='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "year"))==1?Convert.ToBoolean("True"):Convert.ToBoolean("False") %>' Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
我的其他一些想法包括使用“OnClick”事件或“CommandArgument”。我尝试使用“OnClick”,并在后面的代码中简单地使用:
LinkButton lb = ((LinkButton) FindControl("lbYear");
lb.Enabled = false;
任何帮助、想法、想法、示例等将不胜感激。提前谢谢大家!
调整后的代码:
<ItemTemplate>
<asp:LinkButton ID="lbYear" runat="server" OnClick="testClick" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
<a id="MyContrl_lbYear" href="javascript:__doPostBack('MyContrl$lbYear','')" onclick="this.href='#';this.disabled=true;__doPostBack('MyContrl$lbYear','');"></a>
</ItemTemplate>
protected void showChart(object sender, EventArgs e)
{
LinkButton lbYear = ((LinkButton)FindControl("lbYear"));
lbYear.Attributes.Add("onclick", "this.href='#';this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(lbYear, "").ToString());
}
I am using Visual Studio 2008 with .NET Framework 3.5. I have a DataGrid with a LinkButton inside a TemplateColumn. I am trying to figure out how to disable the ability to click the LinkButton once it has been clicked. My DataGrid has 6 columns with the LinkButton column displaying years and the others displaying year end data for those years. When a year is clicked the DataGrid displays a breakdown of that year's data on a month by month basis. When the DataGrid is displaying the month by month breakdown I still need the year column to be visible but without the ability to click. I also have a button and a chart that, by default Visibility is set to false, but after a year is selected the Visibility is set to true with the button giving the ability to close out of the month by month breakdown and return to the year end breakdown. I have everything working except the disabling of the LinkButton.
Here is the code for my DataGrid's TemplateColumn:
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
I have tried the following:
Attempt 1 using code behind:
protected void Page_Load(object sender, EventArgs e)
{
LinkButton lb = ((LinkButton) FindControl("lbYear"));
lb.Attributes.Add("onClick", "return false;");
}
Attempt 2 using Javascript:
function disableLinkButton() {
var lb = document.getElementById("lbYear");
if (lb.disabled != true) { lb.disabled = true; return true; }
}
else { return false; }
}
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" OnClientClick="disableLinkButton()" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
-- The 3rd attempt was close which did gray out the LinkButtons but did not disable the ability to click them
Attempt 3 using the 'Enabled' property:
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" Enabled='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "year"))==1?Convert.ToBoolean("True"):Convert.ToBoolean("False") %>' Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
Some other thoughts I have include using an 'OnClick' event or a 'CommandArgument'. I tried using 'OnClick' and in the code behind simply using:
LinkButton lb = ((LinkButton) FindControl("lbYear");
lb.Enabled = false;
Any help, thoughts, ideas, examples, etc. would be greatly appreciated. Thank you all in advance!
Adjusted Code:
<ItemTemplate>
<asp:LinkButton ID="lbYear" runat="server" OnClick="testClick" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
<a id="MyContrl_lbYear" href="javascript:__doPostBack('MyContrl$lbYear','')" onclick="this.href='#';this.disabled=true;__doPostBack('MyContrl$lbYear','');"></a>
</ItemTemplate>
protected void showChart(object sender, EventArgs e)
{
LinkButton lbYear = ((LinkButton)FindControl("lbYear"));
lbYear.Attributes.Add("onclick", "this.href='#';this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(lbYear, "").ToString());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选项 3 和你最后的方法似乎是正确的选择。 LinkButtons 的问题在于,即使将 Enabled 设置为 false,您也不会阻止它们回发。请参阅:http://weblogs.asp.net/jeffwids/archive/2011/02/14/how-to-disable-an-asp-net-linkbutton-when-clicked.aspx
因此你必须手动执行此操作:
Option 3 and your last approach seem to be the way to go. The problem with LinkButtons is that even by putting Enabled on false, you wont block them from posting back. See: http://weblogs.asp.net/jeffwids/archive/2011/02/14/how-to-disable-an-asp-net-linkbutton-when-clicked.aspx
Therefore you have to do this manually with:
Polity,非常感谢您的帮助,我真的很感激。不过,我找到了一种不同的方法来解决这个问题。
.ASPX 代码:
.ASPX.CS 代码:
Polity, thank you very much for your help, I really appreciate it. I found a different way to go about fixing this issue though.
.ASPX Code:
.ASPX.CS Code: