在事件处理中添加如下代码: DetailsView dv = sender as DetailsView; if (dv is DetailsView) { FileUpload fu = dv.FindControl("FileUpload1") as FileUpload; if (fu is FileUpload) { //成功获取FileUpload控件的处理逻辑 string fileName = fu.FileName; } }
if (e.AffectedRows <= 0) 这个地方是返回在Updating事件之后,执行ObjectDataSource中的Update方法中影响的行数,所以这里你应该检查Update方法是否正确。 关于后面FindControl的地方,我试了没有问题,我是这样找的: DetailsView dv = sender as DetailsView; if (dv is DetailsView) { FileUpload fu = dv.FindControl("FileUpload1") as FileUpload; if (fu is FileUpload) { string fileName = fu.FileName; } } HasFile为True.
1. e.Values[index]
2. e.Values[key name]
绑定了四个字段(TypdID,BigTypeID,TypeName,Explain)。另外还有一个唯一的模版列,
该模版列的编辑模版中有一个名为PhotoFileUpload1的文件上传控件。
我想实现,在点击DetailsView1的 "编辑 "命令按钮后,在编辑状态可以编辑模版列中的文件上传控件,然后点
击“更新”命令按钮将图片数据通过自定义方法更新到数据库中。****遇到的问题是不在于怎么更新,而是可以触发ItemUpdated事件,并且能够找到DetailsView中的FileUpload1控件,
但确总是认为它没有上传文件。而在页面放这么一个上传控件并用不着同样的代码上传文件就没有问题。
最重要的错误是竟然在编辑行后(无论是绑定字段,还是模版字段),影响行数为0。下面的我的代码:(有问题处已用//★标记标出)
(1)这是DetailsView的定义
<asp:DetailsView ID= "DetailsView1 " runat= "server " AutoGenerateRows= "False " CellPadding= "4 "
DataSourceID= "ObjectDataSource1 " ForeColor= "#333333 " GridLines= "None " Height= "50px "
Width= "219px "
OnItemUpdated= "DetailsView1_ItemUpdated "
OnItemCommand= "DetailsView1_ItemCommand "
DataKeyNames= "TypeID "
OnDataBound= "DetailsView1_DataBound ">
<FooterStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<CommandRowStyle BackColor= "#D1DDF1 " Font-Bold= "True " />
<EditRowStyle BackColor= "#2461BF " />
<RowStyle BackColor= "#EFF3FB " />
<PagerStyle BackColor= "#2461BF " ForeColor= "White " HorizontalAlign= "Center " />
<Fields>
<asp:BoundField DataField= "TypeID " HeaderText= "TypeID " SortExpression= "TypeID " />
<asp:BoundField DataField= "BigTypeID " HeaderText= "BigTypeID " SortExpression= "BigTypeID " />
<asp:BoundField DataField= "TypeName " HeaderText= "TypeName " SortExpression= "TypeName " />
<asp:BoundField DataField= "Explain " HeaderText= "Explain " SortExpression= "Explain " />
<asp:TemplateField>
<ItemTemplate></ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID= "FileUpload1 " runat= "server " />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton= "True " ShowInsertButton= "True ">
<ItemStyle Font-Bold= "True " ForeColor= "#C00000 " />
</asp:CommandField>
</Fields>
<FieldHeaderStyle BackColor= "#DEE8F5 " Font-Bold= "True " />
<HeaderStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<AlternatingRowStyle BackColor= "White " />
</asp:DetailsView>
(2)这是DetailsView1的ItemUpdated事件
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{ if (e.Exception != null)
{
Response.Write( " <script> alert( ' " + "发生异常 " + " ') </script> "); //★该行没执行
} if (e.AffectedRows <= 0)
{
Response.Write( " <script> alert( ' " + "没有编辑任何行 " + " ') </script> "); //★该行竟被执行了(实际上编辑了绑定字段或模版列)
}
if (e.Exception != null ¦ ¦ e.AffectedRows <= 0)
{ lblMessage.Text = "在编辑数据时发生错误,请确认所输入之数据的格式是否正确。 ";
e.ExceptionHandled = true;
}
else
{
DetailsView myDetailsView = (DetailsView)(sender);
string Id = Convert.ToString(e.Keys[ "TypeID "]); FileUpload PhotoFile =
(FileUpload)(myDetailsView.Rows[myDetailsView.DataItemIndex].FindControl( "FileUpload1 "));
if (PhotoFile == null )
{
Response.Write( " <script> alert( ' " + "没有找到文件上传控件 " + " ') </script> "); //★该行没执行(找到了FileUpload1)
}
if (!PhotoFile.HasFile)
{
Response.Write( " <script> alert( ' " + "上传控件没有上传文件 " + " ') </script> "); //★该行被执行了(不知道为什么????)
}
SmallTypeLogic smallTypeLogic = new SmallTypeLogic();
bool result = smallTypeLogic.UpdateSmallTypePhotoByID(Id, PhotoFile);if (result)
{ lblMessage.Text = "已经成功编辑数据... ";
} this.UpdatePanel1.Update(); }
}
DetailsView dv = sender as DetailsView;
if (dv is DetailsView)
{
FileUpload fu = dv.FindControl("FileUpload1") as FileUpload;
if (fu is FileUpload)
{
//成功获取FileUpload控件的处理逻辑
string fileName = fu.FileName;
}
}
绑定了四个字段(TypdID,BigTypeID,TypeName,Explain)。另外还有一个唯一的模版列,
该模版列的编辑模版中有一个名为PhotoFileUpload1的文件上传控件。
我想实现,在点击DetailsView1的 "编辑 "命令按钮后,在编辑状态可以编辑模版列中的文件上传控件,然后点
击“更新”命令按钮将图片数据通过自定义方法更新到数据库中。****遇到的问题是不在于怎么更新,而是可以触发ItemUpdated事件,并且能够找到DetailsView中的FileUpload1控件,
但确总是认为它没有上传文件。而在页面放这么一个上传控件并用不着同样的代码上传文件就没有问题。
最重要的错误是竟然在编辑行后(无论是绑定字段,还是模版字段),影响行数为0。下面的我的代码:(有问题处已用//★标记标出)
(1)这是DetailsView的定义
<asp:DetailsView ID= "DetailsView1 " runat= "server " AutoGenerateRows= "False " CellPadding= "4 "
DataSourceID= "ObjectDataSource1 " ForeColor= "#333333 " GridLines= "None " Height= "50px "
Width= "219px "
OnItemUpdated= "DetailsView1_ItemUpdated "
OnItemCommand= "DetailsView1_ItemCommand "
DataKeyNames= "TypeID "
OnDataBound= "DetailsView1_DataBound ">
<FooterStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<CommandRowStyle BackColor= "#D1DDF1 " Font-Bold= "True " />
<EditRowStyle BackColor= "#2461BF " />
<RowStyle BackColor= "#EFF3FB " />
<PagerStyle BackColor= "#2461BF " ForeColor= "White " HorizontalAlign= "Center " />
<Fields>
<asp:BoundField DataField= "TypeID " HeaderText= "TypeID " SortExpression= "TypeID " />
<asp:BoundField DataField= "BigTypeID " HeaderText= "BigTypeID " SortExpression= "BigTypeID " />
<asp:BoundField DataField= "TypeName " HeaderText= "TypeName " SortExpression= "TypeName " />
<asp:BoundField DataField= "Explain " HeaderText= "Explain " SortExpression= "Explain " />
<asp:TemplateField>
<ItemTemplate></ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID= "FileUpload1 " runat= "server " />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton= "True " ShowInsertButton= "True ">
<ItemStyle Font-Bold= "True " ForeColor= "#C00000 " />
</asp:CommandField>
</Fields>
<FieldHeaderStyle BackColor= "#DEE8F5 " Font-Bold= "True " />
<HeaderStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<AlternatingRowStyle BackColor= "White " />
</asp:DetailsView>
(2)这是DetailsView1的ItemUpdated事件
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{ if (e.Exception != null)
{
Response.Write( " <script> alert( ' " + "发生异常 " + " ') </script> "); //★该行没执行
} if (e.AffectedRows <= 0)
{
Response.Write( " <script> alert( ' " + "没有编辑任何行 " + " ') </script> "); //★该行竟被执行了(实际上编辑了绑定字段或模版列)
}
if (e.Exception != null ¦ ¦ e.AffectedRows <= 0)
{ lblMessage.Text = "在编辑数据时发生错误,请确认所输入之数据的格式是否正确。 ";
e.ExceptionHandled = true;
}
else
{
DetailsView myDetailsView = (DetailsView)(sender);
string Id = Convert.ToString(e.Keys[ "TypeID "]); FileUpload PhotoFile =
(FileUpload)(myDetailsView.Rows[myDetailsView.DataItemIndex].FindControl( "FileUpload1 "));
if (PhotoFile == null )
{
Response.Write( " <script> alert( ' " + "没有找到文件上传控件 " + " ') </script> "); //★该行没执行(找到了FileUpload1)
}
if (!PhotoFile.HasFile)
{
Response.Write( " <script> alert( ' " + "上传控件没有上传文件 " + " ') </script> "); //★该行被执行了(不知道为什么????)
}
SmallTypeLogic smallTypeLogic = new SmallTypeLogic();
bool result = smallTypeLogic.UpdateSmallTypePhotoByID(Id, PhotoFile);if (result)
{ lblMessage.Text = "已经成功编辑数据... ";
} this.UpdatePanel1.Update(); }
}
这个地方是返回在Updating事件之后,执行ObjectDataSource中的Update方法中影响的行数,所以这里你应该检查Update方法是否正确。
关于后面FindControl的地方,我试了没有问题,我是这样找的:
DetailsView dv = sender as DetailsView;
if (dv is DetailsView)
{
FileUpload fu = dv.FindControl("FileUpload1") as FileUpload;
if (fu is FileUpload)
{
string fileName = fu.FileName;
}
}
HasFile为True.