Question

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());
}
Was it helpful?

Solution 2

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;
    }
}

OTHER TIPS

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top