我有一个带有数据源的gridview。由于某种原因,当我在数据源上使用更新按钮时,它没有更新。我没有收到 sql 错误,直接使用时查询工作正常。
<asp:GridView ID="viewStoryTime" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource10" DataKeyNames="NonScrumStoryId, PK_DailyTaskHours" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
Width="525px" OnRowEditing="viewStoryTime_OnRowEditing" OnRowCancelingEdit="viewStoryTime_OnRowCancelingEdit" OnRowUpdating="viewStoryTime_OnRowUpdating" OnRowUpdated="viewStoryTime_OnRowUpdated" >
<Columns>
<asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
<asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
<asp:BoundField DataField="ActivityDate" HeaderText="Date" SortExpression="ActivityDate" DataFormatString="{0:MM/dd/yyyy}" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource10" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [DailyTaskHours].[PK_DailyTaskHours], [DailyTaskHours].[NonScrumStoryId], [DailyTaskHours].[Hours], [DailyTaskHours].[Notes], [DailyTaskHours].[ActivityDate] FROM [NonScrumStory], [DailyTaskHours] WHERE [DailyTaskHours].[NonScrumStoryId] = @nonScrumStoryId AND [NonScrumStory].[PK_NonScrumStory] = @nonScrumStoryId"
UpdateCommand="UPDATE [DailyTaskHours] SET [Hours] = @setEditHoursParam, [ActivityDate] = @setEditActivityDateParam, [Notes] = @setEditNotesParam WHERE [PK_DailyTaskHours] = @setDailyPKParam">
<SelectParameters>
<asp:QueryStringParameter Name="nonScrumStoryId" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:QueryStringParameter Name="setEditHoursParam" Type="String" />
<asp:QueryStringParameter Name="setEditActivityDateParam" Type="String" />
<asp:QueryStringParameter Name="setEditNotesParam" Type="String" />
<asp:QueryStringParameter Name="setDailyPKParam" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
这是应用参数的c#:
protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource10.UpdateParameters["setDailyPKParam"].DefaultValue = viewStoryTime.DataKeys[e.NewEditIndex].Values["PK_DailyTaskHours"].ToString();
System.Diagnostics.Debug.WriteLine(viewStoryTime.DataKeys[e.NewEditIndex].Values["PK_DailyTaskHours"].ToString());
}
protected void viewStoryTime_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = e.NewValues[0].ToString();
SqlDataSource10.UpdateParameters["setEditActivityDateParam"].DefaultValue = e.NewValues[2].ToString();
SqlDataSource10.UpdateParameters["setEditNotesParam"].DefaultValue = e.NewValues[1].ToString();
System.Diagnostics.Debug.WriteLine(e.NewValues[0].ToString());
System.Diagnostics.Debug.WriteLine(e.NewValues[2].ToString());
System.Diagnostics.Debug.WriteLine(e.NewValues[1].ToString());
SqlDataSource10.Update();
SqlDataSource10.DataBind();
}
请注意,Debug.WriteLine() 是这样我可以看到参数的输出,这是一个示例输出:
调试输出:4911
调试输出:5.5 7/9/2013 12:00:00 AM 更改文本
当我按下更新时: