从Web窗体到Windows窗体 [英] From Web Forms to Windows Forms

查看:86
本文介绍了从Web窗体到Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



请帮帮我。我正在从Web窗体转移到Windows窗体,我正在使用Viewstate和LINQ to SQL连接到数据库。我试图在Windows窗体上使用viewstate,它会抛出错误。我不知道如何改变我的班级,以便我可以使用Windows窗体。



请帮助我如何修改我的班级,因为我不会使用Web窗体,请参阅下面的代码

Hi Everyone,

Please help me. I am moving from Web Forms to Windows Forms and i have being using Viewstate and LINQ to SQL to connect to the database.I tried to use viewstate on windows forms and it throws errors. i have no idea on how can i change my class so that i works fine with windows forms.

Please help me on how can i modifies my class since i wont be using Web Forms, see my code below

public class Degree
{
    private MCX_COLLEGE_Data_ClassesDataContext _ds = new MCX_COLLEGE_Data_ClassesDataContext();
    private string Error = string.Empty;
    private int i = 0;

    public Degree()
    {
    }

    public string Error_Exception
    {
        get
        {
            return Error;
        }
        set
        {
            Error = value;
        }
    }
    public IList<degree> GetDegree()
    {
        return _ds.DEGREEs.ToList();
    }
    public IList<degree> GetDegree(int DegreeID)
    {
        IList<degree> _DegreeList = _ds.DEGREEs.ToList();

        var _List = from P in _ds.DEGREEs
                    where P.DegreeID == DegreeID
                    select P;

        if (_List.Count() > 0)
        {
            _DegreeList = _List.ToList();
        }
        return _DegreeList;
    }

