如何从Chechbox.checked的GridView的获得标识? [英] How to get the Id from Gridview of Chechbox.checked?

查看:195
本文介绍了如何从Chechbox.checked的GridView的获得标识?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有GridView和一个按钮,如下所示。然后我绑定从我的数据库中的数据在GridView。 GridView控件有两个hiddenfield为Id和ClassIndex。
当我选择一个复选框并单击按钮,我希望得到相应的ID和文件名。

I have GridView and a button as follows. Then i am binding the gridview with data from my database. GridView has two hiddenfield for Id and ClassIndex. when i selecting a checkbox and click button, i want to get the corresponding Id and FileName.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
         <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="check" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HiddenField ID="hdfId" runat ="server" Value='<%#Eval("Id") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HiddenField ID="hdfClssIndex" runat ="server" Value='<%#Eval("ClassIndex") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblFileName" runat ="server" Text='<%#Eval("FileName") %>' />
                </ItemTemplate>
            </asp:TemplateField>
         </Columns>
    </asp:GridView>

和按钮类似于

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="Send Request" />

背后的按钮,code是

the code behind button is

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
       var check = row.FindControl("check") as CheckBox;
       if (check.Checked)
       {
         int Id = Convert.ToInt32(row.Cells[1].Text);
         //some logic follws here
       }
    }
}

但我得到一个错误像

but i am getting an error like

输入字符串的不正确的格式。
什么是错误,怎么解决呢?

Input string was not in a correct format. What is the error and how to solve it?

推荐答案

您正确的循环。

但你忘了这里要注意一件事,当你想要访问复选框你做的的FindControl 。这意味着你正在努力寻找该行一些控制。

But you forgot to notice one thing here, when you wanted to access CheckBox you did a FindControl on row. Which means you are trying to find some control in that row.

那你为什么访问内部排 HiddenField 控制与 row.Cell [1]。文本

尝试找到谓也。

Then why are you accessing HiddenField control inside row with row.Cell[1].Text?
Try to find that also.

INT n = Convert.ToInt32(((HiddenField)row.FindControl(hdfId))值);

这篇关于如何从Chechbox.checked的GridView的获得标识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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