Jack
Jack

Reputation: 10037

Question: Regarding GridView boundfield ReadOnly property

I have a Gridview boundfield where i set ReadOnly to true because i don't want user to change its value. However on the objectdatasource control's update method that boundfield became null when i try to use it as parameter in update method. Is there a way to set that value during updating?

Upvotes: 1

Views: 3343

Answers (4)

dviljoen
dviljoen

Reputation: 1632

I had a similar problem and solved it in a slightly different way. But in my case the parameter I wanted to use in my Update method was my primary key and was available in a Query string. So in my DataSource definition I defined the UpdateParameters section to use instead of . Then I was able to remove the parameter completely from my table and it would revert to the Query string parameter.

Upvotes: 0

exils
exils

Reputation: 11

Another approach is to add a new query to your tableadapter. Create an update query that just uses the fields desired to be updated. When selecting the update method on the ODS pick the update query. The BoundFields that are not part of the update query can now be turned to readonly=true and it should work.

Upvotes: 0

Barry Anderberg
Barry Anderberg

Reputation:

No, just add the field name you need to the DataKeyNames attribute of the GridView. Then the value will be sent to the Update command.

Upvotes: 5

tvanfosson
tvanfosson

Reputation: 532595

When you mark a field as read-only on the GridView it renders on the page as a span element, not an input. Therefore the value is not available on PostBack. If you can construct the update statement so that it doesn't expect this field, that would be the best way to deal with this. If the update statement is autogenerated and you can't get around having the value to update, then you can either read the value from the database before doing the update (so that you have it) or include a HiddenField bound to this column and use a literal that obtains the value via Eval instead of binding (if necessary). This will require using a template.

<asp:TemplateField>
    <InsertItemTemplate>
      <asp:TextBox runat="server" ID="itemTextBox" />
    </InsertItemTemplate>
    <EditItemTemplate>
      <asp:HiddenField runat="server" ID="itemHF" Value='<% Bind("Item") %>' />
      <asp:Label runat="server" ID="itemLabel" Text='<% Eval("Item") %>' />
    </EditItemTemplate>
    <ItemTemplate>
       <asp:Label runat="server" ID="itemLabel" Text='<% Bind("Item") %>' />
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 3

Related Questions