gpt4 book ai didi

c# - INT 在复选框中转换为可更新的 bool 值

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:36 25 4
gpt4 key购买 nike

我已经编程很多年了,大约有 10 年使用旧版本的 .NET,因此我对 LINQ、UpdatePanels 和 Ajax 以及它们的工作方式还是陌生的。
以下代码可以正常工作。
数据库字段定义为:

rptPriority INT NOT NULL DEFAULT(0)

一个非常简化的有效页面 stub 是:

<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" />

等等
我需要做的是将该动态控件作为文本框更改为可更新并由相同数据驱动的复选框。以下给出了正确的数据,但没有更新。
如果我直接使用 SQL 或过程来执行此操作,这将不是一个大问题。 (也不是一个选项)

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

我将“Eval”更改为“Bind”并得到了“ERROR CS0103: The name 'Bind' does not exist in the current context”
我怀疑“Convert.ToBoolean”是问题的一部分。
我仔细阅读了很多页面,试图获取我需要的信息。其中How to add checkbox to datagrid in vb.net , Html.CheckBox returns false if disabled, even if selecedCheckbox 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?更不用说堆栈溢出之外的大约 50 个,这让我到了现在的位置。
编辑
根据 Rob 的建议,我添加了以下内容,但无法获得/找到任何使用“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;
}

以下是来自后面保存/更新代码的 stub 。

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 */);
}
}

最佳答案

正如您所发现的,Bind() 没有提供一种转换数据类型的简单方法。您会发现其他建议更改您绑定(bind)的数据类型的答案,但我知道这不是您的选择。

我认为最好的解决方案是回到单向绑定(bind)。例如使用您现有的代码:

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

这将在呈现 DetailsView 时正确设置复选框的值,但不会在更新详细信息时为 rptPriority 提供值。为此,您需要将 OnItemUpdating 事件处理程序添加到您的 DetailsView,这将允许您执行您需要的自定义转换。例如

<asp:DetailsView ID="dvReport" runat="server"
onitemupdating="dvReport_ItemUpdating"

代码隐藏中的实现将如下所示:-

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;
}

希望这对您有所帮助。

关于c# - INT 在复选框中转换为可更新的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20694537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com