禁用 DataGrid TemplateColumn 中的 LinkBut​​ton

发布于 2024-11-07 11:16:03 字数 2908 浏览 4 评论 0原文

我正在使用 Visual Studio 2008 和 .NET Framework 3.5。我有一个 DataGrid,在 TemplateColumn 内有一个 LinkBut​​ton。我试图弄清楚如何禁用单击 LinkBut​​ton 后单击它的功能。我的 DataGrid 有 6 列,其中 LinkBut​​ton 列显示年份,其他列显示这些年份的年末数据。单击某一年时,DataGrid 会按月显示该年数据的详细信息。当 DataGrid 显示按月细分时,我仍然需要年份列可见,但无法单击。我还有一个按钮和一个图表,默认情况下“可见性”设置为“假”,但选择一年后,“可见性”设置为“真”,该按钮可以关闭按月细分并返回到年份结束崩溃。除了禁用 LinkBut​​ton 之外,一切都正常。

这是我的 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>

-- 第三次尝试很接近,它使 LinkBut​​tons 变灰,但没有禁用单击它们的功能 尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-11-14 11:16:03

选项 3 和你最后的方法似乎是正确的选择。 LinkBut​​tons 的问题在于,即使将 Enabled 设置为 false,您也不会阻止它们回发。请参阅:http://weblogs.asp.net/jeffwids/archive/2011/02/14/how-to-disable-an-asp-net-linkbutton-when-clicked.aspx

因此你必须手动执行此操作:

lb.Attributes.Add("onclick", "this.href='#';this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(lb, "").ToString());

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:

lb.Attributes.Add("onclick", "this.href='#';this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(lb, "").ToString());
温柔少女心 2024-11-14 11:16:03

Polity,非常感谢您的帮助,我真的很感激。不过,我找到了一种不同的方法来解决这个问题。

.ASPX 代码:

<TemplateColumn>
    <ItemTemplate>
        <asp:LinkButton runat="server" OnClick="test" Text='<%# DataBinder.Eval(Container, "DataItem.year") %></asp:LinkButton>
    </ItemTemplate>
</TemplateColumn>

.ASPX.CS 代码:

protected void test(object sender, EventArgs e)
{
    foreach(var y in myDataGrid.Items)
    {
        LinkButton lb = ((y as TableRow).Cells[1].Controls[1] as LinkButton);
        lb.Enabled = false;
    }
}

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:

<TemplateColumn>
    <ItemTemplate>
        <asp:LinkButton runat="server" OnClick="test" Text='<%# DataBinder.Eval(Container, "DataItem.year") %></asp:LinkButton>
    </ItemTemplate>
</TemplateColumn>

.ASPX.CS Code:

protected void test(object sender, EventArgs e)
{
    foreach(var y in myDataGrid.Items)
    {
        LinkButton lb = ((y as TableRow).Cells[1].Controls[1] as LinkButton);
        lb.Enabled = false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文