gridview 的update参数问题
gridview选择一行进行更新,中间没有报错,但数据却没有被更新.
实在找不出原因,只好将绑定列全部改成了模板列,然后在RowUpdating事件中给参数赋值.相应的textbox中已经写入的新的值,但findcontrol时得到的仍然是原来绑定的值,
郁闷死了,向高手救助
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " Width= "800px " BorderStyle= "Solid " BorderWidth= "1px " DataSourceID= "SqlDataSource1 " DataKeyNames= "ID " AllowSorting= "True " OnRowUpdated= "GridView1_RowUpdated " OnRowUpdating= "GridView1_RowUpdating ">
<Columns>
<asp:TemplateField HeaderText= "Result ">
<ItemTemplate> <asp:Label runat= "server " ID= "lbl " Text= ' <%#Bind( "Result ")%> '> </asp:Label> </ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID= "dropResultEidt " runat= "server ">
<asp:ListItem Text= "Pass "> </asp:ListItem>
<asp:ListItem Text= "Fail "> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "Remark " SortExpression= "Remark ">
<ItemTemplate>
<asp:Label ID= "Label3 " runat= "server " Text= ' <%#Bind( "Remark ")%> ' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID= "txtEditRemark " Width= "100px " runat= "server " Text= ' <%#Bind( "Remark ")%> ' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "Location " SortExpression= "Location ">
<ItemTemplate>
<asp:Label ID= "Label3 " runat= "server " Text= ' <%#Bind( "Location ")%> ' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID= "txtEditLocation " Width= "100px " runat= "server " Text= ' <%#Bind( "Location ")%> ' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "DateCode " SortExpression= "DateCode ">
<ItemTemplate>
<asp:Label ID= "Label3 " runat= "server " Text= ' <%#Bind( "DateCode ")%> ' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID= "txtEditDateCode " Width= "100px " runat= "server " Text= ' <%#Bind( "DateCode ")%> ' />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField= "InputTime " DataFormatString= "{0:yy-MM-dd HH:mm:ss} "
HeaderText= "Input Time " SortExpression= "InputTime " HtmlEncode= "False " ReadOnly= "True " />
<asp:CommandField ShowEditButton= "true " ShowDeleteButton= "true " />
</Columns>
</asp:GridView>
//datasouce
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= " <%$ ConnectionStrings:TPMConnectionString %> "
DeleteCommand= "sp_Delete_Repair_Info " DeleteCommandType= "StoredProcedure " SelectCommand= "SELECT * FROM [tb_Repair_Info] "
UpdateCommand= "sp_Update_Repair_Info " UpdateCommandType= "StoredProcedure " on>
<DeleteParameters>
<asp:ControlParameter ControlID= "GridView1 " Name= "ID " PropertyName= "SelectedDataKey "
Type= "Int32 " />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name= "ID " Type= "Int32 " />
<asp:Parameter Name= "Result " Type= "String " />
<asp:Parameter Name= "Remark " Type= "String " />
<asp:Parameter Name= "Location " Type= "String " />
<asp:Parameter Name= "DateCode " Type= "String " />
</UpdateParameters>
</asp:SqlDataSource>
//updating事件
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow r = GridView1.Rows[e.RowIndex];
SqlDataSource1.UpdateParameters[ "ID "].DefaultValue = e.Keys[0].ToString();
SqlDataSource1.UpdateParameters[ "Result "].DefaultValue = ((DropDownList)r.FindControl( "dropResult ")).Text.Trim();
SqlDataSource1.UpdateParameters[ "Remark "].DefaultValue = ((TextBox)GridView1.SelectedRow.FindControl( "txtEditRemark ")).Text.Trim();
SqlDataSource1.UpdateParameters[ "Location "].DefaultValue = ((TextBox)r.FindControl( "txtEditLocation ")).Text.Trim();
SqlDataSource1.UpdateParameters[ "DateCode "].DefaultValue = ((TextBox)r.FindControl( "txtEditDateCode ")).Text.Trim();
}
[解决办法]
既然你使用 xxxDataSource ,应该不存在 !IsPostBack 的问题,他会自动维护
比较奇怪
[解决办法]
在 RowUpdated 事件中,检查下 AffectedRows ,看是否真的执行了更新
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
Response.Write( e.AffectedRows );
}
[解决办法]
页面不是很复杂的话,建议重新写一遍,不要拷贝原来的代码,可能是某个细节被忽略了。