在C#代码隐藏中获取InsertParameter的QueryStringParameter值

时间:2016-06-03 20:19:02

标签: c# asp.net gridview

我正在处理ASP.NET Web窗体中的gridview,它使用QueryStringParameter作为其Select Parameter。因为我不得不在代码隐藏中为GridView执行插入操作,所以我试图找到一个示例,说明如何为我的SQLDataSource.InsertParameters.Add()块获取QueryStringParameter的值,我将空白。< / p>

SQL DataSource:

<asp:SqlDataSource ID="gvProjectTaskSQL" runat="server" ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>" 
            SelectCommand="SELECT tt.TaskID,
                tt.EmployeeID,
                tt.ProjectID,
                tt.ReleaseID,
                tt.TaskStatusID,
                tt.TaskPriorityID,
                tt.DueDate,
                tt.Notes,
                tt.CompDate,
                p.ProjectName,
                tc.TaskCodeAbbr 
                FROM tblTaskTracker tt INNER JOIN
                    tblProject p ON tt.ProjectID = p.ProjectID INNER JOIN
                    tblTaskCode tc ON tt.TaskCodeID = tc.TaskCodeID INNER JOIN
                    tblTaskStatus ts on tt.TaskStatusID = ts.TaskStatusID
                WHERE tt.ProjectID = @ProjectID AND ts.TaskStatusType <> 'Z' 
                ORDER BY ts.TaskStatusType, tt.TaskPriorityID, tt.DueDate"
            InsertCommand="INSERT INTO [tblTaskTracker] 
                ([EmployeeID], [ProjectID], [ReleaseID], [TaskCodeID], [TaskStatusID], [TaskPriorityID], [DueDate], [Notes], [CompDate])
                VALUES (@EmployeeID, @ProjectID, @ReleaseID, @TaskCodeID, @TaskStatusID, @TaskPriorityID, @DueDate, @Notes, @CompDate)"
            UpdateCommand="UPDATE [tblTaskTracker] SET  [EmployeeID] = @EmployeeID, [TaskStatusID] = @TaskStatusID, [TaskPriorityID] = @TaskPriorityID, 
                            [DueDate] = @DueDate, [Notes] = @Notes, [CompDate] = @CompDate WHERE [TaskID] = @TaskID"
            DeleteCommand="DELETE FROM [tblTaskTracker] WHERE [TaskID] = @TaskID">

            <SelectParameters>
                <asp:QueryStringParameter Name="ProjectID" Type="Int32" QueryStringField="PID" />
            </SelectParameters>
            <InsertParameters>
                <asp:Parameter Name="EmployeeID" Type="Int32" />
                <asp:Parameter Name="ProjectID" Type="Int32" />
                <asp:Parameter Name="ReleaseID" Type="Int32" />
                <asp:Parameter Name="TaskCodeID" Type="Int32" />
                <asp:Parameter Name="TaskStatusID" Type="Int32" />
                <asp:Parameter Name="TaskPriorityID" Type="Int32" />
                <asp:Parameter Name="DueDate" Type="DateTime" />
                <asp:Parameter Name="Notes" Type="String" />
                <asp:Parameter Name="CompDate" Type="DateTime" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="EmployeeID" Type="Int32" />
                <asp:Parameter Name="TaskID" Type="Int32" />
                <asp:Parameter Name="TaskStatusID" Type="Int32" />
                <asp:Parameter Name="TaskPriorityID" Type="Int32" />
                <asp:Parameter Name="DueDate" Type="DateTime" />
                <asp:Parameter Name="Notes" Type="String" />
                <asp:Parameter Name="CompDate" Type="DateTime" />
            </UpdateParameters>
            <DeleteParameters>
                <asp:Parameter Name="TaskID" Type="Int32" />
            </DeleteParameters>
        </asp:SqlDataSource>

OnRowCommand的Codebehind:

