使用第二个 UpdatePanel 的 gridview 中的 LinkBut​​ton 从第一个 UpdatePanel 更新文本框。找不到控件

Update textbox from 1st UpdatePanel using LinkButton from 2nd UpdatePanel's gridview. Control could not be found

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

A control with ID 'btnAddRow' could not be found for the trigger in 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>

您唯一需要做的就是在单击 btnAddRow 时触发 upSectionB 的更新。

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

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

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

LinkButton lb = gvCostBreakdown.FooterRow.FindControl("btnAddRow") as LinkButton;

我有同样的问题(出现此错误的原因是您的 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 (upSectionB) 中删除 UpdateMode="Conditional" ChildrenAsTriggers="true",将 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;
}

请参阅下面的完整示例:

Code-Behind:

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 图片演示: