在按钮单击ASP.NET上检查gridview中的值 [英] Check value from gridview on button click ASP.NET

查看:85
本文介绍了在按钮单击ASP.NET上检查gridview中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



希望你们都做得很好。

我有疑问,我有一个名为'的列的gridview状态'和值是从数据库Sql中获取的。

Gridview关闭之外有一个按钮。我想要做的是当用户点击按钮时,它应首先检查gridview中的所有记录的状态=已关闭,即使有一条状态为待定的记录,它应该生成一个弹出窗口,询问用户是'你确定你想关闭'

我希望我已经清楚了。期待您的帮助。



非常感谢



我的尝试:



搜索谷歌但找不到类似的东西

Hello there,

Hope you all are doing good.
I have question, I have a gridview with a column named 'Status' and the value is fetched from database Sql.
There is a button outside Gridview 'Close'. What i want to do is when user clicks on the button it should first check all the records in gridview have their status = closed, even if there is one record with status ='pending' it should generate a popup asking user are 'you sure you want to close'
I hope i have made myself clear. Looking forward to your help.

Many thanks

What I have tried:

Searhed google but could not find anything similar

推荐答案

看看这个示例代码:

Check out this sample code:
Label cell; // assumming the cell is a Label control
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
    cell = Gridview1.Rows[i].FindControl("Status") as Label;
    if (cell.Text == "pending") return false
}
return true;


相反于解决方案#1由 Peter Leow [ ^ ],如果gridview对象与数据源绑定 ,我建议不要通过gridview单元格的集合,但通过 gridview [ ^ ] datasource [ ^ ]。



例如:

In opposite to the solution #1 by Peter Leow[^], if gridview object is bounded with datasource, i'd suggest to not follow through the collection of gridview cells, but through the collection of data of gridview[^] datasource[^].

For example:
DataSet ds = (DataSet)gridview1.DataSource;
var pendingdata = ds.Tables[0].AsEnumerable()
	.Where(x=>x.Field<string>("Status")=="pending")
	.ToList();
foreach(DataRow r in pendingdata)
{
	//display message here!
}





详情请见:

DataSet类(System.Data) [ ^ ]

DataSets,DataTables和DataViews [ ^ ]

LINQ to DataSet [ ^ ]

查询数据集(LINQ to数据集) [ ^ ]

查询LINQ to DataSet [ ^ ]

LINQ到DataSet示例 [ ^ ]



For further details, please see:
DataSet Class (System.Data)[^]
DataSets, DataTables, and DataViews[^]
LINQ to DataSet[^]
Querying DataSets (LINQ to DataSet)[^]
Queries in LINQ to DataSet[^]
LINQ to DataSet Examples[^]


另一个快速示例供您参考:



Another quick example for your reference:

protected void btnClose_Click(object sender, EventArgs e)
{
        //loop through the grid rows
        foreach (GridViewRow row in GridView1.Rows)
        {
               //assuming your are using BoundField column for displaying the Status field
               //change the value of index where your Status field resides in the grid's column
               //index starts at 0
               if(row.Cells[index].Text.Trim().ToLower() == "pending"){
                       ClientScript.RegisterStartupScript(typeOf(this), "alert", "alert('Found pending status.')", true);
                       break;
               } 
        }
}





要显示代码隐藏的确认信息,那么你可以参考此示例此处


这篇关于在按钮单击ASP.NET上检查gridview中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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