【问题标题】:PostBack from a link in Grid within UpdatePanel从 UpdatePanel 中 Grid 中的链接回发
【发布时间】:2014-05-20 13:21:49
【问题描述】:

我在 UpdatePanel 中有一个网格和几个文本框。在选择特定行时,使用 JS 填充文本框。 UpdatePanel 是在使用代码中的一种方法填充一个文本框时引入的。 网格行中还有一个链接,它会打开一个弹出窗口(也是从后面的代码中填充的)。

使用 UpdatePanel 后链接失效(假设因为没有完整的回帖)。

我尝试使用 UpdateMode="Conditional" 属性并为 PostBAck 控件注册 Link 控件。 但它仍然无法正常工作:

代码

<asp:UpdatePanel ID="Update" runat="server" Up>
        <ContentTemplate>
            <table width="100%">
                <tr>
                    <td>
                        <asp:GridView ID="gvSession" BorderColor="#ffcc00" RowStyle-BorderColor="#ffcc00"
                            AutoGenerateColumns="False" Font-Names="Verdana" DataKeyNames="SessionId" runat="server"
                            RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px" GridLines="Both" Width="100%"
                            OnRowCreated="gvSession_RowCreated" OnDataBound="gvSession_DataBound">
                            <RowStyle CssClass="dbGrid_Table_row" />
                            <HeaderStyle CssClass="dbGrid_Table_Header" />
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:RadioButton ID="rbSelect" onclick="javascript:CheckOtherIsCheckedByGVIDMore(this);"
                                            runat="server" OnCheckedChanged="rbSelect_CheckedChanged" AutoPostBack="True" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Session Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lblSessionName" Text='<%# Eval("SessionName") %>' runat="server"></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Left" Width="16%"></ItemStyle>
                                </asp:TemplateField>
                                                                   <asp:TemplateField HeaderText="Preview">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lbPreview" Text="Preview" CommandName="Preview" AutoPostBack="true"
                                         CommandArgument='<%# Eval("SessionId")  + ";" +  Eval("TrainingTypeName") %>'
                                            runat="server" OnClick="lbPreview_Click"></asp:LinkButton>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
                                </asp:TemplateField>
                               </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
            <table id="Table2" runat="server" width="100%">
                <tr>
                    <td colspan="2" align="center" style="text-align: center">
                        <asp:TextBox ID="txtSessionDtl" runat="server" Font-Size="Small" Style="text-align: center"
                            ForeColor="Black" Wrap="true" Height="20px" Width="95%" BorderStyle="None"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="width: 12%">
                        <asp:Label ID="Label9" CssClass="label" runat="server">Upload File : </asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtFilePath" Enabled="false" BorderStyle="Solid" BorderColor="Black"
                            runat="server" Width="741px"></asp:TextBox>
                    </td>
                </tr>
            </table>
                       </ContentTemplate>
    </asp:UpdatePanel>

后面的代码:

对于链接点击:

protected void lbPreview_Click(object sender, EventArgs e)
    {
        try
        {
            LinkButton link = (LinkButton)sender;
            GridViewRow gv = (GridViewRow)(link.Parent.Parent);
            LinkButton linkPreview = (LinkButton)gv.FindControl("lbPreview");
            string[] arg = new string[2];
            arg = linkPreview.CommandArgument.ToString().Split(';');
            string strSessionId = arg[0];
            string strTrgType = arg[1];
            string mailContent = GenerateNominationMailer(strSessionId, strTrgType);
            hidMailContent.Value = mailContent;
            string mailContent1 = mailContent.Replace(System.Environment.NewLine, "");

            string myScript = "<SCRIPT LANGUAGE='javascript'>";
            myScript = myScript + " my_window = window.open('', '', 'status=1,width=1000,height=800,scrollbars=yes');";
            myScript = myScript + " my_window.document.write(\"" + mailContent1.Replace("\"", "'") + "\");";
            myScript = myScript + " my_window.document.close();";
            myScript = myScript + " </script>";
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Nomination Mailer", myScript,false);
        }
        catch (Exception ex)
        {
            AlnErrorHandler.HandleError(ex);

        }
    }

注册控制:

 protected void gvSession_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow grdrow in gvSession.Rows)
        {
            LinkButton Preview = (LinkButton)grdrow.FindControl("lbPreview");
            ScriptManager current = ScriptManager.GetCurrent(Page);
            if (current != null)
                current.RegisterPostBackControl(Preview);
        }
    }
enter code here

【问题讨论】:

    标签: c# javascript asp.net updatepanel postback


    【解决方案1】:

    为您的活动设置触发器。把它放在 ContentTemplate 的末尾

    <Triggers>
            <asp:AsyncPostBackTrigger ControlID="lbPreview" EventName="Click" />
     </Triggers>
    

    【讨论】:

    • 我收到此错误 - 在 UpdatePanel 'Update' 中找不到触发器的 ID 为 'lbPreview' 的控件
    • 好的,你的控制在网格内,这就是为什么这不起作用
    【解决方案2】:

    用这个替换你的旧事件

    protected void gvSession_DataBound(object sender, GridViewRowEventArgs e)  
    {  
       LinkButton Preview = e.Row.FindControl("lbPreview") as LinkButton;  
       ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(Preview);  
    }  
    

    并将 GridView 中的 gvSession_DataBound 事件分配为 DataBound 事件

    【讨论】:

    • e.Row 没有被解析所以..我这样说.. foreach (GridViewRow grdrow in gvSession.Rows) { LinkBut​​ton Preview = (LinkBut​​ton)grdrow.FindControl("lbPreview"); ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(Preview); ..................................但这也行不通..即使在调试方法被调用和一切..我只是没有得到原因:( :(
    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多