別のグリッドビュー内にネストされたグリッドビューがあります。親グリッドビューにはタスクのリストがあり、各タスクにはステップのリスト(子グリッドビュー)があります。各ステップにはチェックボックスがあるため、ユーザーがステップを完了すると、チェックボックスがオンになり、CheckChangedイベントが発生します。このイベントでは、データベースを更新するストアドプロシージャがあり、チェックされたステップが完了として登録されます。
チェックボックスがオンになっている行のステップIDを検索し、ステップIDを入力パラメーターとして使用してストアドプロシージャを起動するCheckChangedイベントがあります。これは機能します。ただし、ステップを追加することはできます。何らかの理由で、前のステップのチェックボックスをクリックすると、ステップIDを検索するコードが逆戻りして、ステップIDを認識しません。つまり、特定のタスクのステップIDが20で、チェックされてイベントが発生した場合、前のタスク(グリッドビューの上位にリストされているタスク)にステップを追加すると、そのステップはIDが21の場合、それをクリックすると、CheckChangedイベントが別のタスクに追加されたステップを認識しないかのように、ステップIDが常に20として登録されます。これが私のコードです:
aspx:
<asp:GridView ID="GridView1"
DataKeyNames="TaskID"
runat="server"
OnRowDataBound="GridView1_OnRowDataBound"
CssClass="DefaultGrid"
onRowCommand="GridView1_RowCommand"
EmptyDataText = "<br/>There are no Tasks in this Project."
AllowPaging="True"
AllowSorting="True"
CellPadding="4"
DataSourceID="SqlDataSource_mp_Display_Tasks_Home"
ForeColor="#333333"
GridLines="None"
AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<RowStyle CssClass=" table-responsive body-content " />
<Columns>
<asp:CommandField ShowSelectButton="False" />
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="Images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display:none">
<asp:GridView ID="GridView2"
runat="server"
onRowCommand="GridView2_RowCommand"
Datakeynames="TaskStepID"
CssClass="ChildGrid"
EmptyDataText="<br/>There are no Steps in this Task."
AutoGenerateColumns="false"
onselectedindexchanged="GridView2_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="TaskStepID" HeaderText="TaskStepID" Visible="true" />
<asp:BoundField DataField="TaskID" HeaderText="TaskID" Visible="false" />
<asp:BoundField DataField="TaskStepTypeID" HeaderText="TaskStepTypeID" Visible="false"/>
<asp:BoundField DataField="TaskStepPriority" HeaderText="Task Step Priority" Visible="false" />
<asp:BoundField ItemStyle-Width="70%" DataField="TaskStepDesc" HeaderText="Step Description" />
<asp:TemplateField HeaderText="Step Completed" ItemStyle-Width="15%" >
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Checkbox ID="TaskStepCompleted"
OnCheckedChanged="TaskStepCompleted_CheckedChanged"
Checked='<%# Eval("TaskStepCompleted") %>'
runat="server"
AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TaskStepComment" HeaderText="TaskStepComment" Visible="false" />
<asp:TemplateField HeaderText="Step Activity" ItemStyle-Width="15%">
<ItemTemplate>
<asp:LinkButton Text="Add" ID="Addstep" runat="server" CommandName="addstep" CommandArgument='<%# Eval("TaskStepID") %>'/>
<asp:LinkButton Text="Edit" ID="Editstep" runat="server" CommandName="editstep" CommandArgument='<%# Eval("TaskStepID") %>'/>
<asp:LinkButton Text="Delete" ID="Deletestep" runat="server" CommandName="deletestep" OnClientClick="return confirm('<%=AlertMe%>');" CommandArgument='<%# Eval("TaskStepID") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TaskID" HeaderText="Task ID" Visible="false"/>
<asp:BoundField DataField="ProjectID" HeaderText="Project ID" Visible="false" />
<asp:BoundField DataField="TaskTypeID" HeaderText="Task Type ID" Visible="false" />
<asp:BoundField DataField="TaskCompleted" HeaderText="Taske Completed?" Visible="false" />
<asp:BoundField DataField="TaskCreationDate" HeaderText="Task Creation Date" Visible="false" />
<asp:BoundField DataField="TaskSubmitterID" HeaderText="Task Submitter ID" Visible="false" />
<asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" Visible="false" />
<asp:BoundField DataField="TaskDueDateCommentType" HeaderText="Due Date Comment" Visible="false" />
<asp:BoundField DataField="TaskLastUpdatedDate" HeaderText="Task Last Updated Date" Visible="false" />
<asp:BoundField DataField="TaskLastUpdatedUserID" HeaderText="Task Last Updated UserID" Visible="false" />
<asp:BoundField DataField ="TaskSubmitterName" HeaderText="Task Submitter Name" Visible="false" />
<asp:BoundField DataField="TaskLastUpdatedUser" HeaderText="Task Last Updated User" Visible="false" />
<asp:BoundField DataField="DepartmentDesc" HeaderText="Dept" visible="false"/>
<asp:TemplateField HeaderText="Task Description">
<ItemTemplate>
<asp:Label ID="TaskDescription" runat="server" Text='<%# HighlightText(Eval("TaskDescription").ToString()) %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="40%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="% Comp.">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="PercentCompleted" runat="server" Text='<%# (String.IsNullOrEmpty(Eval("PercentCompleted").ToString()) ? "0" : Eval("PercentCompleted")) + " %" %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TaskDueDate" HeaderStyle-HorizontalAlign="Right" HeaderText="Task Due Date" DataFormatString="{0:MM-dd-yyyy}" visible="true"/>
<asp:BoundField DataField="TaskTypeName" HeaderText="Task Type" visible="true" />
<asp:BoundField DataField="DepartmentAbbrev" HeaderText="Dept" Visible="true" />
<asp:TemplateField HeaderText="Activity">
<ItemTemplate>
<asp:LinkButton Text="Edit" ID="Edittask" runat="server" CommandName="edittask" CommandArgument='<%# Eval("TaskID") %>'/>
<asp:LinkButton Text="Delete" ID="Deletetask" runat="server" CommandName="deletetask" OnClientClick="return confirm('Are you sure you wish to Delete this Task?');" CommandArgument='<%# Eval("TaskID") %>'/>
<asp:LinkButton Text="Comments" ID="Detailstask" runat="server" CommandName="detailstask" CommandArgument='<%# Eval("TaskID") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource_mp_Display_Tasks_Home" runat="server" FilterExpression="TaskDescription LIKE '%{0}%'" ConnectionString="<%$ ConnectionStrings:IntelliBaseConnectionString_mp_Display_Projects_Home %>" SelectCommand="mp_Display_Tasks_Home" SelectCommandType="StoredProcedure" ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:Parameter Name="Incoming_ProjectID" Type="Int32" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="TaskDescription" ControlID="txtSearchtasks" PropertyName="Text"/>
</FilterParameters>
</asp:SqlDataSource>
cs:
protected void TaskStepCompleted_CheckedChanged(object sender, EventArgs e)
{
// this nested foreach grabs the taskstepID number for the row in which the checkbox was just checked.
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
GridView GridView2 = (GridView)row.FindControl("GridView2");
if (GridView2 != null)
{
foreach (GridViewRow Row in GridView2.Rows)
{
if (Row.RowType == DataControlRowType.DataRow)
{
System.Web.UI.WebControls.CheckBox chk = (System.Web.UI.WebControls.CheckBox)Row.FindControl("TaskStepCompleted");
if (chk.Checked || !chk.Checked)
{
Session["TaskStepID"] = GridView2.DataKeys[Row.RowIndex]["TaskStepID"].ToString();
}
}
}
}
}
}
// autopost back the check box to run the stored procedure
var TaskStepID = Session["TaskStepID"].ToString();
var ProjectID = Session["ProjectID"].ToString();
using (var conn = new SqlConnection("Data Source=orlandosql1;Initial Catalog=IntelliBase;Persist Security Info=True;User ID=trainingsurveys_webuser;Password=C@mb3rSQL;"))
using (SqlCommand cmd = new SqlCommand("dbo.mp_Task_Step_Completed_Toggle", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@TaskStepID", SqlDbType.Int).Value = TaskStepID;
cmd.Parameters.Add("@AdminUserID", SqlDbType.Int).Value = "10";
cmd.Parameters.Add("@LogActionItemID", SqlDbType.Int).Value = "15";
cmd.Parameters.AddWithValue("@DTStamp", SqlDbType.DateTime.ToString("d")).Value = DateTime.Now.ToString("d");
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("Tasks.aspx?id=" + ProjectID.ToString());
}
}
私はその問題を説明するために最善を尽くしました。要約すると、ネストされたグリッドビューにはチェックボックスのある行があります。チェックボックスをクリックすると、データベーステーブルが自動的に更新されますが、チェックボックスの行に関係なく、CheckedChangedコードは最新のIDのIDのみを登録します。どんな洞察も大歓迎です。
カルロス
私が正しく理解していれば、あなたの問題はTaskStepID
、GridViewRowをどのように取得していると思うかにあると思います。あなたは多くの不必要な仕事をしています。両方のGridViewのすべての行を繰り返す代わりに、クリックしたばかりのCheckBoxのGridViewRowを取得するだけです。
CheckBox cb = (CheckBox)sender;
GridViewRow row = (GridViewRow)cb.NamingContainer;
GridView gv = (GridView)row.NamingContainer;
string taskStepID = gv.DataKeys[row.RowIndex].Value.ToString();
あなたがそうであったように各行を繰り返すことによって、あなたは常にTaskStepID
最後の行のを取得していたことに注意してください。したがって、その前に行を追加すると、予期した行ではなく最後の行が使用されていました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加