Solved

Make a TemplateField ReadOnly in gridView when Edit button is clicked

Posted on 2010-08-30
3
2,253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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