在Formview中嵌套Gridview会中断插入代码隐藏上下文

时间:2015-09-23 21:35:12

标签: c# asp.net gridview sqldatasource

我在FormView页面中有几个GridView,我想在Gridview的FooterRow中创建一个Insert行。布局和一切都很好。但是,当我为Insert命令构建代码隐藏时,我遇到了上下文问题。如果我将GridView移动到FormView标记之外,则会立即清除上下文错误。

GridView标记

        <asp:GridView ID="gvBFMats" runat="server" ShowFooter="True" AutoGenerateColumns="False" DataKeyNames="MaterialID" DataSourceID="BFMatsSQL" OnRowCommand="gvBFMats_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="Commands" ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="ButtonUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="ButtonEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="ButtonDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="ButtonAdd" runat="server" CommandName="Insert" Text="Add to Table" />
                    </FooterTemplate>
...

插入命令代码隐藏

    protected void gvBFMats_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert" && Page.IsValid)
        {
            BFMatsSQL.Insert();
        }
    }

    protected void BFMatsSQL_Inserting
        (object sender, ObjectDataSourceMethodEventArgs e)
    {
        DropDownList ddlNewMfr =
            (DropDownList)gvBFMats.FooterRow.FindControl("ddlNewMfr");
        DropDownList ddlNewThickness =
            (DropDownList)gvBFMats.FooterRow.FindControl("ddlNewThickness");
        DropDownList ddlNewCore =
            (DropDownList)gvBFMats.FooterRow.FindControl("ddlNewCore");
        DropDownList ddlNewSize =
            (DropDownList)gvBFMats.FooterRow.FindControl("ddlNewSize");
        TextBox txtNewColor =
            (TextBox)gvBFMats.FooterRow.FindControl("txtNewColor");
        TextBox txtNewQty =
            (TextBox)gvBFMats.FooterRow.FindControl("txtNewQty");
        DropDownList ddlNewFinish =
            (DropDownList)gvBFMats.FooterRow.FindControl("ddlNewFinish");
        TextBox txtNewExtra =
            (TextBox)gvBFMats.FooterRow.FindControl("txtNewExtra");

        // Set the SQLDataSource's InsertParameters values
        e.InputParameters["MatManufacturerID"] =
            Convert.ToInt32(ddlNewMfr.SelectedValue);
        e.InputParameters["MatThicknessID"] =
            Convert.ToInt32(ddlNewThickness.SelectedValue);
        e.InputParameters["MatCoreID"] =
            Convert.ToInt32(ddlNewCore.SelectedValue);
        e.InputParameters["MatSizeID"] =
             Convert.ToInt32(ddlNewSize.SelectedValue);
        e.InputParameters["MatFinishPrePostID"] =
             Convert.ToInt32(ddlNewFinish.SelectedValue);

        string strNewColor = null;
        if (!string.IsNullOrEmpty(txtNewColor.Text))
            strNewColor = txtNewColor.Text;
        e.InputParameters["MatFinishColor"] = strNewColor;

        int? intNewQty = null;
        if (!string.IsNullOrEmpty(txtNewQty.Text))
            intNewQty = Convert.ToInt32(txtNewQty.Text);
        e.InputParameters["MatQty"] = intNewQty;

        string strNewExtra = null;
        if (!string.IsNullOrEmpty(txtNewExtra.Text))
            strNewExtra = txtNewExtra.Text;
        e.InputParameters["MatNumExtraSheets"] = strNewExtra;

    }

具体来说,我在(Control)gvBFMats.FooterRow.FindControl("Control ID");中的gvBFMats下得到了红色波浪形,其中说“当前上下文中不存在名称'gvBFMats'。”我只是猜测当它嵌套在FormView模板中时,它不喜欢对GridView的调用。有没有办法以编程方式传递这个上下文?

1 个答案:

答案 0 :(得分:1)

您是否正确认识到名称gvBFMats,因为它已嵌入模板中。只有&#34;顶级&#34;,非嵌入式控件将在Code Behind中被处理,就像它们已被明确声明一样。在模板中声明的控件不会。编译器不识别这些名称。

这是一个原因。想象一下,你在其中一个TextBox1中有一个名为ItemTemplates的控件用于重复控制。你绑定它。您网页的控制树现在有数十个ID为TextBox1的控件。如果您想参考TextBox1,它是如何知道您的意思?

因此。在你的情况下你能做什么?好吧,BFMatsSQL_InsertinggvBFMats_RowCommand都是事件处理程序,因此您无法更改其签名。

但是,您可以使用属于同一个类的它们,并使用模块级变量来保存对gvBFMats的引用。像这样:

private GridView gvBFMats;
protected void gvBFMats_RowCommand(object sender, GridViewCommandEventArgs e)
{
    gvBFMats = [your form view].Row.FindControl("gvBFMats") as GridView;
    if (e.CommandName == "Insert" && Page.IsValid)
    {  
        BFMatsSQL.Insert();
    }
}

现在,BFMatsSQL_Inserting将能够引用gvBFMats,它应该有一个值。

相关问题