Solved

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

Posted on 2011-02-23
11
364 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

12 Experts available now in Live!

Get 1:1 Help Now