Getting event when asp:checkboxfield is clicked

Discussion in 'ASP .Net Web Controls' started by David Ching, Nov 1, 2007.

  1. David Ching

    David Ching Guest

    Hello, I have a DetailsView with a <asp:checkboxfield> in it. Here's the
    abbreviated definition:

    <asp:DetailsView runat="server" id="DetailsView1"
    OnItemCommand="DetailsView1_OnItemCommand">
    <Fields>
    <asp:checkboxfield DataField="Contractor1099"
    SortExpression="Contractor1099" HeaderText="Independent">
    </asp:checkboxfield>
    </Fields>


    I want to get notified when the user clicks the checkbox so that I can
    show/hide other fields in the DetailsView. But there seems no notification
    of this! The <asp:checkboxfield> does not have an OnClick event (unlike an
    <asp:checkbox>), and the DetailsView's OnItemCommand() is not called when
    the checkbox field is clicked.

    Please help.

    Thanks,
    David
    Visual C++ MVP (and feeling totally lost in ASP.NET) ;)
     
    David Ching, Nov 1, 2007
    #1
    1. Advertising

  2. David Ching

    David Ching Guest

    "David Ching" <> wrote in message
    news:NgtWi.17925$...
    > Hello, I have a DetailsView with a <asp:checkboxfield> in it. Here's the
    > abbreviated definition:
    >
    > <asp:DetailsView runat="server" id="DetailsView1"
    > OnItemCommand="DetailsView1_OnItemCommand">
    > <Fields>
    > <asp:checkboxfield DataField="Contractor1099"
    > SortExpression="Contractor1099" HeaderText="Independent">
    > </asp:checkboxfield>
    > </Fields>
    >
    >
    > I want to get notified when the user clicks the checkbox so that I can
    > show/hide other fields in the DetailsView. But there seems no
    > notification of this! The <asp:checkboxfield> does not have an OnClick
    > event (unlike an <asp:checkbox>), and the DetailsView's OnItemCommand() is
    > not called when the checkbox field is clicked.
    >


    I've since figured out that the <asp:checkboxfield> is not meant to post
    back to the server when clicked, that is why there are no events to support
    this. Therefore, I replaced this field with a template field containing an
    <asp:checkbox> control:

    <asp:DetailsView runat="server" id="DetailsView1"
    OnItemCommand="DetailsView1_OnItemCommand">
    <Fields>
    <asp:templatefield SortExpression="Contractor1099"
    HeaderText="Contractor is Independent">
    <EditItemTemplate>
    <asp:CheckBox runat="server" Checked='<%# Bind("Contractor1099")
    %>' id="CheckBox1099" AutoPostBack="true"
    OnCheckedChanged="CheckBox1099_OnCheckedChanged" />
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:CheckBox runat="server" Checked='<%# Bind("Contractor1099")
    %>' id="CheckBox1099" AutoPostBack="true"
    OnCheckedChanged="CheckBox1099_OnCheckedChanged" />
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:CheckBox runat="server" Checked='<%# Bind("Contractor1099")
    %>' Enabled="false" id="CheckBox1099" />
    </ItemTemplate>
    </asp:templatefield>
    </Fields>


    This almost works. Due to the AutoPostBack="true", the state of the
    checkbox is posted to the server, and the CheckBox1099_OnCheckedChanged()
    script is called.

    But now the issue is that the rest of the DetailsView fields were not saved
    to the database when the postback occurred, so the page is displayed with
    the other fields empty again. How do I get the other fields saved to the
    database during the postback so that they are refreshed correctly when the
    checkbox is clicked? (I suppose it's like clicking the Insert/Update button
    prior to the auto-postback?)

    Thanks,
    David
     
    David Ching, Nov 2, 2007
    #2
    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?QnJhZA==?=

    checkBoxField on gridView

    =?Utf-8?B?QnJhZA==?=, Dec 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    21,515
    bhawin13
    Dec 22, 2005
  2. Replies:
    3
    Views:
    452
    Jon Paal
    Jan 17, 2006
  3. hazz
    Replies:
    2
    Views:
    10,030
  4. graphicsxp
    Replies:
    1
    Views:
    2,641
    Ray Booysen
    Jul 14, 2006
  5. Replies:
    0
    Views:
    296
Loading...

Share This Page