Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-23
11
Medium Priority
?
375 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
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!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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