?
Solved

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

Posted on 2011-02-23
11
Medium Priority
?
373 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 2000 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
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 

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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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