Solved

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

Posted on 2011-02-23
11
366 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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