protected void gvProjectTask_RowCommand(object sender, GridViewCommandEventArgs e)
{

    if (e.CommandName == "FooterInsert" && Page.IsValid)
    {

        GridView gv = (GridView)MultiView1.Views[10].FindControl("gvProjectTask");
        GridViewRow gvr = (GridViewRow)gv.FooterRow;
        if (gvr == null) { return; }

        // Get Controls
        DropDownList ddlEmployee = gvr.FindControl("ddlNewEmployee") as DropDownList;
        TextBox txtReleaseID = gvr.FindControl("txtNewReleaseID") as TextBox;
        DropDownList ddlTaskCode = gvr.FindControl("ddlNewTaskCode") as DropDownList;
        DropDownList ddlStatus = gvr.FindControl("ddlNewStatus") as DropDownList;
        DropDownList ddlPriority = gvr.FindControl("ddlNewPriority") as DropDownList;
        TextBox txtDueDate = gvr.FindControl("txtNewDueDate") as TextBox;
        TextBox txtNotes = gvr.FindControl("txtNewNotes") as TextBox;
        TextBox txtCompDate = gvr.FindControl("txtNewCompDate") as TextBox;

        // Test for nulls
        if (string.IsNullOrWhiteSpace(txtDueDate.Text))
        {
            // Throw error message
            ClientScript.RegisterStartupScript(GetType(), "error", "alert('Enter Due Date.');", true);
        }

        else
        {
            // Initialize Insert Parameters
            gvProjectTaskSQL.InsertParameters.Clear();
            gvProjectTaskSQL.InsertParameters.Add("EmployeeID", ddlEmployee.SelectedValue);
            // gvProjectTaskSQL.InsertParameters.Add("ProjectID", gvProjectTaskSQL.SelectParameters.ToString());
            gvProjectTaskSQL.InsertParameters.Add("ReleaseID", txtReleaseID.Text);
            gvProjectTaskSQL.InsertParameters.Add("TaskCodeID", ddlTaskCode.SelectedValue);
            gvProjectTaskSQL.InsertParameters.Add("TaskStatusID", ddlStatus.SelectedValue);
            gvProjectTaskSQL.InsertParameters.Add("TaskPriorityID", ddlPriority.SelectedValue);
            gvProjectTaskSQL.InsertParameters.Add("DueDate", txtDueDate.Text);
            gvProjectTaskSQL.InsertParameters.Add("Notes", txtNotes.Text);
            gvProjectTaskSQL.InsertParameters.Add("CompDate", txtCompDate.Text);
            gvProjectTaskSQL.Insert();

        }
    } // end FooterInsert

    if (e.CommandName == "EmptyInsert" && Page.IsValid)
    {
GridView gv = (GridView)MultiView1.Views[10].FindControl("gvProjectTask");        
GridViewRow gvr = (GridViewRow)gv.Controls[0].Controls[0];
        if (gvr == null) { return; }

        // Get Controls
        DropDownList ddlEmployee = (DropDownList)gvr.FindControl("ddlEmptyEmployee");
        TextBox txtReleaseID = (TextBox)gvr.FindControl("txtEmptyReleaseID");
        DropDownList ddlTaskCode = (DropDownList)gvr.FindControl("ddlEmptyTaskCode");
        DropDownList ddlStatus = (DropDownList)gvr.FindControl("ddlEmptyStatus");
        DropDownList ddlPriority = (DropDownList)gvr.FindControl("ddlEmptyPriority");
        TextBox txtDueDate = (TextBox)gvr.FindControl("txtEmptyDueDate");
        TextBox txtNotes = (TextBox)gvr.FindControl("txtEmptyNotes");
        TextBox txtCompDate = (TextBox)gvr.FindControl("txtEmptyCompDate");

        // Test for nulls
        if (string.IsNullOrWhiteSpace(txtDueDate.Text))
        {
            // Throw error message
            ClientScript.RegisterStartupScript(GetType(), "error", "alert('Enter Due Date.');", true);
        }

        else
        {
            // Initialize Insert Parameters
            gvProjectTaskSQL.InsertParameters.Clear();
            gvProjectTaskSQL.InsertParameters.Add("EmployeeID", ddlEmployee.SelectedValue);
            // gvProjectTaskSQL.InsertParameters.Add("ProjectID", gvProjectTaskSQL.SelectParameters.ToString());
            gvProjectTaskSQL.InsertParameters.Add("ReleaseID", txtReleaseID.Text);
            gvProjectTaskSQL.InsertParameters.Add("TaskCodeID", ddlTaskCode.SelectedValue);
            gvProjectTaskSQL.InsertParameters.Add("TaskStatusID", ddlStatus.SelectedValue);
            gvProjectTaskSQL.InsertParameters.Add("TaskPriorityID", ddlPriority.SelectedValue);
            gvProjectTaskSQL.InsertParameters.Add("DueDate", txtDueDate.Text);
            gvProjectTaskSQL.InsertParameters.Add("Notes", txtNotes.Text);
            gvProjectTaskSQL.InsertParameters.Add("CompDate", txtCompDate.Text);
            gvProjectTaskSQL.Insert();
        }
    } // end EmptyInsert 

} // end gvTask_Command

我已经注释掉了ProjectID的InsertParameter,因为我确定它是不正确的。我找不到任何如何将QueryString值传递给InsertParameter的示例。

1 个答案:

答案 0 :(得分:1)

只需将此代码添加到OnRowCommand事件中即可获取查询字符串值:

string projectID = Request.QueryString["PID"];