如何在按钮点击时触发gridview selectedindex更改事件 [英] how to fire gridview selectedindex changed event on button click

查看:70
本文介绍了如何在按钮点击时触发gridview selectedindex更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中有程序,它在gridview中显示产品详细信息。如果我单击gridview上的选择按钮,行项将选择并添加到相应的文本框中。但是我有一个提交按钮。所以我想将选定的索引更改事件添加到该提交按钮。请帮助。



这是我在gridview选择的索引中的代码已更改:

  GridViewRow   row   =   GridView1  .SelectedRow; 
TextBox1 文本 = row .Cells [1] .Text;
TextBox2 文本 = row .Cells [2] .Text;
TextBox3 文本 = row .Cells [3] .Text;
TextBox4 文本 = row .Cells [4] .Text;







如何将其添加到提交按钮点击事件。



添加回答:

 使用系统; 
使用 System.Collections;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;
使用 System.IO;
使用 System.Collections.Generic;

public partial class Default4:System.Web.UI.Page
{
SqlConnection con = new SqlConnection( 数据源= VISHNUIT;初始目录= sims1;集成安全性=真);
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
DataTable dt = new DataTable();
受保护 void Page_Load( object sender,EventArgs e)
{
Body.Attributes [ bgcolor ] = apple green;
}
protected void GridView1_SelectedIndexChanged( object sender,EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
TextBox3.Text = row.Cells [ 1 ]。文字;
TextBox4.Text = row.Cells [ 2 ]。文字;
TextBox5.Text = row.Cells [ 3 ]。文字;
}
受保护 void TextBox3_TextChanged( object sender,EventArgs e)
{
String str = select * from login where(uname like + @search +'%');
SqlCommand xp = new SqlCommand(str,con);
xp.Parameters.Add( @ search,SqlDbType.VarChar).Value = TextBox3.Text;
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, uname);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
受保护 void Button1_Click(对象发​​件人,EventArgs e)
{

}
}





这里button1是我的提交按钮。

解决方案

如果你只想在按钮点击时调用gridview选择的索引更改属性,那么只需做一件事..

在按钮点击下面写下代码。





 受保护  void  Button2_Click( object  sender,EventArgs e)
{
GridView1_SelectedIndexChanged(sender,e)

}





它只是在按钮点击事件中调用gridview1_selectedIndexchanged事件。那么你在索引改变事件中写的所有代码都将被激活。希望这个答案可以帮到你。


  GridViewRow   row   =   GridView1  .SelectedRow; 
TextBox1 文本 = row .Cells [1] .Text;
TextBox2 文本 = row .Cells [2] .Text;
TextBox3 文本 = row .Cells [3] .Text;
TextBox4 文本 = row .Cells [4] .Text;









在隐藏字段中存储选定的行索引

ex:hiddenfield1.value = 0;





 TextBox1.Text = GridView1.Rows [hiddenfield1.value] .Cells [1] .Text; 
TextBox2.Text = GridView1.Rows [hiddenfield1.value] .Cells [2] .Text;
TextBox3.Text = GridView1.Rows [hiddenfield1.value] .Cells [3] .Text;
TextBox4.Text = GridView1.Rows [hiddenfield1.value] .Cells [4] .Text;
< / pre >

.rows


I have program in asp.net which displays product details in gridview. If I click on the select button on the gridview that row items will select and added to the corresponding textboxes. But n0w I have a submit button. So I want to add the selected index changed event to that submit button. pls help.

this is my code in gridview selected index changed:

GridViewRow row = GridView1.SelectedRow;
        TextBox1.Text = row.Cells[1].Text;
        TextBox2.Text = row.Cells[2].Text;
        TextBox3.Text = row.Cells[3].Text;
        TextBox4.Text = row.Cells[4].Text;




how can I add this to the submit button click event.

Added from answering:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Collections.Generic;

public partial class Default4 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data source = VISHNUIT;Initial Catalog = sims1; Integrated security= True");
    SqlCommand cmd = new SqlCommand();
    SqlDataReader dr;
    DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
    {
        Body.Attributes["bgcolor"] = "apple green"; 
    }
   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        TextBox3.Text = row.Cells[1].Text;
        TextBox4.Text = row.Cells[2].Text;
        TextBox5.Text = row.Cells[3].Text;
    }
    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        String str = "select * from login where (uname like  + @search + '%' )";
        SqlCommand xp = new SqlCommand(str,con);
        xp.Parameters.Add("@search", SqlDbType.VarChar).Value = TextBox3.Text;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = xp;
        DataSet ds = new DataSet();
        da.Fill(ds, "uname");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
}



Here button1 is my submit button.

解决方案

if you only want to called the gridview selected index change property on button click then just simply do one thing..
write below code on button click.


protected void Button2_Click(object sender, EventArgs e)
{
      GridView1_SelectedIndexChanged(sender, e)

}



it just called the gridview1_selectedIndexchanged event in your button click event. then all your code written in the index changed event will fire. hope this answer helps you.


GridViewRow row = GridView1.SelectedRow;
        TextBox1.Text = row.Cells[1].Text;
        TextBox2.Text = row.Cells[2].Text;
        TextBox3.Text = row.Cells[3].Text;
        TextBox4.Text = row.Cells[4].Text;





store selected row index in hidden field
ex: hiddenfield1.value=0;


TextBox1.Text = GridView1.Rows[hiddenfield1.value].Cells[1].Text;
        TextBox2.Text = GridView1.Rows[hiddenfield1.value].Cells[2].Text;
        TextBox3.Text = GridView1.Rows[hiddenfield1.value].Cells[3].Text;
        TextBox4.Text = GridView1.Rows[hiddenfield1.value].Cells[4].Text;
</pre>

.rows


这篇关于如何在按钮点击时触发gridview selectedindex更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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