Programmatically disabling textbox in GridViewRow edititemtemplate

Last post 06-24-2008 11:50 PM by jaychan. 4 replies.

Sort Posts:

  • Programmatically disabling textbox in GridViewRow edititemtemplate

    03-04-2008, 11:55 AM

    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 

  • Re: Programmatically disabling textbox in GridViewRow edititemtemplate

    03-04-2008, 12:03 PM
    Answer
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,603
    • Moderator

    Why don't you simply call the function to determine your permissions declaratively from within the ReadOnly or Enabled property of your TextBox like so: 

    <edititemtemplate>
    	<asp:textbox id="TextBox1" runat="server" readonly='<%# FunctionToCheckPermissionsWhichReturnsTrueOrFalse() %>' text='<%# Bind("MyField") %>' />
    </edititemtemplate>
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Programmatically disabling textbox in GridViewRow edititemtemplate

    03-05-2008, 4:08 AM

    thanks! solved the problem! amazing how you forget the simplest solutions..... ;)

     

     

  • Re: Programmatically disabling textbox in GridViewRow edititemtemplate

    05-06-2008, 11:07 AM
    • Loading...
    • eengin
    • Joined on 04-08-2008, 12:55 PM
    • Posts 22

    Hi,

     

    Your problem seems to be solved.  However I want to suggest another method which may be useful for you in some other cases. The problem is "reaching the controls inside the EditItemTemplate of a data presenter (e.g. gridview,  datalist ...)" .  I have faced with the same problem with a Datalist control which I called "dl_albums" and I tried to use the editCommand function as follows but unfortunately it DOES NOT WORK:

        protected void dl_albums_EditCommand(object source, DataListCommandEventArgs e) 

        {

             CheckBoxList cblist = e.Item.FindControl("cblist_friends") as CheckBoxList;

        } 

    cblist is a checkboxlist control that is defined in the EditItemTemplate of the datalist .  In this code cblist is always NULL because at the moment this function is reached the Datalist is still in normal mode (not in the edit mode) eventhough this function is called when the user presses the edit button.  Within this function you can reach any control defined in the ItemTemplate of the Datalist but not the controls that are in the EditItemTemplate of the Datalist.

     
    What one has to do in order to reach the controls in editItemTemplate is using the ItemDataBound  function (this function is specific to DataList Control,  if it is a Gridview you can use the rowDataBound function).   This function is called everytime a dataItem of the datalist is bound with some data (therefore launches for every single dataItem of the Datalist).  We are only interested in the DataItem which is to be edited.  We check this by the conditional statement if (dl_albums.EditItemIndex == e.Item.ItemIndex).  dl_albums.EditItemIndex is the index of the row being edited.  It is -1 if no row is being edited.  e.Item.ItemIndex gives us the index of the row that is just bound with data.  When  this conditional statement is true  then that means "the DataItem  that is being edited is currently bound with data."  While this is true then we can reach the control inside the EditItemTemplate of that DataItem. 

    My code for my specific application is as follows:

     
         protected void dl_albums_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            int index = dl_albums.EditItemIndex;
          
    if (dl_albums.EditItemIndex == e.Item.ItemIndex)
            {
                CheckBoxList cblist = e.Item.FindControl("cblist_friends") as CheckBoxList;
                // Do anything you want with cblist.
            }
        }

     

    Tom,  may be you already solved this but I wanted other people that face this same problem to take as reference.

    Regards...

     
     

     

     
     

  • Re: Programmatically disabling textbox in GridViewRow edititemtemplate

    06-24-2008, 11:50 PM
    • Loading...
    • jaychan
    • Joined on 10-30-2007, 1:55 PM
    • Posts 13

     

    I get some enlightment from eengin.

    Unless you are exactly at the Gridview.EditIndex, you cannot get the reference of those controls which reside in the EditItemTemplate.

    If you want to access those control in EditItemTemplate, make sure that your RowIndex equals Gridview.EditIndex

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter