如何在选中该复选框的gridview中获取值? [英] How to get the value in the gridview which the checkbox is checked ?

查看:112
本文介绍了如何在选中该复选框的gridview中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在选中该复选框的gridview中获取值
在我的gridview中,当我单击一行的复选框时,创建了一个复选框
单击按钮时需要显示选中的行值的第3列的值.
我编写的示例代码是:

How do I get the value in the gridview which the checkbox is checked
in my gridview one column I created checkboxs ,when i click that checkbox of a row
a need to display the value of 3rd column of the checked row value on button click.
sample code I written is:

protected void Button_Click(object sender, EventArgs e)
   {

       foreach (GridViewRow itemrow in GridView1.Rows)
       {


          CheckBox cbview = (CheckBox)(itemrow.Cells[0].FindControl("viewdata"));

        if (cbview.Checked)
         {

            // String s = row.Cells[2].Text;
           string value = GridView1.DataKeys[itemrow.RowIndex]["id"].ToString();
         Response.Write(value);
}
}
}

推荐答案

protected void Button_Click(object sender, EventArgs e)
{
 foreach (GridViewRow itemrow in GridView1.Rows)
 {
   CheckBox cbview = (CheckBox)(itemrow.Cells[0].FindControl("viewdata"));
   if (cbview.Checked)
   {

      string value = GetObjectType(itemrow.Cells[2].Controls[1]);
      Response.Write(value);
   }
 }

private String GetObjectType(Control ctrl)
    {
        switch (ctrl.GetType().Name)
        {
            case "Label":
                return ((Label)ctrl).Text;
            case "TextBox":
                return ((TextBox)ctrl).Text;
        }
        return "";
    }


在该行的复选框中选择一个隐藏字段
并将要访问的值存储在
后面的代码中
在您的代码后面找到隐藏的字段控件,然后...

take One hidden field with checkbox in that row
and store that value which you want to access in code behind

find that hidden field control in your code behind and...

if (cbview.Checked)
        {
              string value = hiddenfieldId.value;
        }




参见示例代码:

示例:
在您的客户代码中,您的网格中必须有一列TemplateField ...
Hi,

See sample code:

Example:
In your Client Code you have to have a column TemplateField in your Grid...
<asp:TemplateField HeaderStyle-HorizontalAlign="Center"HeaderText="View">
<itemtemplate>
   <asp:LinkButton ID="viewdata" runat="server" Text="View"
       OnClick="viewdata_Click">
</itemtemplate>




在您后面的代码中:




In your code behind:

protected void viewdata_Click(object sender, EventArgs e)
{
 LinkButton viewdata = sender as LinkButton;
 GridViewRow row = (GridViewRow)viewdata.NamingContainer;
 string column3 = row.Cells[2].Text.Trim();
}


在上面的示例中,网格上的第3列或单元格是 ClaimNo
变量column3应该为 ClaimNo 的值,因为列索引从0开始.


请记住,如果有帮助,请将其标记为答案;如果没有帮助,则将其取消标记.


希望这可以帮助...

问候,

代数


In above example 3rd column or cell on your grid is ClaimNo
The variable column3 should be the value of ClaimNo because the column index begins at 0.


Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Hope this could help...

Regards,

Algem


这篇关于如何在选中该复选框的gridview中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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