Question: Regarding the GridView's boundfield ReadOnly property

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

+1


source to share


4 answers


When you mark a field as read-only in the GridView, it displays the span element on the page, not the input. Therefore the value is not available in PostBack. If you can build an update statement so that it doesn't expect this field, that would be the best way to handle it. If the update statement is autogenerated and you cannot get around that value to update, you can either read the value from the database before doing the update (so you have it), or turn on the HiddenField binding on that column and use the literal that gets the value via Eval instead of binding (if needed). 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>

      

+3


source


No, just add the field name that is required for the DataKeyNames attribute of the GridView. The value will then be sent to the Update command.



+4


source


Another approach is to add a new query to your tableadapter. Create an update request that simply uses the fields that need to be updated. When choosing an update method in ODS, select an update request. BoundFields that are not part of the update request can now be accessed readonly = true and should work.

0


source


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 the query string. So in my DataSource definition, I have defined an UpdateParameters section to use instead. Then I was able to completely remove the parameter from my table and it reverts back to the Query row parameter.

0


source







All Articles