Readonly bound field returns null in update method

I

Ismail

Hello,

I have grid view which can go into edit mode. I have fields one of
which is primary key I dont want to display this field but will use
this field in my update method. If I make the bound column readonly i
get nulls if i set visible to false i get nulls. When visible and not
readonly i get value however I dont want people trying to change
unique ids. Here is some code

<asp:GridView ID="dgMembers" runat="server"
AutoGenerateEditButton="True" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="TeamMemberID"
NullDisplayText="" ConvertEmptyStringToNull="False"/>
<asp:BoundField DataField="MemberFirstName"
HeaderText="First Name" />
<asp:BoundField DataField="MemberLastName"
HeaderText="Last Name" />
<asp:BoundField DataField="MemberJobTitle" HeaderText="Job
Title" />
<asp:TemplateField HeaderText="Email - Not editable">
<ItemTemplate>
<%#Eval("MemberEmail")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>

</asp:GridView>

my dataasource looks like

<asp:ObjectDataSource ID="dsMembers" runat="server"
SelectMethod="GetAllTeamMembers"
TypeName="TeamMemberController" UpdateMethod="UpdateTeamMember">
<SelectParameters>
<asp:SessionParameter Name="GUID" SessionField="GUID"
Type="String" />
<asp:parameter DefaultValue="true" Name="AddCaptain"
Type="Boolean" />
</SelectParameters>
<UpdateParameters>
<asp:parameter Name="TeamMemberID" Type="String"/>
<asp:parameter Name="MemberFirstName" Type="String" />
<asp:parameter Name="MemberLastName" Type="String" />
<asp:parameter Name="MemberJobTitle" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>

when i do this in gridview

<asp:BoundField DataField="TeamMemberID" NullDisplayText=""
ConvertEmptyStringToNull="False" ReadOnly=true/>

I dont get null passed through to update method.

Any ideas anyone?

Regards

Ismail
 
E

Eliyahu Goldin

Server controls with Visible=false don't get rendered to the client and
don't come back on postbacks. You should leave Visible=true and hide the
column with css rule display:none.
 
Joined
Sep 23, 2008
Messages
3
Reaction score
0
There is another possibility

If the hidden field is a keyfield (or I think even if it is not but I did not try it ) you can simply define the property DataKeyNames equal to the field then you can reference the value in your code by DataKeys["fieldname"]

Ex :
<asp:GridView Id="Grv1" ... DataKeyNames="clepri" runat="sever">
......
</asp:Gridview>

clepri is a field that do not show in you Gridview. In my case it was the primary key of the DataSource to which the Griview was bound. But I think it could work for anyfield.

then if you select a row
in your code behind after postback you can reference the value of clepri of the selected item by either
Grv1.DataKeys["clepri"].ToString();
or
Grv1.SelectedDataKey["clepri"].ToString();
the second method will render not on postback but on SelectedIndexChanged Event procedure, so I will rather advise to use the first method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top