如何在每一行中获取复选框的值? [英] How to get value of checkbox in each row?

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

问题描述

这是UI

< asp:GridView ID =   gvSecurity 
runat = server
AutoGenerateColumns = False CellPadding = 3
OnPageIndexChanging = gvSecurity_PageIndexChanging
OnRowDeleting = gvSecurity_RowDeleting
onrowcommand = gvSecurity_RowCommand
OnSelectedIndexChanged = < span class =code-string> gvSecurity_SelectedIndexChanged
AllowPaging = True
Horizo​​ntalAlign = 对齐
Width = 100%

CssClass = avoGrid
PagerStyle-CssClass = avoGridPgr
AlternatingRowStyle-CssClass = avoGridAlt AllowSorting = True >

< ; AlternatingRowStyle CssClass = avoGridAlt > < / AlternatingRowStyle >

< Columns>
< asp:BoundField DataField = szSecurityItemId ItemStyle-Horizo​​ntalAlign = Left
HeaderStyle-Horizo​​ntalAlign = HeaderText = 安全项目 >
< HeaderStyle Horizo​​ntalAlign = > < / HeaderStyle >
< ItemStyle Horizo​​ntalAlign = Left > < / ItemStyle >
< / asp:BoundField >
< asp:BoundField DataField = szDescription ItemStyle-Horizo​​ntalAlign =
HeaderStyle-Horizo​​ntalAlign = Left HeaderText = Deskripsi >
< HeaderStyle Horizo​​ntalAlign = Left > < / HeaderStyle > ;
< ItemStyle Horizo​​ntalAlign = > < / ItemStyle >
< / asp:BoundField >
< asp:templatefield HeaderText = >
< itemtemplate>
< asp:CheckBox ID = ckbValue runat = server oncheckedchanged = gvSecurity_SelectedIndexChanged CssClass = pkchkbox Checked = ' <%#Eval(szValue)== DBNull.Value? false:Convert.ToBoolean(Eval(szValue))%>' > < / asp:CheckBox >
< / itemtemplate >
< / asp:templatefield >
< / >
< ; PagerStyle CssClass = avoGridPgr > < / PagerStyle >
< / asp:GridView >



这是CodeProgram < pre lang =c#> private DataTable m_dt;
int index;
CheckBox chk = null ;

foreach (DataRow dr in m_dt.Rows)
{
string szValue =( string )dr [ szValue];
string szGroupId =( string )dr [ szSecurityItemId];
item = new SecurityItemData();

for int i = 0 ; i < gvSecurity.Rows.Count; i ++)
{
chk =(CheckBox)gvSecurity.Rows [ i] .FindControl( ckbValue);


item.szSecurityItemId =( string )dr [ szSecurityItemId];
if (szValue.ToLower()== true
{
if (chk.Checked)
{
item.szValue = N;
}
else
{
item.szValue = Y;
}

}
else
{
if (chk.Checked)
{
item.szValue = Y;
}
else
{
item.szValue = N;
}
} // item.szValue = bVal? Y:N;
}
dat.itemList.Add(item);
}
return dat;
}

受保护 void gvSecurity_SelectedIndexChanged( object sender,EventArgs e)
{
// 使用SelectedRow属性获取当前选定的行。
GridViewRow row =((GridViewRow)((CheckBox)sender).NamingContainer);
index = row.RowIndex;
}



请帮助我,因为当我保存复选框的值时,对应于所选的复选框。

解决方案

