使用第二个UpdatePanel的gridview中的LinkBut​​ton从第一个UpdatePanel更新文本框。无法找到控制权

时间:2018-04-02 04:11:05

标签: c# asp.net gridview triggers updatepanel

我需要在UpdatePanel btnAddRow中添加LinkBut​​ton upSectionB,但问题是我在加载过程中遇到此错误:

  

无法为触发器找到ID为“btnAddRow”的控件   UpdatePanel'upSectionB'。

我简化的aspx

<asp:UpdatePanel ID="upSectionB" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>
        Request Budget (USD)
        <asp:TextBox ID="txtTotal" runat="server"></asp:TextBox>

    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnAddRow" EventName="Click" />
        //Poses problems when I uncomment the trigger above. btnAddRow is a LinkButton inside upSectionC
    </Triggers>
</asp:UpdatePanel>

<asp:UpdatePanel ID="upSectionC" runat="server">
    <ContentTemplate>
        <asp:GridView ID="gvCostBreakdown" runat="server" AutoGenerateColumns="false" ShowFooter="true">
            <Columns>
                <asp:TemplateField HeaderText="Amount">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAmount" runat="server" Text='<%# Eval("BudgetUSD") %>'></asp:TextBox>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAmountTotal" runat="server" Enabled="false"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:LinkButton ID="btnDeleteRow" runat="server" ToolTip="Delete" OnClick="btnDeleteRow_Click" CommandArgument='<%# Eval("Id","{0}") %>' OnClientClick="">Delete</asp:LinkButton>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="btnAddRow" runat="server" CausesValidation="true" ValidationGroup="vgAddRow" ToolTip="Add Row" OnClick="btnAddRow_Click">Add Row</asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

2 个答案:

答案 0 :(得分:2)

您需要做的唯一事情是在点击SparkException: Job aborted due to stage failure: Task not serializable: java.io.NotSerializableException: org.apache.spark.sql.DataFrameReader 时触发upSectionB的更新。

btnAddRow

您收到该错误的原因是因为该按钮位于GridView控件中,因此无法在aspx页面的范围内访问。如果您想对页脚中的按钮执行某些操作,则需要使用FindControl。

protected void btnAddRow_Click(object sender, EventArgs e)
{
    txtTotal.Text = "new value";

    //update the other updatepanel
    upSectionB.Update();
}

答案 1 :(得分:1)

我遇到了同样的问题(因为您的LinkBut​​ton在GridView内部而不会在GridView外部触发时发生此错误的原因)并按照以下步骤修复它:

第1步:在GridView中添加RowCommand事件:

<asp:GridView ID="gvCostBreakdown" OnRowCommand="gvCostBreakdown_RowCommand" ... 

第2步:在GridView中的LinkBut​​ton中添加CommandName属性:

<asp:LinkButton ID="btnAddRow" CommandName="AddRow" ...

第3步:删除第一个UpdatePanel中的UpdateMode="Conditional" ChildrenAsTriggers="true"upSectionB),将ControlID设为gvCostBreakdown,将EventName设为RowCommand <Triggers> }}:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="gvCostBreakdown" EventName="RowCommand" />
</Triggers>

第4步:最后,在RowCommand事件中,从GridView行设置TextBox值:

// Getting GridView row which clicked
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

// Check if LinkButton (AddRow) is clicked
if (e.CommandName=="AddRow")
{
    // getting the value of label inside GridView
    string rowCellValue = ((Label)row.FindControl("Label1")).Text;

    // setting value to TextBox which is inside First UpdatePanel
    txtTotal.Text = rowCellValue;
}

请参阅下面的完整示例:

<强>代码隐藏:

DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
    // adding dummy data into DataTable
    if (dt.Rows.Count == 0)
    {
        dt.Columns.Add("Column1");
        dt.Rows.Add("Row1");
        dt.Rows.Add("Row2");
    }

    if (!IsPostBack)
    {
        // setting and binding DataTable to GridView
        gvCostBreakdown.DataSource = dt;
        gvCostBreakdown.DataBind();
    }
}

protected void gvCostBreakdown_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // Getting GridView row which clicked
    GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

    // Check if LinkButton (AddRow) is clicked
    if (e.CommandName=="AddRow")
    {
        // getting the value of label inside GridView
        string rowCellValue = ((Label)row.FindControl("Label1")).Text;

        // setting value to TextBox which is inside First UpdatePanel
        txtTotal.Text = rowCellValue;
    }
}

.Aspx代码:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="upSectionB" runat="server">
    <ContentTemplate>
        TextBox:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="gvCostBreakdown" EventName="RowCommand" />
    </Triggers>
</asp:UpdatePanel>

<asp:UpdatePanel ID="upSectionC" runat="server">
    <ContentTemplate>
        GridView: </b>
        <asp:GridView ID="gvCostBreakdown" runat="server" OnRowCommand="gvCostBreakdown_RowCommand" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Column1">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Column1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Column2">
                    <ItemTemplate>
                        <asp:LinkButton ID="btnAddRow" CommandName="AddRow" runat="server">Add Row</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

.GIF图片演示:

enter image description here