Getting event when asp:checkboxfield is clicked

D

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) ;)
 
D

David Ching

David Ching said:
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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top