Solved

Make a TemplateField ReadOnly in gridView when Edit button is clicked

Posted on 2010-08-30
3
2,153 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now