Home All Groups Group Topic Archive Search About

Getting event when asp:checkboxfield is clicked

Author
1 Nov 2007 11:26 PM
David Ching
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)  ;)

Author
2 Nov 2007 1:07 AM
David Ching
Show quote
"David Ching" <d*@remove-this.dcsoft.com> wrote in message
news:NgtWi.17925$JD.17133@newssvr21.news.prodigy.net...
> 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

AddThis Social Bookmark Button