Reputation: 1169
I have rlly trouble with a imo simple thing i allready did like million times, but this time, I cant.
I have LinkButtons in my Gridview like
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="GVCon_TFEdit" runat="server" CssClass="eh_label_style" CommandArgument="Edit" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
And those i have six times. Allways one Edit and one Delete-LinkButton.
Then i try to change the visible of them in Codebehind at line 5 (if i comment it out, then on 6 etc):
1 protected void Page_PreRenderComplete(object sender, EventArgs e)
2 {
3 if (Session["Permissions"].ToString() == "User")
4 {
5 GVComp_TFDelete.Visible = false;
6 GVComp_TFEdit.Visible = false;
7 GVCon_TFDelete.Visible = false;
8 GVCon_TFEdit.Visible = false;
9 GVMatr_TFDelete.Visible = false;
10 GVMatr_TFEdit.Visible = false;
But it allways brings me the goodknown exception:
Operation is not valid due to the current state of the object
Well - I'm sure the object isnt null there anymore ?! (And Session["Permissions"] isnt null for sure also, because it is getting his Status in PreLoad allready ...)
And maybe I have to tell you more, because i had problems with EXACTLY those LinkButtons bevore ^^ So - in those TemplateFields i had exactly the same LinkButtons, then i changed the name of those LB, and since then i couldnt use them in CodeBehind. It was because they werent in the designerfile anymore ... So i tried alot of tips I found evrywhere in the inet, also tips from here - but they didnt worked (Like rebuilding the designerfile, writing the LinkButtons new into the aspx-file also I made sure that the file is in the right folder etc ...) Nothing worked. So i added them by hand into the designer.cs. Now i can "use" them in the CodeBehind, but when I start debugging - it runs into this error.
Would be really nice if you could help me, this is so annoying ...
Upvotes: 2
Views: 1226
Reputation: 501
You can find also with RowCommand
event by this way
LinkButton name = (LinkButton)gridview.Rows[index].FindControl("lnkbtnActionNames");
name.visible=false;
Upvotes: 2
Reputation: 1939
Since you are using a Gridview, you will have to use the event RowDataBound
for the gridview.
And in that event, you will have to check for e.Row.RowType == DataControlRowType.DataRow
.
After that, you can use
LinkButton lb = ((LinkButton)e.Row.FindControl("GVCon_TFEdit"));
to get the linkbutton for the current row, and then use lb.Visible = false;
or whatever code you need.
Upvotes: 1