【问题标题】:asp:DataGrid not displaying data on Delegate callbackasp:DataGrid 在委托回调中不显示数据
【发布时间】:2014-08-08 14:22:09
【问题描述】:

当我单击“关机”按钮时,它会发送一条消息,并且我也收到了可以在 DataTable 中验证的响应。但是 DataGrid 没有被填充。我检查了 DataGrid 的 Rows 属性,它有行,但是页面不显示结果。

这是我的代码:

BasicControls.aspx 页面

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <%-- asp:AsyncPostBackTrigger ControlID="UpdateTimer" EventName="Tick" />--%>
    </Triggers>
    <ContentTemplate>
        <div id="cmdResultsgrdView">
            <asp:GridView ID="grdMessage" runat="server" AutoGenerateColumns="true">
            </asp:GridView>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ListenerTimer" EventName="Tick" />
    </Triggers>
    <ContentTemplate>

.aspx.cs 文件

{
    private static Action<List<IMessage>> callBackCommandResponse = null;
    //ctor
    public BasicControls()
    {
        callBackCommandResponse = CommandResponse;
    }
    //Callback Method
    public void CommandResponse(List<IMessage> Message)
    {
        //Based on MsgType we will filter and display the response.
        if (null != Message)
        {
            //2. Call GetMessage to filter and process message and return DataTable
            resultsDataTable = GetMessages(Message);
            //3. Populate the Grid binding to DataTable
            grdMessage.DataSource = resultsDataTable;
            grdMessage.DataBind();
        }
    }
    //receiver constructor in which we are passing callback to be populated. 
    private ReceivePM receiver = new ReceivePM(callBackCommandResponse);

    //On clicking the code sends Shutdown Command to message queue.
    protected void btnShutdown_Click(object sender, EventArgs e)
    {
        SenderInformation cmdSender = new SenderInformation();
        cmdSender.SendShutdownCommand();
    }
}

【问题讨论】:

  • Gridview 在 UpdatePanel1 里面,并且 UpdateMode 是“Conditional”,那么你需要在后面的代码中手动调用 Update() 方法。尝试在 DataBind() 事件后调用“UpdatePanel1.Update()”方法。

标签: c# asp.net datagrid datatable


【解决方案1】:

尝试在 DataBind() 事件后调用“UpdatePanel1.Update()”方法。

grdMessage.DataSource = resultsDataTable;
grdMessage.DataBind();
UpdatePanel1.Update();

【讨论】:

  • 如果您能够看到数据源中的行,它应该可以工作。您是否尝试将 UpdateMode 更改为默认模式“Always”?
  • 我对两者都进行了测试。发生的事情是,当我单击按钮时,会发生回发并发出命令。我通过回调获取数据,但 DataGrid 已经呈现。我在更新面板中也有文本框,即使它没有被渲染
  • 糟糕.. 没想到。您将必须实现 ICallBackEventHandler。欲了解更多信息,请查看这篇文章Binding GridView using CallBack
【解决方案2】:

谢谢大家。 但我想我的情况是应用程序与之交互的不同场景 队列。 所以,我没有使用 ICallBackEventHandler,我实现了自己的回调并将队列侦听器方法作为一个单独的任务而不是在计时器内启动。 如果您有自己的回调,则需要以下代码 if (resultsDataTable.Rows.Count > 0) { grdMessage.DataSource = 结果数据表; grdMessage.DataBind(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); grdMessage.RenderControl(hw);

            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多