I want to be able to hide some item controls on a Formview. I have defined a method so that when a certain requirement is met, the Add, Update and Delete linkbuttons that I have set won’t be displayed in my Formview. The code that I use to achieve this is the same as that shown below. This works correctly on initial display.
However, when the paging controls are used, and when another item is displayed in the Formview, the linkbuttons are made visible again.
I have tried using both FormView1_PageIndexChanging and _PageIndexChanged events to re-hide the linkbuttons, in the following manner:
protected void FormView1_PageIndexChanged(object sender, EventArgs e)
{
// Check to see if PDP requirement has been removed
if (txtStatusMessages.Text == "PDP Required has been set to False for this User so PDP cannot be updated or signed off.")
{
Control lb_n = FormView1.FindControl("LinkButton_New");
lb_n.Visible = false;
Control lb_e = FormView1.FindControl("LinkButton_Edit");
lb_e.Visible = false;
Control lb_d = FormView1.FindControl("LinkButton_Delete");
lb_d.Visible = false;
}
}
I realise that the idea of checking the contents of a textbox in order to hide controls is far from ideal; but at this point I just want to ensure that I can hide the item controls using this method.
When using the debugger to run through this code, the event is fired on the use of a pager button. The visible properties are correctly changed from true to false. However, the linkbuttons are still visible.
Does anyone know why this is not working as anticipated?
Thanks in advance,
Gary.
I appear to have solved the problem.
Initially I tried using
FormView1_ModeChanging, which allowed me to cancel the change of mode from ReadOnly to Edit (Upon clicking theEditlinkbutton, for example). This worked, but meant that the linkbuttons were still visible.When looking into this issue some more, I found that while I could set the Linkbutton visible properties to false, they were effectively being reset. This made me think that my issue was due to event ordering (Maybe the wrong term to use) and that a refresh of the
Formviewwas overwriting my changes.So, I added a new
HiddenFieldto store Edit Allowed type data, which would be set based on whether a user should be able to update a data item or not. I then used it with the PreRender event, as follows:This may not be an ideal solution, but allows me to manage the display of my LinkButtons as required.
Hopefully this information will be of use to someone else!
Gary.