Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Readonly bound field returns null in update method

Reply
Thread Tools

Readonly bound field returns null in update method

 
 
Ismail
Guest
Posts: n/a
 
      03-06-2007
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" />
<asparameter DefaultValue="true" Name="AddCaptain"
Type="Boolean" />
</SelectParameters>
<UpdateParameters>
<asparameter Name="TeamMemberID" Type="String"/>
<asparameter Name="MemberFirstName" Type="String" />
<asparameter Name="MemberLastName" Type="String" />
<asparameter 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

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      03-06-2007
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.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


"Ismail" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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" />
> <asparameter DefaultValue="true" Name="AddCaptain"
> Type="Boolean" />
> </SelectParameters>
> <UpdateParameters>
> <asparameter Name="TeamMemberID" Type="String"/>
> <asparameter Name="MemberFirstName" Type="String" />
> <asparameter Name="MemberLastName" Type="String" />
> <asparameter 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
>



 
Reply With Quote
 
 
 
 
Ismail
Guest
Posts: n/a
 
      03-06-2007
Eliyahu,

Many thanks for this.

Regards

Ismail

 
Reply With Quote
 
Marc BICHARA Marc BICHARA is offline
Junior Member
Join Date: Sep 2008
Posts: 3
 
      10-22-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView: how to pass readonly ID to Update method of a DataSoureObject? Bogdan ASP .Net 2 03-05-2008 02:46 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
Begin() applied on empty Vector returns NULL or non null value???? Col C++ 1 04-21-2006 01:12 PM
Set ReadOnly of DataGrid Bound column =?Utf-8?B?QmVu?= ASP .Net 0 11-12-2004 07:34 AM
ASP.NET Readonly panel function with readonly checkbox, readonly radiobutton Jonathan Hyatt ASP .Net Web Controls 1 06-08-2004 07:42 PM



Advertisments