Solved

Make a TemplateField ReadOnly in gridView when Edit button is clicked

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

17 Experts available now in Live!

Get 1:1 Help Now