一个gridview中的绑定和访问复选框值 [英] Bind and access checkbox value within a gridview

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

问题描述

在我的网格视图我有一个复选框列,和我结合了字典中的gridview的。我需要从检查盒送相应的编号

在我的字典里我已经键值像

  ID名称
- ----
 1艺术
 2科学
 3工程

在这里,我试图为

此复选框值绑定

 < ASP:的TemplateField>
    <&ItemTemplate中GT;
      < ASP:复选框ID =chkSelItemVALUE =<%#的eval(Key.Id)%>中
                                                       =服务器/>
    < / ItemTemplate中>
< / ASP:的TemplateField>< ASP:的TemplateField的HeaderText =部门>
    <&ItemTemplate中GT;
         <%#的eval(Key.Name)%>
    < / ItemTemplate中>
    < HeaderStyle Horizo​​ntalAlign =左/>
< / ASP:的TemplateField>

和从codebehind,我试着像

 的foreach(在gridDepartments.Rows GridViewRow行)
        {
            复选框chkSelItem =(复选框)row.FindControl(chkSelItem);            如果(chkSelItem.Checked)
            {
                INT =的DepartmentID int.Parse(chkSelItem.Text);
                ////
                ////
            }
        }

其投掷的错误,或不显示的复选框的任意值。

我也TRID与的FindControl ,但没有使用它,因为在键和放大器;值对正在使用类(继承另一个类)和一个布尔值。那是什么我想是这样,任何人都可以帮助我在这里,在此先感谢.....


解决方案

 你可以试试这个...绑定ID,以标贴,而不是为下面的复选框。< ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:复选框ID =chkSelItem=服务器/>< ASP:标签ID =lblSelectedItem值=<%#的eval(Key.Id))%GT;可见=FALSE/>
< / ItemTemplate中> < / ASP:的TemplateField>在codebehind试试这个的foreach(在gridDepartments.Rows GridViewRow行)
{
      复选框chkSelItem =(复选框)row.FindControl(chkSelItem);
       标签lblSelectedItem =(标签)row.FindControl(lblSelectedItem);      如果(chkSelItem.Checked)
      {
                 INT =的DepartmentID int.Parse(lblSelectedItem.Text);
      }
}希望这是ü想要什么?

In my grid view I've a checkbox column, and am binding the gridview with a dictionary. I need to get the corresponding Id from the checked boxes.

In my dictionary I've the key values like

Id         Name
--         ----
 1         Arts
 2         Science
 3         Engineering

Here, I tried to bind the value for this checkbox as

<asp:TemplateField>
    <ItemTemplate>
      <asp:CheckBox ID="chkSelItem" value="<%# Eval("Key.Id") %>" 
                                                       runat="server" />
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Department">
    <ItemTemplate>
         <%# Eval("Key.Name") %>
    </ItemTemplate>
    <HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>

and from the codebehind, I tried like

foreach (GridViewRow row in gridDepartments.Rows)
        {
            CheckBox chkSelItem = (CheckBox)row.FindControl("chkSelItem");

            if (chkSelItem.Checked)
            {
                int departmentId = int.Parse(chkSelItem.Text);
                ////
                ////                 
            }
        }

its throwing error, or not showing any value for the checkbox.

I also trid with FindControl, but no use of it, coz in the key & Value pair am using a class(which inherits another class) and a bool. thats what am trying like this, can anyone help me here, thanks in advance.....

解决方案

You can try this one...

Bind id to lable instead of to checkbox as below.

<asp:TemplateField>     
<ItemTemplate>       
<asp:CheckBox ID="chkSelItem" runat="server" />     

<asp:Label ID="lblSelectedItem" value=<%# Eval("Key.Id")) %> visible="False"/>
</ItemTemplate> </asp:TemplateField>



In codebehind try this

foreach (GridViewRow row in gridDepartments.Rows)         
{             
      CheckBox chkSelItem = (CheckBox)row.FindControl("chkSelItem");
       Label lblSelectedItem= (Label)row.FindControl("lblSelectedItem");

      if (chkSelItem.Checked) 
      {
                 int departmentId = int.Parse(lblSelectedItem.Text); 
      }
} 



Hope this is what u want...

这篇关于一个gridview中的绑定和访问复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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