在asp.net中找不到id gridview [英] not finding id gridview in asp.net

查看:105
本文介绍了在asp.net中找不到id gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在使用此代码从网格视图中匹配id中的值.但是我遇到一个错误(索引超出范围.必须为非负数并且小于集合的大小.
参数名称:索引)此处的mu code复选框正在运行,但我没有获得id的行的ID.请帮助我,我要去哪里了.如果您在理解中遇到问题,请别生气,我的英语不太好.


Hello friends,

I am using this code to gatch the value in id from a grid view.but i am getting an error(Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index)Here in mu code check box is working but i not getting the value id the id of the row.please help me out , where i am going wrong.And plese do not be angry if you are getting problem in understanding, my english is not so good.


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

            BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" 

            CellPadding="3" CellSpacing="1" GridLines="None">
            <footerstyle backcolor="#C6C3C6" forecolor="Black" />
            <rowstyle backcolor="#DEDFDE" forecolor="Black" />
            <columns>
            <asp:TemplateField HeaderText="Select">
                                <itemtemplate>
                                    <asp:CheckBox ID="chk" runat="server" />
                                </itemtemplate>
            
                <asp:BoundField HeaderText="id" DataField="id" />
                
                <asp:BoundField HeaderText="Question" DataField="Question" />
                
                <asp:BoundField DataField="Option1" HeaderText="Option1" />
                
                <asp:BoundField DataField="Option2" HeaderText="Option2" />
                <asp:BoundField DataField="Option3" HeaderText="Option3" />
                <asp:BoundField DataField="Option4" HeaderText="Option4" />
                <asp:BoundField DataField="Correct" HeaderText="Answer" />
            </columns>
            <pagerstyle backcolor="#C6C3C6" forecolor="Black" horizontalalign="Right" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />


Dfault.aspx.cs代码


Dfault.aspx.cs code

 for (int num = 0; num &lt; i; num++)
       {

     CheckBox chk_box = (CheckBox)GridView1.Rows[num].FindControl("chk");
     string ques_id=GridView1.DataKeys[num].Values["id"].ToString();
           if (chk_box.Checked == true)
           {
            // code
           }
}

</pre>

推荐答案

看起来您要迭代的次数比网格视图中的行数还多.改为使用foreach ,不会出现问题.
Looks like you are iterating more times then number of rows in grid view. Use foreach instead and the problem should not come.


将绑定字段更改为< asp:templatefield xmlns:asp =#unknown">并在项目模板中标记.之后,请在您的CS页面上尝试此操作...
change your bound field to <asp:templatefield xmlns:asp="#unknown"> and label in item template.after that try this on your cs page...
foreach (GridViewRow gr in Gridview1.Rows)
       {
           CheckBox chk = (CheckBox)gr.FindControl("CheckBox1");
           if (chk.Checked == true)
           {
               Label lbl = (Label)gr.FindControl("label1"); // id of label from which tou want to fetch values
            //code
           }
       }</asp:templatefield>


这篇关于在asp.net中找不到id gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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