ASP.Net Gridview中的复选框 [英] Checkboxes in ASP.Net Gridview

查看:231
本文介绍了ASP.Net Gridview中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是在我的asp.net页面上有一个gridview,它在第一列中有一个复选框,在网格下面有一个按钮。现在我将网格数据绑定到一个只包含三个属性的表,即ID,Name和Email。现在我想要做的是,当单击按钮时,应该通过相应的选中复选框的名称在文件夹中创建文件。

这是我到目前为止所做的,但代码的作用是即使选中了一个复选框,它也会遍历gridview并制作一些文件。以下是代码:

Hi, What I want to do is to have a gridview on my asp.net page that has checkboxes in the first column and below the grid there is to be a button. Now I databind the grid to a table which contains only three attributes namely ID, Name and Email. Now what I want to do is that when the button is clicked files should be created in a folder by the names of the corresponding checked checkboxes.
Here is what I have so far, but what the code does is that even if a single checkbox is checked it will loop through the gridview and make some files. Here is the code:

foreach (GridViewRow row in GridView1.Rows)
           {
               // Access the CheckBox
               CheckBox cb = (CheckBox)row.FindControl("StatusUpdate");
               if (cb.Checked || cb != null)
               {
                   //int ID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                   int index = row.RowIndex;
                   string name = GridView1.Rows[index].Cells[1].Text;
                   File.Create("D:\\"+name+".txt");
               }
               else
               {
               }
           }
           File.AppendAllText(@"E:\Try.text", "No box checked" + Environment.NewLine);

推荐答案

add onclick功能如



<checkboxönclick=checkme(this)>

功能检查()

{

if(this.check == true)

{



//取一个隐藏字段

hiddenfield.value = this.id;

}

}



在隐藏字段中隐藏字段valuechanged =hidden_​​changed



现在在c#



protected void hidden_​​changed

{

string aa = hiddenfieldid.value

//这里你得到复选框的id



}

你只会获得复选复选框的文件

这只是逻辑
add onclick function like

<checkbox önclick="checkme(this)">
function checkme()
{
if(this.check==true)
{

//take one hiddenfield
hiddenfield.value=this.id;
}
}

and in hidden field on hiddenfield valuechanged="hidden_changed"

now in c#

protected void hidden_changed
{
string aa= hiddenfieldid.value
//here you get id of checkbox checked

}
and you will get files for checked checkbox only
this is logic only


创建文件是因为你的if条件总是评估为True。



Files are created because your if condition evaluates to True always.

if (cb.Checked || cb != null)





即使未选中复选框,因为已应用||,此条件为真(或)找到复选框,即它不为空。



只需更改||和&&这将确保您的代码仅在找到复选框并且也被选中时创建文件!



This condition is true even if the checkbox is not checked because you have applied || (or) and the checkbox is found ie its not null.

Simply change || to && that will make sure your code creates file only if "checkbox is found and is checked too"!


这篇关于ASP.Net Gridview中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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