0

我想为我想做的事情找到最好的方法。我在数据库中有一个存储过程,它确实从另一个数据库导入到我们的数据库并链接信息。如果链接信息丢失,它会产生错误,由用户解决。在我的代码隐藏中,我收到了这些错误,我创建了一个 html 表,为每个错误添加了行。现在我想在多个页面中保留这些信息,所以我将它存储在会话中,因为它只是一个 html 表而不是像网格这样的重型控件,什么是在会话中存储 HTML 表或任何其他方式的最佳方式和如何在客户端的 Javascript 或 Jquery 中访问它,以便我可以利用 Jquery 函数来显示显示此信息的可扩展 div

protected void BtnAddDock1_Click(object sender, EventArgs e)
    {
        try
        {
            HtmlGenericControl RadDocksDiv = (HtmlGenericControl)Master.FindControl("RadDocksDiv");
            RadDocksDiv.Style["display"] = "block";

            Session["Docking"] = "Open";

            Table tblErrors = new Table();
            tblErrors.ID = "tblErrors";
            tblErrors.CssClass = "tabularData";
            tblErrors.Width = 800;
            TableHeaderRow hr = new TableHeaderRow();
            // hr.ID = "tblErrorsHeaderRow";
            TableHeaderCell thc = new TableHeaderCell();
            //thc.ID = "tblErrorsHeaderCell";
            thc.Text = "Error Descriptions";
            hr.Cells.Add(thc);
            tblErrors.Rows.Add(hr);
            Panel pnlErrors = (Panel)Master.FindControl("pnlErrors");

            Table dobleTable = (Table)Master.FindControl("tblErrors");

            if (dobleTable == null)
            {
                pnlErrors.Controls.Add(tblErrors);



                for (int i = 0; i < 10; i++)
                {
                    TableRow tableRow = new TableRow();
                    string error = "This is a website generated Import Error that needs to be added to the panel on Masterpage!";
                    TableCell tableCell = new TableCell();
                    tableCell.Text = error;
                    tableRow.Cells.Add(tableCell);
                    tblErrors.Rows.Add(tableRow);
                }


                Session["ErrorsTable"] = tblErrors;
            }
            //Table tblErrors = (Table)Master.FindControl("tblErrors");


        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

这就是我试图在 Javascript 中获取它的方式,但是当我尝试从会话中获取它时,我所能看到的只是 System.Web.UI.WebControls.Table。

    //Access html table in  session
    var dockingState = '<%= Session["Docking"] %>'

    if (dockingState == "Open") {

        // This doesnt return the html table correctly?!?
        var sessiontblErrors = '<%= Session["ErrorsTable"] %>'

        if (sessiontblErrors != null) {
            var pnlErrors = $("[id$=pnlErrors]");

        }
    }
4

4 回答 4

3

If you really want to keep errors in session to show across pages, I would do it in this way. Instead of creating HTML table and storing in session, i would create a list of Errors in Session and access it whenever i need and display it in whatever format i want. It can be a HTML table, Ordered List , Spans etc...

I would simply create a class to hold my Error Messages

public class ErrorMessage
{
  public string ErrorCode { set;get;}
  public string Message { set;get;}
}

and in your code, instead of creating the HTML table row, i will add this to a list

List<ErrorMessage> objErrorList=new List<ErrorMessage>();

for(int i = 0; i < 10; i++)
{
  ErrorMessage objError=new ErrorMessage();
  objError.ErrorCode=5020;
  objError.Message="There is such a bad error!";
  objErrorList.Add(objError);
}
if(objErrorList.Count>0)
{
  Session["Errors"]=objErrorList;
}

And Whenever i want this i will return this from Session. I would wrap this in a function.

public List<ErrorMessage> GetErrors()
{
  List<ErrorMessage> objList=new List<ErrorMessage>();
  if(Session["Errors"]!=null)
  {
    objList=(List<ErrorMessage>)Session["Errors"];
  }
  return objList;
}

If you want this from javascript, you can get it by making a call to a server page (an aspx page / ashx handler) using jQuery ajax/getJson. Some thing like this.

var strErrors="<ul>";
$.getJSON('geterrors.ashx', function(data) {
    $.each(data, function(key, val) {
      strErrors+="<li>" + val + "</li>";
    });
});
strErrors+="</ul>";

$("#yourMsgDiv").html(strErrors);

Where in geterrors.ashx, you will call our GetErrors method and convert that toJSon format and return.

The main advantage in this approach is that the Presentation is not tightly tied to data. We can show the error message in various styles.

于 2012-04-26T15:53:31.853 回答
1

我创建一个 html 表,为每个错误添加行

实际上,这一行Table tblErrors = new Table(); 创建了一个System.Web.UI.WebControls.Table,它是一个 ASP.NET 服务器端控件。

如果你真的想创建一个 HTML 表格,你需要使用该Render方法来生成 HTML。也就是说,我认为您应该按照Shyju的建议进行操作,并从数据中删除演示文稿。

于 2012-04-26T15:59:19.090 回答
0

首先,这里有一个很大的误解。

Session存储对象的序列化字符串表示。

Session["ErrorsTable"] = tblErrors;

了解tblErrors在您的 c# 代码中不等于

<table><tr><td>...</td></tr></table>

tblErrors是一个对象,它有一个类,就像任何其他 c# 类一样。(释义)当页面由 asp.net 呈现以发送到客户端时,该类被翻译为 html。但是,如果您将类的一个实例放入Session中,那将不是您将得到的。您正在存储实际对象,该对象Session为您进行序列化和反序列化。

我希望这会有所帮助,我意识到这是一个难以解释的话题。

于 2012-04-26T15:55:50.180 回答