Solved

Make a TemplateField ReadOnly in gridView when Edit button is clicked

Posted on 2010-08-30
3
2,210 Views
Last Modified: 2012-05-10
Hello.
I have a gridView with Edit button. My columns are template columns such as:

<asp:TemplateField HeaderText="Start Date">
                    <ItemTemplate>
                        <asp:Label ID="lblStartDate1" runat="server" Text='<%# Eval("StartDate")>'></asp:Label>                        
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtStartDate1" Text='<%# Eval("StartDate")%>' runat="server"></asp:TextBox>                        
                    </EditItemTemplate>                    
                </asp:TemplateField>

My gridView is nested. So whenever a row doesn't have children, all columns should be editable, and when a row has children, some columns become summary columns, and should not be editable.
How would I achive this with templateFields? I just want to show a label like it is, not convert it into a textbox.
0
Comment
Question by:yanasizonenko
3 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 33560645
use ReadOnly="True|False"

0
 
LVL 30

Accepted Solution

by:
MlandaT earned 500 total points
ID: 33560761
Also consider moving both controls into the ItemTemplate. Then show/hide the label/textbox based on you condition for showing the field.

in ASPX:

<asp:TemplateField HeaderText="Start Date">
       <ItemTemplate>
              <asp:Label ID="lblStartDate1" runat="server" Text='<%# Eval("StartDate")>'></asp:Label>                        
              <asp:TextBox ID="txtStartDate1" Text='<%# Eval("StartDate")%>' runat="server"></asp:TextBox>
       </ItemTemplate>                
</asp:TemplateField>

in ASPX.VB.. DataBound event of gridView:

If e.Row.RowType = DataControlRowType.DataRow Then

     Dim myTextBox = CType(e.Item.Cells(index).Controls(1), TextBox)
     Dim myLabel = CType(e.Item.Cells(index).Controls(0), Label)
     
     myTextBox.Visible =Not FIELD_SHOULD_BE_READONLY_CONDITION
     myLabel.Visible = FIELD_SHOULD_BE_READONLY_CONDITION

End If
0
 

Author Closing Comment

by:yanasizonenko
ID: 33572448
I used the code on RowDataBound event
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question