I have been programming for many years, about 10 in an older version of .NET, as a result I'm new to LINQ, UpdatePanels and Ajax and their way of doing things.
The following code works correctly.
The database field is defined as:
rptPriority INT NOT NULL DEFAULT(0)
A much simplified page stub which works is:
<asp:UpdatePanel ID="upReport" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<fieldset>
<asp:DetailsView ID="dvReport" runat="server" DataSourceID="ldsReport" CssClass="reset"
AutoGenerateRows="False" DefaultMode="Edit" Width="100%">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:DynamicControl ID="Priority" runat="server" DataField="rptPriority" Mode="Edit" CssClass="general" />
etc
What I need to do is change that Dynamic Control as a textbox to a checkbox which is updatable and driven by the same data. The following gives the correct data but doesn't update.
Were I doing this using straight SQL or PROCEDURES, this would not be a big problem. (also not an option)
<asp:CheckBox ID="Priority" runat="server" Checked='<%# Convert.ToBoolean(Eval("rptPriority")) %>' />
I changed "Eval" to "Bind" and got the "ERROR CS0103: The name 'Bind' does not exist in the current context"
I suspect that the "Convert.ToBoolean" is part of the problem.
There are many pages which I've perused trying to get the info I need. Among them How to add checkbox to datagrid in vb.net , Html.CheckBox returns false if disabled, even if seleced and Checkbox server/client events , How do you know to use Container.DataItem when data binding in ASP.NET? Is there a reference? , The databind bind() function belongs to which class? not to mention about 50 more outside of stack overflow which got me to where I am now.
Edit
As per Rob's suggestion, I added the following, but couldn't get / find any way to use "DetailsViewUpdateEventArgs".
protected void dvReport_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
DetailsView dv = sender as DetailsView;
CheckBox ctlPriority = dv.FindControl("Priority") as CheckBox;
e.NewValues["rptPriority"] = ctlPriority.Checked ? 1 : 0;
}
The following is a stub from the save / update code behind.
protected void btnSave_OnCLick(object sender, EventArgs e)
{
RadioButtonList rblReportStatus = (RadioButtonList)dvReport.FindControl("rblReportStatus");
using (RiderReportDataContext db = new RiderReportDataContext())
{
Report report = null;
Contact contact = null;
DateTime now = DateTime.Now;
bool doUpdate = false;
ldsContact.Updating += (o, ea) =>
{
Contact original = (Contact)ea.OriginalObject;
contact = (Contact)ea.NewObject;
if (
original.FirstName != contact.FirstName ||
...
original.Email != contact.Email)
{
doUpdate = true;
}
db.Contacts.Attach(contact, original);
ea.Cancel = true;
};
// force the update procedure
dvContact.UpdateItem(true);
ldsReport.Updating += (o, ea) =>
{
Report original = ea.OriginalObject as Report;
report = ea.NewObject as Report;
if (rblReportStatus.SelectedItem != null)
{
report.ReportStatusId = int.Parse(rblReportStatus.SelectedValue);
}
if (
original.ReportStatusId != report.ReportStatusId ||
original.SourceId != report.SourceId ||
...
original.ReportTypeID != report.ReportTypeID ||
original.rptPriority != report.rptPriority || // The value here doesn't change
original.SupervisorId != report.SupervisorId)
{
doUpdate = true;
}
db.Reports.Attach(report, original);
ea.Cancel = true;
};
// force the update procedure
dvReport.UpdateItem(true);
// Other Search For Updates
if (doUpdate)
{
contact.ChangedOn = now;
report.ChangedOn = now;
Log log = new Log();
log.ReportId = report.Id;
log.UserId = MembershipClass.User.Id;
log.Text = "The report updated.";
log.CreatedOn = now;
report.Logs.Add(log);
db.SubmitChanges();
}
}
if (Page.IsValid)
{
Response.Redirect("~/default.aspx" /*table.ListActionPath */);
}
}