Why doesn't CheckBox in FormView save OldValues ​​and NewValues?

My FormView with two flags - chbFFT and chbCertG ...

<asp:FormView HeaderText="Basic Enrolment" HeaderStyle-HorizontalAlign="Center" ID="FormView1"
    runat="server" CellPadding="4" DataKeyNames="StudentID" DataSourceID="SqlDataSource1"
    ForeColor="#333333" Style="width: 100%" OnItemUpdated="FormView1_ItemUpdated" OnItemUpdating="FormView1_ItemUpdating" OnDataBound="FormView1_DataBound" >
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <EditItemTemplate>
        <table style="width: 100%">
            <tr>
                <td>
                    <table cellpadding="4" style="width: 100%">
                    ..
                    ..
                        <tr>
                            <td valign="top" align="right">
                                FFT / Cert. Guarantee
                            </td>
                            <td style="vertical-align: top;">
                                <asp:CheckBox ID="chbFFT" runat="server" Checked='<%# Convert.ToBoolean(Eval("StudentHasFFT")) %>' Text="FFT" ToolTip="Tick this to alert WhiteRoom the student has FFT competencies. Update required to send alert." />
                                <asp:CheckBox ID="chbCertG" runat="server" Checked='<%# Convert.ToBoolean(Eval("StudentHasCertG")) %>' Text="CertG" ToolTip="Tick this to alert WhiteRoom the student has a Certificate Guarantee." />
                            </td>
                        </tr>
                   ..
                   ..
                    </table>
                </td>
            </tr>
        </table>
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
            Text="Update">
        </asp:LinkButton>
        <asp:LinkButton OnClick="EnableSideBarDisplay" ID="UpdateCancelButton" runat="server"
            CausesValidation="False" CommandName="Cancel" Text="Cancel">
        </asp:LinkButton>
    </EditItemTemplate>
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <ItemTemplate>
        <table style="width: 100%">
            <tr>
                <td style="width: 50%">
                    <table cellpadding="4" style="width: 100%">
                    ..
                    ..
                        <tr>
                            <td valign="top" align="right">
                                FFT / Cert. Guarantee
                            </td>
                            <td style="vertical-align: top;">
                                <asp:CheckBox ID="chbFFT" runat="server" Enabled="false" Checked='<%# Convert.ToBoolean(Eval("StudentHasFFT")) %>' Text="FFT" ToolTip="Tick this to alert WhiteRoom that the student has at least one FFT competency. Update required to send alert." />
                                <asp:CheckBox ID="chbCertG" runat="server" Enabled="false" Checked='<%# Convert.ToBoolean(Eval("StudentHasCertG")) %>' Text="CertG" ToolTip="Tick this to alert WhiteRoom the student has a Certificate Guarantee." />
                            </td>
                        </tr>
                     ..
                     ..
                    </table>
                </td>
            </tr>
        </table>
        <asp:LinkButton OnClick="DisableSideBarDisplay" ID="lnkbttnEditItems" runat="server"
            CausesValidation="False" CommandName="Edit" Text="Edit" OnInit="SetVisibility">
        </asp:LinkButton>
    </ItemTemplate>
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
</asp:FormView>

As you can see, a lot of other code, such as text fields and labels, has been removed in the whole form.

I also have some events to trigger: ItemUpdated, ItemUpdating, etc.

In ItemUpdated, for example, I email people based on situations with the form. There I checked some data checksums (text fields) to verify that OldValues ​​and NewValues ​​guarantee that people will be emailed. Obviously, I don’t want to send people to people all the time when the user presses UPDATE, especially when nothing in the form has changed. This works great.

protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
    //Email accounts when student withdrawn
    if (e.NewValues["StudentEnrolmentStatus"].ToString() == "20" && e.OldValues["StudentEnrolmentStatus"].ToString() != "20")
    {
        //blahblahblah
        //...

        SmtpClient smtpClient = new SmtpClient();
        try
        {
            smtpClient.Send(message);
        }
        catch (Exception ex) { }
    }
}

CheckBoxes, OldValues, NewValues ​​ . NULL. , .

//email WhiteRoom when student has been flagged FFT or CertG
if (e.NewValues["StudentHasFFT"].ToString() == "1" || !e.OldValues["StudentHasFFT"].ToString() == "1")
    {
    //blahblahblah
    //...

    SmtpClient smtpClient = new SmtpClient();
    try
    {
        smtpClient.Send(message);
    }
    catch (Exception ex) { }
}

, FormView .., OldValues ​​ NewValues, CheckBoxes?

ItemUpdating (NewValues), , , , ItemUpdated, , .

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    //convert a tickbox to '1' or '0' and not null
    FormView fv = sender as FormView;
    if (fv != null)
    {
        CheckBox chbFFT = fv.FindControl("chbFFT") as CheckBox;
        if (chbFFT != null)
            e.NewValues["StudentHasFFT"] = chbFFT.Checked ? 1 : 0;
        CheckBox chbCertG = fv.FindControl("chbCertG") as CheckBox;
        if (chbCertG != null)
            e.NewValues["StudentHasCertG"] = chbCertG.Checked ? 1 : 0;
    }
}

+4
1

FormView . :

<asp:CheckBox ID="chbFFT" runat="server"
              Checked='<%# Convert.ToBoolean(Eval("StudentHasFFT")) %>'
              ...

- , . , , FormView (, NewValues) : Convert.ToBoolean(Eval("StudentHasFFT"))? , FormView .

ASP.NET :

<%# Bind("FieldName") %>

, , . :

<asp:CheckBox ID="chbFFT" runat="server"
              Checked='<%# Bind("StudentHasFFT") %>'
              ...

, StudentHasFFT , . . db - , .

+1

Source: https://habr.com/ru/post/1540473/


All Articles