Readonly bound field returns null in update method

Discussion in 'ASP .Net' started by Ismail, Mar 6, 2007.

  1. Ismail

    Ismail Guest

    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
     
    Ismail, Mar 6, 2007
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    >
     
    Eliyahu Goldin, Mar 6, 2007
    #2
    1. Advertising

  3. Ismail

    Ismail Guest

    Eliyahu,

    Many thanks for this.

    Regards

    Ismail
     
    Ismail, Mar 6, 2007
    #3
  4. Ismail

    Marc BICHARA

    Joined:
    Sep 23, 2008
    Messages:
    3
    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.
     
    Marc BICHARA, Oct 22, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVu?=

    Set ReadOnly of DataGrid Bound column

    =?Utf-8?B?QmVu?=, Nov 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    464
    =?Utf-8?B?QmVu?=
    Nov 12, 2004
  2. vizlab
    Replies:
    3
    Views:
    4,233
    Michael Bar-Sinai
    Oct 17, 2007
  3. Bogdan
    Replies:
    2
    Views:
    5,559
    Bogdan
    Mar 5, 2008
  4. Jonathan Hyatt
    Replies:
    1
    Views:
    2,015
    Jonathan Hyatt
    Jun 8, 2004
  5. Nick Bennett
    Replies:
    1
    Views:
    208
    Bob Barrows [MVP]
    Jan 20, 2008
Loading...

Share This Page