【问题标题】:Nesting Gridview inside Formview breaks insert codebehind context在 Formview 中嵌套 Gridview 会中断插入代码隐藏上下文
【发布时间】:2015-12-21 09:09:50
【问题描述】:

我的 FormView 页面中有几个 GridView,我想在 Gridview 的 FooterRow 中创建一个插入行。布局和一切都很好。但是,当我为 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 的调用。有没有办法以编程方式传递这个上下文?

【问题讨论】:

    标签: c# asp.net gridview sqldatasource


    【解决方案1】:

    您说得对,因为它嵌入在模板中,所以无法识别名称 gvBFMats。只有“顶级”、非嵌入式控件将在代码隐藏中被视为已被显式声明。模板中声明的控件不会。编译器无法识别这些名称。

    这是有原因的。假设您在 ItemTemplates 之一中有一个名为 TextBox1 的控件,用于重复控件。你绑定它。您页面的控件树现在有几十个 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,它应该有一个值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2012-08-11
      相关资源
      最近更新 更多