我们如何编码在GridView中动态添加的按钮 [英] how can we code for dynamically added button in gridview

查看:51
本文介绍了我们如何编码在GridView中动态添加的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Drawing;


public partial class Approver : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection("server='fre-ggn-fstest\\sharepoint'; database = 'DMS1'; integrated security = 'true';");
    SqlDataAdapter sda;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn.Open();
        FillData();
    }

    public void FillData()
    {
        string str = "select * from tdocument";
        sda = new SqlDataAdapter(str, cn);
        ds = new DataSet();
                
        sda.Fill(ds);
        cn.Close();
        GVDetails.DataSource = ds;
        GVDetails.DataBind();


        Button apr = new Button();
        Button rej = new Button();

        apr.Text = "APPROVE";
        rej.Text = "REJECT";
        
        
        GVDetails.Controls.Add(apr);
        GVDetails.Controls.Add(rej);
  // FROM HERE I WANT TO ADD THE CODE FOR THE 2 CREATED BUTTONS...... ON CLICKING THE "approve" THE STATUS FIELD IN DATABASE MUST BE CHANGED IN AS "approved"..... AND ON CLICKING THE ..... "reject" BUTTON THE STATUS IN THE DATABASE MUST BE ENTEERED AS "rejected"........      

    }
    
}

推荐答案

如上所述,您已经添加了两个按钮.喜欢:

As you mentioned above you have added two buttons. Like:

apr.Text = "APPROVE";
rej.Text = "REJECT";


//Add the command name here to work with this buttons.
//You can also add a command argument if required
apr.CommandName="Approve";
rej.CommandName="Reject";

GVDetails.Controls.Add(apr);
GVDetails.Controls.Add(rej);



现在转到网格视图的RowCommand事件并过滤命令.喜欢:



Now go to the grid view RowCommand event and filter your commands. Like:

protected void gvMyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Reject")
    {
        //Your code goes here
    }
    else if (e.CommandName == "Approve")
    {
       //Your code goes here
    }
}




试试看.如果愿意,请接受答案.
祝一切顺利.:)




Try this.. Accept the answer if you like..
All the best.:)




您也可以这样做,

如果您有下拉列表,则

Hi,

you can do this as well,

if you have dropdownlist then

ddlDropDown2.OnSelectedIndexChanged += new EventHandler(this.ddlDropDown2_SelectedIndexChanged);



在这里,您可以看到下拉列表中的selectIndexChaged事件已创建,并且其处理程序也已创建.同样,您也可以将其用于按钮.

谢谢
-amit.



Here you can see dropdown selectIndexChaged event is created and it''s handler is also created. similarly you can do it for button.

thanks
-amit.


我正在提供所有代码.请立即检查.
I am giving all codes.. Check it now yaar..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=IBM\\IBMDB;Initial Catalog=Testing;Persist Security Info=True;User ID=admin;Password=admin@2011");
    protected void Page_Load(object sender, EventArgs e)
    {
        fillGrid();
    }

    public void fillGrid() {
        con.Open();
        SqlCommand com = new SqlCommand("Select * from tbLogin", con);
        SqlDataAdapter sda = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        com.Dispose();
        sda.Dispose();
        con.Close();
        //Adding two columns in datatable.
        ds.Tables[0].Columns.Add("Approv");
        ds.Tables[0].Columns.Add("Reject");

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //Adding the buttons
        Button b1 = new Button();
        Button b2 = new Button();
        b1.Text = "Approved";
        b2.Text = "Reject";

        //assignnig command name
        b1.CommandName = "Approved";
        b2.CommandName = "Reject";

        if (e.Row.RowType == DataControlRowType.DataRow) {
            e.Row.Cells[3].Controls.Add(b1);
            e.Row.Cells[4].Controls.Add(b2);
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Approved") {
            Response.Write("Aproved");
        }
        else if (e.CommandName == "Reject") {
            Response.Write("Reject");
        }
    }
}



如果您得到确切的答案,请回复我.而且您可以根据自己的要求进行修改..

阿米特·库玛(Amit Kumar)



Reply me if you got the exact answer.. And ya you can modify this according to your requirement..

Amit Kumar


这篇关于我们如何编码在GridView中动态添加的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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