关键是在同一个edititemtemplate添加一个label,和dropdownlist,因为第一个控件,vs2005会自动绑定,即使你不在label中加<%# str %>也会自动绑定所以要先加一个label再加dropdownlist,气死我了,居然有这种事
然后dropdownlist的数据绑定我是放在itemcommand中的
if(e.CommandName=="Edit"){.........}
下面是aspx代码---------------------------
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="cxywmkxg.ascx.cs" Inherits="xianbiancang.qiantaimokuai_cxywmkxg" %>
<table width="700" cellpadding="0" cellspacing="0">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="3">
<asp:DetailsView ID="dvyw" runat="server" AutoGenerateRows="False" Height="50px"
OnItemUpdating="dvyw_ItemUpdating" OnModeChanging="dvyw_ModeChanging" Width="498px" OnItemCommand="dvyw_ItemCommand" OnPageIndexChanging="dvyw_PageIndexChanging">
<Fields>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="登記時間">
<EditItemTemplate>
<asp:Label ID="rq_e" runat="server" Text='<%# Bind("rq") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("rq") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="rq_i" runat="server" Text='<%# Bind("rq") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="客戶代號">
<EditItemTemplate>
<asp:TextBox ID="khdh_e" runat="server" Text='<%# Bind("khdh") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("khdh") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="khdh_i" runat="server" Text='<%# Bind("khdh") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="客戶名字">
<EditItemTemplate>
<asp:TextBox ID="khmc_e" runat="server" Text='<%# Bind("khmc") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("khmc") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="khmc_i" runat="server" Text='<%# Bind("khmc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="訂單號">
<EditItemTemplate>
<asp:Label ID="ddh_e" runat="server" Text='<%# Bind("ddh") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ddh") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ddh") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="業務">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:DropDownList ID="yw_e" runat="server" Width="110px">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("yw") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="yw_i" runat="server" Text='<%# Bind("yw") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="業助">
<EditItemTemplate>
<asp:TextBox ID="yz_e" runat="server" Text='<%# Bind("yz") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("yz") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("yz") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="接單時間">
<EditItemTemplate>
<asp:TextBox ID="jdsj_e" runat="server" Text='<%# Bind("jdsj") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("jdsj") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("jdsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="出貨時間">
<EditItemTemplate>
<asp:TextBox ID="chsj_e" runat="server" Text='<%# Bind("chsj") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("chsj") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("chsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="產品總量">
<EditItemTemplate>
<asp:TextBox ID="cpzl_e" runat="server" Text='<%# Bind("cpzl") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("cpzl") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("cpzl") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="產品明細">
<EditItemTemplate>
<asp:TextBox ID="cpmx_e" runat="server" Text='<%# Bind("cpmx") %>' TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("cpmx") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("cpmx") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="業務確認出貨時間">
<EditItemTemplate>
<asp:TextBox ID="ywqrchsj_e" runat="server" Text='<%# Bind("ywqrchsj") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("ywqrchsj") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("ywqrchsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="生管刀胚確認">
<EditItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("sgdpqr") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox12" runat="server" Text='<%# Bind("sgdpqr") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("sgdpqr") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="生管欠料明細">
<EditItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Bind("sgqlmx") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("sgqlmx") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Bind("sgqlmx") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="物控配件確認">
<EditItemTemplate>
<asp:Label ID="Label14" runat="server" Text='<%# Bind("wkpjqr") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox14" runat="server" Text='<%# Bind("wkpjqr") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label14" runat="server" Text='<%# Bind("wkpjqr") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="物控配件欠料明細">
<EditItemTemplate>
<asp:Label ID="Label15" runat="server" Text='<%# Bind("wkpjqlmx") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox15" runat="server" Text='<%# Bind("wkpjqlmx") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label15" runat="server" Text='<%# Bind("wkpjqlmx") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="物控包裝確認">
<EditItemTemplate>
<asp:Label ID="Label16" runat="server" Text='<%# Bind("wkbzqr") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox16" runat="server" Text='<%# Bind("wkbzqr") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label16" runat="server" Text='<%# Bind("wkbzqr") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="物控包裝欠料明細">
<EditItemTemplate>
<asp:Label ID="Label17" runat="server" Text='<%# Bind("wkbzqlmx") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox17" runat="server" Text='<%# Bind("wkbzqlmx") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label17" runat="server" Text='<%# Bind("wkbzqlmx") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="採購配件進料時間">
<EditItemTemplate>
<asp:Label ID="Label18" runat="server" Text='<%# Bind("cgpjjlsj") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox18" runat="server" Text='<%# Bind("cgpjjlsj") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label18" runat="server" Text='<%# Bind("cgpjjlsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="採購包裝進料明細">
<EditItemTemplate>
<asp:Label ID="Label19" runat="server" Text='<%# Bind("cgbzjlsj") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox19" runat="server" Text='<%# Bind("cgbzjlsj") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label19" runat="server" Text='<%# Bind("cgbzjlsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="製造回覆完成時間">
<EditItemTemplate>
<asp:Label ID="Label20" runat="server" Text='<%# Bind("zzhfwcsj") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox20" runat="server" Text='<%# Bind("zzhfwcsj") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label20" runat="server" Text='<%# Bind("zzhfwcsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="船務確認進倉時間">
<EditItemTemplate>
<asp:Label ID="Label21" runat="server" Text='<%# Bind("cwqrjcsj") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox21" runat="server" Text='<%# Bind("cwqrjcsj") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label21" runat="server" Text='<%# Bind("cwqrjcsj") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
接着下来是cs代码-----------------------------------------------
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;
namespace xianbiancang
{
public partial class qiantaimokuai_cxywmkxg : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dvyw_bind();
}
}
//protected void dvyw_ModeChanging(object sender, DetailsViewModeEventArgs e)
//{
// dvyw.ChangeMode(DetailsViewMode.Edit);
// //dvyw.CurrentMode = DetailsViewMode.Edit;
//}
protected void dvyw_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
}
protected void dvyw_bind()
{
string ddh = Request.QueryString[0].ToString();
DataSet ds = new DataSet();
ds = selectforsimplecontrol.selectfordataset("select * from chanxiao where ddh='" + ddh + "'");
dvyw.DataSource = ds.Tables[0];
dvyw.DataBind();
dvyw.ChangeMode(DetailsViewMode.Edit);
}
protected void dvyw_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
//((TextBox)dvyw.FindControl("khmc_e")).Enabled = false;
dvyw.ChangeMode(DetailsViewMode.Edit);
Response.Write(((Label)dvyw.FindControl("Label1")).Text);
string yw = ((Label)dvyw.FindControl("Label1")).Text;
((DropDownList)dvyw .FindControl ("yw_e")).Items.Add(yw);
}
if (e.CommandName == "Update")
{
#region 更新字符串
string ddh = ((Label)dvyw.FindControl("ddh_e")).Text;
string khdh = ((TextBox)dvyw.FindControl("khdh_e")).Text;
string khmc = ((TextBox)dvyw.FindControl("khmc_e")).Text;
string yw = ((DropDownList )dvyw.FindControl("yw_e")).SelectedItem .Text ;
string yz = ((TextBox)dvyw.FindControl("yz_e")).Text;
string jdsj = ((TextBox)dvyw.FindControl("jdsj_e")).Text;
string chsj = ((TextBox)dvyw.FindControl("chsj_e")).Text;
string cpmx = ((TextBox)dvyw.FindControl("cpmx_e")).Text;
string cpzl = ((TextBox)dvyw.FindControl("cpzl_e")).Text;
string ywqrchsj = ((TextBox)dvyw.FindControl("ywqrchsj_e")).Text;
string sqlstr = "update chanxiao set khdh='"
+ khdh +
"',khmc='" + khmc +
"',yw='" + yw +
"',yz='" + yz +
"',jdsj='" + jdsj +
"',chsj='" + chsj +
"',cpmx='" + cpmx +
"',cpzl='" + cpzl +
"',ywqrchsj='" + ywqrchsj +
"' where ddh='" + ddh + "'";
insertdeleteupdata.ins("update chanxiao set khdh='"
+ khdh +
"',khmc='" + khmc +
"',yw='" + yw +
"',yz='" + yz +
"',jdsj='" + jdsj +
"',chsj='" + chsj +
"',cpmx='" + cpmx +
"',cpzl='" + cpzl +
"',ywqrchsj='" + ywqrchsj +
"' where ddh='" + ddh + "'");
//Response.Write(khdh);
#endregion
dvyw.ChangeMode(DetailsViewMode.ReadOnly);
dvyw_bind();
}
if (e.CommandName == "Cancel")
{
dvyw.ChangeMode(DetailsViewMode.ReadOnly);
dvyw_bind();
}
}
protected void dvyw_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
//dvyw.ChangeMode(DetailsViewMode.Edit);
//dvyw.ChangeMode(DetailsViewMode.Edit);
}
protected void dvyw_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
}
}
}