Solved

How to keep a field as read only in a gridview that allows updates

Posted on 2011-02-23
11
372 Views
Last Modified: 2012-05-11
I have a gridview that updates the underlying data.  I used this for my starting point: http://msdn.microsoft.com/en-us/library/ms972948.aspx

One of the fields in the row needs to be left as "read only":
<asp:BoundField DataField="EmployeeID" ReadOnly="True" Visible="true" />

But here is the problem.  I don't want to show it on the gridview.  So if I change it to this...
<asp:BoundField DataField="EmployeeID" ReadOnly="True" Visible="false" />

...the value gets passed to the business and data tier as zero instead of its true value.  If I set it to true, it works just fine.

Is there a way gridview can be set up to just update certain fields, or is it an "all or nothing" deal?
0
Comment
Question by:ToolTimeGang
[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
  • 6
  • 5
11 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34965605
Is EmployeeID the key field? What happens if you just remove the BoundField from the grid completely?
0
 

Author Comment

by:ToolTimeGang
ID: 34965635
No it is not the key field.  If I take it out, the value is 0.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 34965680
I guess your other option is to use a TemplateField with a HiddenField:
<asp:TemplateField>
    <EditItemTemplate>
        <asp:HiddenField ID="HdnEmpID" runat="server" Value='<%# Bind("EmployeeID") %>'></asp:HiddenField>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:HiddenField ID="HdnEmpID" runat="server" Value='<%# Bind("EmployeeID") %>'></asp:HiddenField>
    </ItemTemplate>
</asp:TemplateField>

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:ToolTimeGang
ID: 34965727
Tried it like you have it above, but EmployeeID=0 once it gets to the busines tier.
If I change that first line to this...
<asp:TemplateField Visible="true">

...it works just fine.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34965748
So are you sorted then?
0
 

Author Comment

by:ToolTimeGang
ID: 34969890
No.  Should I be?  Does it make a difference?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34970005
When you said it worked when you set Visible="true" i thought you meant it was now working.

0
 

Author Comment

by:ToolTimeGang
ID: 34970059
Sorry for the confusion.  
This works:
                <asp:TemplateField Visible="true">
                    <EditItemTemplate>
                        <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("EmployeeID") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("EmployeeID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window


This does not work:
                <asp:TemplateField Visible="false">
                    <EditItemTemplate>
                        <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("EmployeeID") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("EmployeeID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34970093
So I assume the issue is that you don't want to have the extra column showing since it only contains a hidden field?

I think you're only option is to convert a column (one that will be displayed) into a template and smuggle the hidden field into it's definition.
0
 

Author Comment

by:ToolTimeGang
ID: 34970303
Happy dance!  That did the trick!  Thank you so very much...you made my day!
0
 

Author Closing Comment

by:ToolTimeGang
ID: 34970338
the promptness of the answers were appreciated so very much.  This was one of those things that you just can't put down until it is working.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

724 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