试试这样..



  foreach (GridViewRow row  in  gvSecurity.Rows)
{
CheckBox chk = row.FindControl( ckbValue as CheckBox;
// 否则CheckBox chk =(CheckBox)row.FindControl(ckbValue);

if (chk!= null
{
if (chk.Checked)
{
item.szValue = Y;
}
else
{
item.szValue = N;
}
}

}


在您的代码中,您有以下片段:

  private  DataTable m_dt; 
int index;
CheckBox chk = null ;

foreach (DataRow dr in m_dt.Rows)






因为你没有设置m_dt你没有数据(事实上,你没有m_dt所以它应该以对象未设置为实例错误而失败。当然,你的每一个都不会触发,其中的所有代码都不会执行。



所以,删除每个外部它应该工作。





如果这有帮助请花时间接受解决方案。谢谢。


在此行之前:

 chk =(CheckBox)gvSecurity.Rows [i] .FindControl(  ckbValue); 



如果添加以下内容声明,

  if (gvSecurity.Rows [i] .RowType == DataControlRowType.DataRow)





如果问题没有解决,那么问题不在您展示的代码中



此外,什么是 dat obje ct你回来了,你在哪里使用它?


This is UI

<asp:GridView ID="gvSecurity" 
    runat="server" 
    AutoGenerateColumns="False" CellPadding="3" 
    OnPageIndexChanging="gvSecurity_PageIndexChanging"
    OnRowDeleting="gvSecurity_RowDeleting"
    onrowcommand="gvSecurity_RowCommand" 
    OnSelectedIndexChanged="gvSecurity_SelectedIndexChanged"
    AllowPaging="True"  
    HorizontalAlign="Justify" 
    Width="100%"
                    
    CssClass="avoGrid"
    PagerStyle-CssClass="avoGridPgr"
    AlternatingRowStyle-CssClass="avoGridAlt" AllowSorting="True">

    <AlternatingRowStyle CssClass="avoGridAlt"></AlternatingRowStyle>

        <Columns>                                                     
            <asp:BoundField DataField="szSecurityItemId" ItemStyle-HorizontalAlign="Left"  
                HeaderStyle-HorizontalAlign="Left" HeaderText="Security Item">                        
                <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                <ItemStyle HorizontalAlign="Left"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="szDescription" ItemStyle-HorizontalAlign="Left"  
                HeaderStyle-HorizontalAlign="Left"  HeaderText="Deskripsi">
                <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                <ItemStyle HorizontalAlign="Left"></ItemStyle>
            </asp:BoundField> 
            <asp:templatefield HeaderText="Value">
                <itemtemplate>
                    <asp:CheckBox ID="ckbValue" runat="server" oncheckedchanged="gvSecurity_SelectedIndexChanged" CssClass="pkchkbox" Checked='<%# Eval("szValue") == DBNull.Value ? false : Convert.ToBoolean(Eval("szValue")) %>'></asp:CheckBox>
                </itemtemplate>
            </asp:templatefield>
        </Columns>        
    <PagerStyle CssClass="avoGridPgr"></PagerStyle>
</asp:GridView>


This is CodeProgram

private DataTable m_dt;
int index;
CheckBox chk = null;

 foreach (DataRow dr in m_dt.Rows)
            {
                string szValue = (string)dr["szValue"];
                string szGroupId = (string)dr["szSecurityItemId"];
                item = new SecurityItemData();
                
                for (int i = 0; i < gvSecurity.Rows.Count; i++)
                {
                    chk = (CheckBox)gvSecurity.Rows[i].FindControl("ckbValue");
                    
                    
                    item.szSecurityItemId = (string)dr["szSecurityItemId"];
                    if (szValue.ToLower() == "true")
                    {
                        if (chk.Checked)
                        {
                            item.szValue = "N";
                        }
                        else
                        {
                            item.szValue = "Y";
                        }
                        
                    }
                    else
                    {
                        if (chk.Checked)
                        {
                            item.szValue = "Y";
                        }
                        else
                        {
                            item.szValue = "N";
                        }
                    }                //item.szValue = bVal ? "Y" : "N";
                }
                dat.itemList.Add(item);
            }
            return dat;
        }

  protected void gvSecurity_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the currently selected row using the SelectedRow property.
            GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
            index = row.RowIndex;
}


Please help me, because when i save value of checkbox doesn't not correspond to the selected checkboxes.

解决方案

Try like this..

foreach (GridViewRow row in gvSecurity.Rows)
{
  CheckBox chk= row.FindControl("ckbValue") as CheckBox ;
//else CheckBox  chk=(CheckBox)row.FindControl("ckbValue");

if(chk!=null)
{
if (chk.Checked)
   {
    item.szValue = "Y";
   }
  else
   {
    item.szValue = "N";
   }
}

}


In your code you have the following fragment:

private DataTable m_dt;
int index;
CheckBox chk = null;
 
 foreach (DataRow dr in m_dt.Rows)
.
.
.



Since you're not setting m_dt you have no data (in fact, you don't have m_dt so it should fail with "object not set to an instance" error. Definitely, your for each here will NOT trigger and all the code inside it will never execute.

So, remove the outer for each and it should work.


If this helps please take time to accept the solution. Thank you.


Before this line:

chk = (CheckBox)gvSecurity.Rows[i].FindControl("ckbValue");


add the following if statement,

if (gvSecurity.Rows[i].RowType == DataControlRowType.DataRow)



If the problem is not solved, then the issue is not in the code you displayed

Also, what is the dat object your are returning, where are you using it?


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

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