从会话中检索值并将其分配给变量

时间:2014-03-25 08:05:06

标签: asp.net

在下面的代码中,我获取arraylist中的所有id并将其存储在sample.aspx中的会话中,并在test.aspx中检索会话值。现在我想在页面加载中将项目ID分配给DataSet dsField。我怎样才能分别得到这个价值。 sample.aspx

Button btnView = (Button)e.CommandSource;
Label lblProjectId = (Label)btnView.Parent.FindControl("ProjectID");
Label lblBatchID = (Label)btnView.Parent.FindControl("BatchID");
Label lblImageID = (Label)btnView.Parent.FindControl("ImageID");
Label lblReasons = (Label)btnView.Parent.FindControl("Reasons");
Label lblLayerID = (Label)btnView.Parent.FindControl("LayerID");
Label lblStatusID = (Label)btnView.Parent.FindControl("StatusID");
Label lblProcessID = (Label)btnView.Parent.FindControl("ProcessID");
ArrayList SearchUrlValues = new ArrayList();
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblImageID);
SearchUrlValues.Add(lblReasons);
SearchUrlValues.Add(lblLayerID);
SearchUrlValues.Add(lblStatusID);
SearchUrlValues.Add(lblProcessID);
Session["ProjectDetails"] = SearchUrlValues.ToArray();
Response.Write(SearchUrlValues); 

Test.aspx文件:

 Array SearchUrlValues = (Array)Session["ProjectDetails"];

 if (!IsPostBack)
 {
      DataSet dsField = GetFieldData(10);//how to assign projectid instead of 10
      gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
      gmasFieldsContr.EnumTable = dsField.Tables[1];
      gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
      gmasFieldsContr.BindData();
 }
public DataSet GetFieldData(int iProjectID)
    {
        try
        {
            SqlParameter[] SqlParam = new SqlParameter[1];

            SqlParam[0] = new SqlParameter("@i_ProjectID", SqlDbType.Int);
            SqlParam[0].Value = iProjectID;


            return ExecuteQuery(SqlParam, "spGetFieldData");
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
    }

2 个答案:

答案 0 :(得分:1)

<强>被修改

在Sample.aspx中,不要将SearchUrlValues存储为数组

Button btnView = (Button)e.CommandSource;
Label lblProjectId = (Label)btnView.Parent.FindControl("ProjectID");
Label lblBatchID = (Label)btnView.Parent.FindControl("BatchID");
Label lblImageID = (Label)btnView.Parent.FindControl("ImageID");
Label lblReasons = (Label)btnView.Parent.FindControl("Reasons");
Label lblLayerID = (Label)btnView.Parent.FindControl("LayerID");
Label lblStatusID = (Label)btnView.Parent.FindControl("StatusID");
Label lblProcessID = (Label)btnView.Parent.FindControl("ProcessID");
ArrayList SearchUrlValues = new ArrayList();
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblImageID);
SearchUrlValues.Add(lblReasons);
SearchUrlValues.Add(lblLayerID);
SearchUrlValues.Add(lblStatusID);
SearchUrlValues.Add(lblProcessID);
Session["ProjectDetails"] = SearchUrlValues; // Store it as ArrayList
Response.Write(SearchUrlValues);

然后test.aspx,将Session对象转换为ArrayList;

var SearchUrlValues = (ArrayList)Session["ProjectDetails"];

if (!IsPostBack)
{
    var projectId = int.Parse(SearchUrlValues[0].ToString());
    DataSet dsField = GetFieldData(projectId);//how to assign projectid instead of 10
    gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
    gmasFieldsContr.EnumTable = dsField.Tables[1];
    gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
    gmasFieldsContr.BindData();
}

顺便提一下,请注意您已经两次添加lblProjectId;

SearchUrlValues.Add(lblProjectId); // First
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId); // Second

此外,我更愿意使用一个对象在会话中存储这些值。

public class SearchUrlValues
{
    public int lblProjectId { get; set; }
    public int lblBatchID { get; set; }
    public int lblImageID { get; set; }
    public int lblReasons { get; set; }
    public int lblLayerID { get; set; }
    public int lblStatusID { get; set; }
    public int lblProcessID { get; set; }
}

然后,而不是arraylist;

var newSearchUrlValues = new SearchUrlValues()
{
    lblProjectId = lblProjectId,
    lblBatchID = lblBatchID,
    lblImageID = lblImageID,
    lblReasons = lblReasons,
    lblLayerID = lblLayerID,
    lblStatusID = lblStatusID,
    lblProcessID = lblProcessID
};

Session["ProjectDetails"] = newSearchUrlValues;

并检索它;

var searchUrlValues = (SearchUrlValues)Session["ProjectDetails"];
var projectId = searchUrlValues.lblProjectId;

答案 1 :(得分:0)

尝试这样

 ArrayList SearchUrlValues = (ArrayList)Session["ProjectDetails"];

 if (!IsPostBack)
 {
      DataSet dsField = GetFieldData(Convert.ToInt32(SearchUrlValues[0].ToString()));
    //fetech 1st element of array List
      gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
      gmasFieldsContr.EnumTable = dsField.Tables[1];
      gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
      gmasFieldsContr.BindData();
 }
public DataSet GetFieldData(int iProjectID)
    {
        try
        {
            SqlParameter[] SqlParam = new SqlParameter[1];

            SqlParam[0] = new SqlParameter("@i_ProjectID", SqlDbType.Int);
            SqlParam[0].Value = iProjectID;


            return ExecuteQuery(SqlParam, "spGetFieldData");
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
    }