Jack Jack - 1 year ago 125
ASP.NET (C#) Question

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?

Answer Source

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:TextBox runat="server" ID="itemTextBox" />
      <asp:HiddenField runat="server" ID="itemHF" Value='<% Bind("Item") %>' />
      <asp:Label runat="server" ID="itemLabel" Text='<% Eval("Item") %>' />
       <asp:Label runat="server" ID="itemLabel" Text='<% Bind("Item") %>' />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download