如何在没有任何事件的情况下从网格视图文本框中获取所有值. [英] how to fetch all the values from a grid view textbox without any events.

查看:87
本文介绍了如何在没有任何事件的情况下从网格视图文本框中获取所有值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从网格视图项目Templete文本框中获取所有值?
我的网格视图没有任何编辑或更新按钮.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        getdataset();
    }
    public void getdataset()
    {
        SqlConnection con = new SqlConnection("Data Source=BIZ1;Initial Catalog=Venky;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select * from student",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds, "s1");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        //cmd.ExecuteNonQuery();
        con.Close();     
    }
    protected void txt1_Click(object sender, EventArgs e)
    {
       int rowIndex = 0;
        string  box1 = ((TextBox)GridView1.Rows[0].Cells[2].FindControl("TextBox1")).ToString();
    }
}



我尝试了上面的代码,但无法正常工作!请帮忙.

解决方案

您可以从网格的文本框中获取值,但需要触发一些事件以使页面回发.
您也可以使用JavaScript触发相同的操作.

用于提取的代码段为

DBHelper objCon = new DBHelper("Conn String");
string strQuery = string.Empty;
string strLoginId = Session["LoginId"].ToString();
try
{
    foreach (GridViewRow gvRow in grvData.Rows)
    {
        TextBox objText = (TextBox)gvRow.FindControl("txtSortOrder");
        string strSortOrder = objText.Text;
        string strCatId = objText.ToolTip;
        strQuery = "<SQL Query>";
        objCon.ExecuteNonQuery(strQuery);
    }
    lblError.Text = "Sort Order Updated!!!";
}
catch (Exception ex)
{
    lblError.Text = "There was error while Processing!!";
    lblError.ToolTip = ex.ToString();
}



这样,您可以从网格控件中获取值

how to get all the values from a grid-view item templete textbox?
my grid view does not have any edit or update buttons .

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        getdataset();
    }
    public void getdataset()
    {
        SqlConnection con = new SqlConnection("Data Source=BIZ1;Initial Catalog=Venky;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select * from student",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds, "s1");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        //cmd.ExecuteNonQuery();
        con.Close();     
    }
    protected void txt1_Click(object sender, EventArgs e)
    {
       int rowIndex = 0;
        string  box1 = ((TextBox)GridView1.Rows[0].Cells[2].FindControl("TextBox1")).ToString();
    }
}



I tried the above code but its not working! Please help.

解决方案

You can fetch the value from the textbox of the grid but you need to fire some event so that the page post back.

You can trigger the same using the JavaScript also.

Code snippet for fetching is as

DBHelper objCon = new DBHelper("Conn String");
string strQuery = string.Empty;
string strLoginId = Session["LoginId"].ToString();
try
{
    foreach (GridViewRow gvRow in grvData.Rows)
    {
        TextBox objText = (TextBox)gvRow.FindControl("txtSortOrder");
        string strSortOrder = objText.Text;
        string strCatId = objText.ToolTip;
        strQuery = "<SQL Query>";
        objCon.ExecuteNonQuery(strQuery);
    }
    lblError.Text = "Sort Order Updated!!!";
}
catch (Exception ex)
{
    lblError.Text = "There was error while Processing!!";
    lblError.ToolTip = ex.ToString();
}



This way you can fetch the values from the grid control


这篇关于如何在没有任何事件的情况下从网格视图文本框中获取所有值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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