Hi,
I am trying to write some code to disable a textbox control in a GridView row depending on the user_ID of the person logged into my site. I am having difficulty doing this, as I seem to be only able to access the controls in the itemtemplate and unable to set the GridViewRow to edit mode. Can someone please advise as to what event I should put the code in? I will simplify the real world application into the following example:
When the page first loads, I display a GridView control with 3 template columns. EVERYONE who logs in should see these columns (i.e. itemtemplates all contain Label controls displaying a number), However, when someone clicks the Edit button on a GridViewRow, if they were not the person who originally added the record to the database, they cannot alter the value in column 1 (textbox), but can still edit the values in columns 2 and 3. I am looking for the right place to put the code that disables the textbox in column 1. I tried the RowUpdating event, but could only access the Label controls in the itemtemplates (as at this stage, the row hasn't actually entered Edit mode). The RowUpdating event is too late.
Very simplified markup:
<asp:GridView ID="GridView" runat="server" >
<Columns>
<asp:TemplateField>
<edititemtemplate>
<asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" ValidationGroup="Updating" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</edititemtemplate>
<itemtemplate>
<asp:LinkButton id="lbEdit" runat="server" Text="Edit" CausesValidation="False" CommandName="Edit"></asp:LinkButton>
<asp:LinkButton id="lbDelete" runat="server" Text="Delete" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you certain you want to delete this record?');"></asp:LinkButton>
</itemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column1">
<itemtemplate>
<asp:Label ID="lblColumn1" runat="server" Text='<%# Bind("Column1") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="tbColumn1" runat="server" Text='<%# Bind("Column1") %>'></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<itemtemplate>
<asp:Label ID="lblColumn2" runat="server" Text='<%# Bind("Column2") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="tbColumn2" runat="server"
Text='<%# Bind("Column2")
%>'></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column3">
<itemtemplate>
<asp:Label ID="lblColumn3" runat="server" Text='<%# Bind("Column3") %>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="tbColumn3" runat="server"
Text='<%# Bind("Column3")
%>'></asp:TextBox>
</edititemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Sort of code I am trying:
GridViewRow gvr = (GridViewRow)GridView.Rows[int]; (or, depending on where it lives, setting the row by eventArgs i.e. e.Row.RowIndex etc..)
if (conditional args to find if person owns record.....)
{
TextBox tbColumn1 = (TextBox)gvr.FindControl("tbColumn1");
tbColumn1.Enabled = false;
}
this of course isn't working - it is not finding the textbox becuase I cannot figure out when the edititemtemplate controls become available.
I have tried row.RowState = DataControlRowState.Edit; but this doesn't work as it is read only.
Any help would be appreciated.
Tom