    public int Save(int ParID, DEGREE _degreetbl)
    {

        using (var MyTran = new TransactionScope())

            try
            {
                _ds.Connection.Open();
                _ds.SP_DEGREE
                    (
                      ParID,
                     _degreetbl.DegreeID,
                     _degreetbl.Description,
                     _degreetbl.Duration_Years
                     );

                _ds.SubmitChanges();
                MyTran.Complete();
            }
            catch (Exception ex)
            {
                _ds.Connection.Close();
            }
         return ParID;



------------------------------- ----------------------------------------



主表格代码

-----------------


-----------------------------------------------------------------------

Code for main form
-----------------

private Degree _d = new Degree();
     private DEGREE _degtbl = new DEGREE();
     private int _Results = 0;

     private Enum_Class _enum = new Enum_Class();

     private string _ApplicationMessage = string.Empty;

     protected void Page_Load(object sender, EventArgs e)
     {
         if (!Page.IsPostBack)
         {
             ViewState["PageMode"] = "Save";
             this.Populate();
         }

     }


     public void Populate()
     {
         grdDegree.DataSource = _d.GetDegree();
         grdDegree.DataBind();
     }
     public int Save()
     {
         _degtbl.DegreeID = 0;
         _degtbl.Description = txtDescription.Text;
         _degtbl.Duration_Years = Convert.ToInt32(txtDuration.Text);
         return _d.Save((int)Enum_Class.SQL_Parameters.Save, _degtbl);
     }
     public int Update()
     {
         _degtbl.DegreeID = int.Parse(ViewState["DegreeID"].ToString());
         _degtbl.Description = txtDescription.Text;
         _degtbl.Duration_Years = int.Parse(txtDuration.Text);
         return _d.Save((int)Enum_Class.SQL_Parameters.Update, _degtbl);
     }
     public int Delete()
     {
         _degtbl.DegreeID = int.Parse(ViewState["DegreeID"].ToString());
         _degtbl.Description = txtDescription.Text;
         _degtbl.Duration_Years = int.Parse(txtDuration.Text);

         return _d.Save((int)Enum_Class.SQL_Parameters.Delete, _degtbl);
     }


     protected void btnSubmit(object sender, EventArgs e)
     {
         switch (ViewState["PageMode"].ToString())
         {
             case "Save":

                 switch(this.Save())
                 {
                     case 1:
                         _ApplicationMessage = _d.Error_Exception;
                         lblError.Text = _ApplicationMessage;
                         lblMessage.Visible = true;
                         break;

                     case 0:
                         _ApplicationMessage = "Degree sucessfully submited";
                         lblMessage.Text = _ApplicationMessage;
                         lblMessage.Visible = true;

                         this.NoDuplicate();
                         break;


                 }
                 break;

             case "Upade":

                 switch(this.Update())
                 {
                     case 1:
                         _ApplicationMessage = _d.Error_Exception;
                         lblError.Text = _ApplicationMessage;
                         lblError.Visible = true;
                         break;

                     case 0:
                         _ApplicationMessage = "Degree sucessfully updated submited";
                         lblMessage.Text = _ApplicationMessage;
                         lblMessage.Visible = true;

                         this.NoDuplicate();
                         break;
                 }
                 break;

             case "Delete":

                 switch (this.Delete())
                 {
                     case 1:
                         _ApplicationMessage = _d.Error_Exception;
                         lblError.Text = _ApplicationMessage;
                         lblError.Visible = true;
                         break;

                     case 0:
                         _ApplicationMessage = "Degree Deleted";
                         lblMessage.Text = _ApplicationMessage;
                         lblMessage.Visible = true;
                         break;
                 }
                 break;

         }
         this.Populate();
     }

     protected void btnDelete(object sender, EventArgs e)
     {
         txtDescription.Text = "";
         txtDuration.Text = "";
     }
     public void NoDuplicate()
     {
         txtDescription.Text = "";
         txtDuration.Text = "";
         this.Populate();
         ViewState["PageMode"] = "Save";
     }

     protected void grdDegree_SelectedIndexChanged(object sender, EventArgs e)
     {
         GridViewRow _Rows = grdDegree.SelectedRow;
         string str = _Rows.Cells[0].Text;
         ViewState["DegreeID"] = str;

         IList<degree> _Edit = _d.GetDegree(int.Parse(str));

         if(_Edit != null)
         {
             foreach (DEGREE _degreeList in _Edit)
             {
                 ViewState["DegreeID"] = _degreeList.DegreeID.ToString();
                 txtDescription.Text = _degreeList.Description;
                 txtDuration.Text = _degreeList.Duration_Years.ToString();

             }
         }
         ViewState["PageMode"] = "Update";
     }</degree></degree></degree></degree>

推荐答案

Win Forms编程使用完全不同的Web表单方法。



使用Web表单,客户端代码(HTML /浏览器页面)与服务器端(应用程序代码)分离,因此ASP.Net使用查看状态和会话状态来管理应用程序状态。实施Web园艺会使此问题变得更糟,因为您无法保证为每个后续请求获取相同的过程。



这不是Win Forms的问题。表单附加到应用程序代码并作为单个进程运行。因此,不需要视图状态和会话状态。



要在Win Forms应用程序中保留信息,您只需将对象分配给可在需要时检索的引用。



在Web窗体中,每个帖子都会构建页面。使用Win Forms,表单只构造一次,动作通常是表单上的事件。



视图状态的目的是保持Web表单的状态在回发后重建页面时因为使用Win Forms只有一个表单的实例在关闭之前一直存在,所以不需要查看状态。



如何在其他方面存在很多差异你也应该接近设计。
Win Forms programming uses a completely different approach to Web Forms.

With web forms the client code (HTML/Browser page) is detached from the Server Side (Application code) so ASP.Net uses the View State and Session State to manage application state. This problem is made worse by implementing Web Gardening as you cannot guarantee to get the same process for each subsequent request.

This is not an issue with Win Forms. The Form is attached to the application code and runs as a single process. So no view state and no session state are required.

To persist information in a Win Forms application you need simply assign the object to a reference which can be retrieved when required.

In Web Forms the page is constructed with every post back. With Win Forms the form is only constructed once and actions are generally an event on the form.

The purpose of the View State is to persist the state of the Web Form when the page is reconstructed after a post back. Because with Win Forms there is only one instance of the form which persists until closed, there is no need for a View State.

There are lots of other differences in how you should approach to design too.


这篇关于从Web窗体到Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