本文介绍了如何在按钮单击时在网格视图中显示文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨! i有4个文本框和一个按钮文字=添加 当我点击在按钮添加,我想在网格视图中显示我在文本框中输入的值,并保存在数据库中。 例如像发票 请帮助 从下面的评论中复制的其他代码 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.UI.WebControls; 使用 System.Data; public partial class _Default:System.Web.UI.Page { protected void Page_Load( object sender,EventArgs e) { } private void BindGrid( int rowcount) { DataTable dt = new DataTable(); DataRow博士; dt.Columns.Add( new System.Data.DataColumn( FIRST NAME, typeof ( String ))); dt.Columns.Add( new System.Data.DataColumn( 父亲姓名, typeof ( String ))); dt.Columns.Add( new System.Data.DataColumn( 最后一个名称, typeof ( String ))); dt.Columns.Add( new System.Data.DataColumn( AGE, typeof ( String ))); dt.Columns.Add( new System.Data.DataColumn( EMAIL, typeof ( String ))); dt.Columns.Add( new System.Data.DataColumn( TELEPHONE, typeof ( String ))); if (ViewState [ CurrentData]!= null ) { for ( int i = 0 ; i < rowcount + 1 ; i ++) { dt =(DataTable)ViewState [ CurrentData]; if (dt.Rows.Count > 0 ) { dr = dt.NewRow(); dr [ 0 ] = dt.Rows [ 0 ] [ 0 ]的ToString(); } } dr = dt.NewRow(); dr [ 0 ] = txtname.Text; dr [ 1 ] = txtfname.Text; dr [ 2 ] = txtlname.Text; dr [ 3 ] = txtage.Text; dr [ 4 ] = txtemail.Text; dr [ 5 ] = txttele.Text; dt.Rows.Add(dr); } else { dr = dt.NewRow(); dr [ 0 ] = txtname.Text; dr [ 1 ] = txtfname.Text; dr [ 2 ] = txtlname.Text; dr [ 3 ] = txtage.Text; dr [ 4 ] = txtemail.Text; dr [ 5 ] = txttele.Text; dt.Rows.Add(dr); } // 如果ViewState有数据则使用值为DataSource 如果(ViewState [ CurrentData]!= null ) { GridView1.DataSource =(DataTable)ViewState [ CurrentData]; GridView1.DataBind(); } 其他 { // 使用DataTable中关联的初始数据绑定GridView GridView1.DataSource = dt; GridView1.DataBind(); } // 将DataTable存储在ViewState中以保留值 ViewState [ CurrentData] = dt; } 受保护 void btnadd_Click( object sender,EventArgs e) { // 检查ViewState是否有相关的数据。如果 if (ViewState [ CurrentData]!= null ) { DataTable dt =(DataTable)ViewState [ CurrentData]; int count = dt.Rows.Count; BindGrid(伯爵); } 其他 { BindGrid( 1 ); } txtname.Text = string .Empty; txtfname.Text = string .Empty; txtlname.Text = string .Empty; txtage.Text = string .Empty; txtemail.Text = string .Empty; txttele.Text = string .Empty; txtname.Focus(); txtfname.Focus(); txtlname.Focus(); txtage.Focus(); txtemail.Focus(); txttele.Focus(); } } 解决方案 在按钮上单击提供代码和数据库之间的绑定在页面加载提供网格和数据库之间的绑定,以便页面将加载按钮单击将立即看到新数据。 请参阅create方法将数据保存在数据库中。这只是为了概念 void SavaData() { //将texboxes值保存到数据库 //调用绑定gridview在列表或网格中显示的方法 BindDatainGridview(); } void BindDatainGridview() { //获取值列表 //绑定到gridview } 您可以使用 ViewState 。更新 ViewState 并将其绑定到 GridView 。 示例 - 从ASP.NET GridView动态添加和删除行 [ ^ ] Hi!i have 4 text boxes and a button text = addwhen i click on the button add, i want to display the values in grid view which i have entered in text boxes, and save in database.for eg like an invoicePlease Helpadditional code copied from comment belowusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } private void BindGrid(int rowcount) { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new System.Data.DataColumn("FIRST NAME", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("FATHER NAME", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("LAST NAME", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("AGE", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("EMAIL", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("TELEPHONE", typeof(String))); if (ViewState["CurrentData"] != null) { for (int i = 0; i < rowcount + 1; i++) { dt = (DataTable)ViewState["CurrentData"]; if (dt.Rows.Count > 0) { dr = dt.NewRow(); dr[0] = dt.Rows[0][0].ToString(); } } dr = dt.NewRow(); dr[0] = txtname.Text; dr[1] = txtfname.Text; dr[2] = txtlname.Text; dr[3] = txtage.Text; dr[4] = txtemail.Text; dr[5] = txttele.Text; dt.Rows.Add(dr); } else { dr = dt.NewRow(); dr[0] = txtname.Text; dr[1] = txtfname.Text; dr[2] = txtlname.Text; dr[3] = txtage.Text; dr[4] = txtemail.Text; dr[5] = txttele.Text; dt.Rows.Add(dr); } // If ViewState has a data then use the value as the DataSource if (ViewState["CurrentData"] != null) { GridView1.DataSource = (DataTable)ViewState["CurrentData"]; GridView1.DataBind(); } else { // Bind GridView with the initial data assocaited in the DataTable GridView1.DataSource = dt; GridView1.DataBind(); } // Store the DataTable in ViewState to retain the values ViewState["CurrentData"] = dt; } protected void btnadd_Click(object sender, EventArgs e) { // Check if the ViewState has a data assoiciated within it. If if (ViewState["CurrentData"] != null) { DataTable dt = (DataTable)ViewState["CurrentData"]; int count = dt.Rows.Count; BindGrid(count); } else { BindGrid(1); } txtname.Text = string.Empty; txtfname.Text = string.Empty; txtlname.Text = string.Empty; txtage.Text = string.Empty; txtemail.Text = string.Empty; txttele.Text = string.Empty; txtname.Focus(); txtfname.Focus(); txtlname.Focus(); txtage.Focus(); txtemail.Focus(); txttele.Focus(); }} 解决方案 On the button click provide the binding between Code and Database and on page load provide the binding between Grid and database so as the page will load on button click new data will be immediately seen.See create method to save data in database. This is just for conceptvoid SavaData(){//Save your texboxes values to database//Call the bind gridview method to display in list or gridBindDatainGridview();}void BindDatainGridview(){//Get list of values//Bind to gridview}You can use ViewState for this. Update the ViewState and bind that to GridView.Example - Dynamically adding and deleting rows from ASP.NET GridView[^] 这篇关于如何在按钮单击时在网格视图中显示文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 22:35