按下按钮时如何获取gridview checkboxfield值? [英] How do I get the gridview checkboxfield value when I press a button?
问题描述
我有一个带有checkboxfield列的gridview,但是当我按下asp.net按钮时,由于回发,所有复选框都被检错。
当我用C#单击我的codeBehind中的按钮时,我想获取复选框值。
是否可以?
解决方案:
我的错误是使用gridview中的checkboxfield列,但最好在本文中使用templatefield作为solution1。
错误:
<前lang =HTML> < asp:CheckBoxField DataField = ESTPROopc HeaderText = OPC / >
解决方案:此帖子中的SOLUTION1是正确。
< asp:TemplateField HeaderText = OPC >
< ItemTemplate >
< asp:CheckBox ID = ESTPROopc runat = server < span class =code-attribute>已选中 =' <% #Eval( ESTPROopc)%>' / >
< / ItemTemplate >
< / asp:TemplateField >
bool ischecked =((CheckBox) row.FindControl( ESTPROopc))。已检查;
我尝试过:
CheckBox chkOpc = row.Cells [ 4 ]。控件[ 0 ] as CheckBox;
bool check = chkOpc.Checked;
请检查下面的代码,我已经测试过了。
ASPX代码:
< asp:GridView ID = GridView1 runat = server AutoGenerateColumns = 错误 >
< ; 列 >
< asp:BoundField DataField = 标题 HeaderText = 标题 / >
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID = chk runat = server 已选中 =' <% #Eval( CheckValue)%> ;' / >
< / ItemTemplate >
< / asp:TemplateField >
< / Co lumns >
< / asp:GridView >
< asp:Button ID = btnGetCheck runat = server 文本 = 获取检查 OnClick = btnGetCheck_Click / >
ASPX.CS代码:
class CheckExample
{
public string 标题{ get ; set ; }
public bool CheckValue { get 跨度>; set ; }
}
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
List< ; CheckExample> list = new 列表< CheckExample>();
list.Add( new CheckExample(){Title = A,CheckValue = false });
list.Add( new CheckExample(){Title = B,CheckValue = false });
list.Add( new CheckExample(){Title = C,CheckValue = false });
list.Add( new CheckExample(){Title = D,CheckValue = false });
list.Add( new CheckExample(){Title = E,CheckValue = false });
GridView1.DataSource = list;
GridView1.DataBind();
}
}
受保护 void btnGetCheck_Click( object sender,EventArgs e)
{
foreach (GridViewRow row GridView1.Rows)
{
bool chkValue =((CheckBox)row.FindControl( chk))。已检查;
}
}
I have a gridview with a "checkboxfield" column, but when I press the asp.net button, all the checkboxes are checked in false due to postback.
I want to get the checkboxes values when I click the button in my codeBehind with C#.
Is it possible?
SOLUTION:
My error was to use the "checkboxfield" column in gridview but is better to use the "templatefield" as the solution1 in this post.
ERROR:
<asp:CheckBoxField DataField="ESTPROopc" HeaderText="OPC"/>
SOLUTION: SOLUTION1 in this post is right.
<asp:TemplateField HeaderText="OPC">
<ItemTemplate>
<asp:CheckBox ID="ESTPROopc" runat="server" Checked='<%#Eval("ESTPROopc")%>'/>
</ItemTemplate>
</asp:TemplateField>
bool ischecked = ((CheckBox)row.FindControl("ESTPROopc")).Checked;
What I have tried:
CheckBox chkOpc = row.Cells[4].Controls[0] as CheckBox;
bool check = chkOpc.Checked;
Please check below code, I've tested it.
ASPX CODE:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Title" HeaderText="Title" /> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chk" runat="server" Checked='<%#Eval("CheckValue") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID="btnGetCheck" runat="server" Text="Get Check" OnClick="btnGetCheck_Click" />
ASPX.CS CODE:
class CheckExample { public string Title { get; set; } public bool CheckValue { get; set; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<CheckExample> list = new List<CheckExample>(); list.Add(new CheckExample() { Title = "A", CheckValue = false }); list.Add(new CheckExample() { Title = "B", CheckValue = false }); list.Add(new CheckExample() { Title = "C", CheckValue = false }); list.Add(new CheckExample() { Title = "D", CheckValue = false }); list.Add(new CheckExample() { Title = "E", CheckValue = false }); GridView1.DataSource = list; GridView1.DataBind(); } } protected void btnGetCheck_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { bool chkValue = ((CheckBox)row.FindControl("chk")).Checked; } }
这篇关于按下按钮时如何获取gridview checkboxfield值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!