如何在DetailsView中实现CheckBoxList的编辑问题?

x1111 2006-10-31 12:05:53
如何在DetailsView中实现CheckBoxList的编辑。。
从数据库中取值与本地ListItem比较,显示有的值打勾,CheckBoxList的VALUES的值存放在一个字段里,且把他们的值用逗号分开,并实现编辑修改上传到数据库功能
...全文
173 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
x1111 2006-10-31
  • 打赏
  • 举报
回复
xie
x1111 2006-10-31
  • 打赏
  • 举报
回复
我是新手。。谢谢您了。我在线等您了。。。。boss急呀。工期紧呀。。。万分感谢了

对了此句报错string tmp = "," + row["PTSS"].ToString() + ",";则么解决。。。
只要cs部分的DropDownList就可以了谢谢
Eddie005 2006-10-31
  • 打赏
  • 举报
回复
虽是举手之劳,但是我想这样并不是很好,上面的例子足以模仿,自己就可以实现

分倒是无所谓的~
x1111 2006-10-31
  • 打赏
  • 举报
回复
麻烦您了。。。。能不能再给一个下拉的DropDownList的例子。。。一样的实现方式。
我把分全给你
x1111 2006-10-31
  • 打赏
  • 举报
回复
谢谢您
更新已没问题,可是此句报错string tmp = "," + row["PTSS"].ToString() + ",";
用户代码没处理,,,
Eddie005 2006-10-31
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CSDN_Questions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (this.DetailsView1.CurrentMode == DetailsViewMode.Edit )
{
DataRowView row = (DataRowView)this.DetailsView1.DataItem;

string tmp = "," + row["interested"].ToString() + ",";

CheckBoxList cblst = (CheckBoxList)this.DetailsView1.FindControl("CheckBoxList1");
foreach (ListItem item in cblst.Items)
if (tmp.IndexOf("," + item.Value + ",") > -1)
item.Selected = true;
else
item.Selected = false;
}
}

protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
CheckBoxList cblst = (CheckBoxList)this.DetailsView1.FindControl("CheckBoxList1");
if (cblst != null)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (ListItem item in cblst.Items)
if (item.Selected)
sb.Append(item.Value + ",");

this.AccessDataSource1.UpdateParameters["interested"].DefaultValue = sb.ToString().TrimEnd(",".ToCharArray()); ;
}
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
CheckBoxList cblst = (CheckBoxList)this.DetailsView1.FindControl("CheckBoxList1");
if (cblst != null)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (ListItem item in cblst.Items)
if (item.Selected)
sb.Append(item.Value + ",");

this.AccessDataSource1.InsertParameters["interested"].DefaultValue = sb.ToString().TrimEnd(",".ToCharArray()); ;
}
}
}
Eddie005 2006-10-31
  • 打赏
  • 举报
回复
完整例子:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Questions.aspx.cs" Inherits="CSDN_Questions" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" xml:lang="zh-cn">
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="id" DataSourceID="AccessDataSource1" Height="50px" Width="328px" OnDataBound="DetailsView1_DataBound" OnItemInserting="DetailsView1_ItemInserting" OnItemUpdating="DetailsView1_ItemUpdating">
<Fields>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
<asp:BoundField DataField="deptid" HeaderText="deptid" SortExpression="deptid" />
<asp:BoundField DataField="countyID" HeaderText="countyID" SortExpression="countyID" />
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>足球</asp:ListItem>
<asp:ListItem>篮球</asp:ListItem>
<asp:ListItem>羽毛球</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "interested")%>
</ItemTemplate>
<HeaderTemplate>
interested
</HeaderTemplate>
<InsertItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>足球</asp:ListItem>
<asp:ListItem>篮球</asp:ListItem>
<asp:ListItem>羽毛球</asp:ListItem>
</asp:CheckBoxList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/test.mdb"
DeleteCommand="DELETE FROM [employees] WHERE [id] = ?" InsertCommand="INSERT INTO [employees] ([id], [name], [sex], [deptid], [countyID], [interested]) VALUES (?, ?, ?, ?, ?, ?)"
SelectCommand="SELECT * FROM [employees]" UpdateCommand="UPDATE [employees] SET [name] = ?, [sex] = ?, [deptid] = ?, [countyID] = ?, [interested] = ? WHERE [id] = ?">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="deptid" Type="Int32" />
<asp:Parameter Name="countyID" Type="Int32" />
<asp:Parameter Name="interested" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="deptid" Type="Int32" />
<asp:Parameter Name="countyID" Type="Int32" />
<asp:Parameter Name="interested" Type="String" />
</InsertParameters>
</asp:AccessDataSource>

</form>
</body>
</html>
x1111 2006-10-31
  • 打赏
  • 举报
回复
没有人会吗?

62,133

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