如何浏览文本框后退 [英] how to navigate through textboxes rewind

查看:87
本文介绍了如何浏览文本框后退的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我正在尝试通过文本框导航到下一条记录,但尚未成功,尝试了其他方法,但没有结果

hi all
i am trying to navigate to next record through textbox but have not yet succeeded, tried different approaches but with no result

public partial class data_entry : System.Web.UI.Page
{
    SqlDataAdapter da1;
    DataTable dt1;
    int i = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        string strcon = ConfigurationManager.ConnectionStrings["gallup"].ConnectionString;
        string next = "SELECT * from city_breakup";
        da1 = new SqlDataAdapter(next, strcon);
        dt1 = new DataTable();
        da1.Fill(dt1);
    }
protected void btn_next_Click(object sender, EventArgs e)
    {
        if(i<dt1.Rows.Count-1){
            i++;
            txtlcity.Text=dt1.Rows[i]["city"].ToString();
            txtsample.Text = dt1.Rows[i]["sample"].ToString();
        }
    }
}

推荐答案

页面类中的实例变量(在本例中为i)不在请求之间存储.您对Web应用程序的性质(通常是HTTP)有一个(非常常见的)基本误解:您的页面类实例存在于单个HTTP请求(即包含页面的单个请求和响应)的生存期内,以及何时用户请求一个新页面(例如,通过按回发按钮),您将获得一个新实例,而我将再次为0.

老式ASP.net所迷恋的伪造鼓励了这种误解(这就是我讨厌它的原因):回发控件以非常不透明的方式存储大量状态,并使其看起来像您的实例保持活动状态并保留了其状态信息.在尝试编写类似于WinForms应用程序的代码时发现的情况并非如此.

在这种情况下,我建议您简单地使下一步"按钮或链接提交要作为要请求页面的查询参数,然后从页面代码中的Request.Form中提取它.
Instance variables in the page class (in this case, i) are not stored between requests. You have a (very common) fundamental misunderstanding of the nature of a web application, and HTTP in general: your page class instance exists for the lifetime of a single HTTP request (i.e. a single request and response containing a page), and when the user requests a new page (for example by pressing a postback button) you will get a new instance, and i will be 0 again.

This misunderstanding is encouraged by the fakery that old school ASP.net indulges in (which is why I hate it): postback controls store lots of state in a very opaque way and make it look like your instance stays active and its state information is preserved. That is not the case as you discover when you try to write code similar to a WinForms application.

In this case I recommend simply making the ''next'' button or link submit a query parameter that is the page to request, and extracting it from Request.Form in your page code.


这篇关于如何浏览文本框后退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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