当前位置: 代码迷 >> .NET相关 >> gridView取得每行的值
  详细解决方案

gridView取得每行的值

热度:126   发布时间:2016-04-24 02:33:43.0
gridView获得每行的值

前台代码:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" EmptyDataText="暂无试题" Width="100%" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False">                            <Columns>                                <asp:TemplateField>                                    <HeaderTemplate>                                                                                <asp:Label id="Label24" runat="server" Text="一、单选题(每题1分)">                                                    </asp:Label>                                    </HeaderTemplate>                                    <ItemTemplate>                                        <table id="Table2" cellSpacing="1" cellPadding="1" width="100%" align="center" border="0">                                            <tr>                                                <td colSpan="4">                                                    <asp:Label id="Label1" runat="server" Text='<%# Container.DataItemIndex+1 %>'>                                                    </asp:Label>                                                    <asp:Label id="Label2" runat="server" Text='<%# Eval("content","、{0}") %>'>                                                    </asp:Label>                                                    <asp:Label id="Label3" runat="server" Text='<%# Eval("answer") %>' Visible="False">                                                    </asp:Label>                                                    <asp:Label id="Label4" runat="server" Text='<%# Eval("ID") %>' Visible="False">                                                    </asp:Label>                                                    </td>                                            </tr>                                            <tr>                                                <td width="35%">                                                    <asp:RadioButton id="rbA" runat="server" Text='<%# Eval("answerA") %>' GroupName="Sl">                                                    </asp:RadioButton></td>                                                <td width="35%">                                                    <asp:RadioButton id="rbB" runat="server" Text='<%# Eval("answerB") %>' GroupName="Sl">                                                    </asp:RadioButton></td>                                                <td></td>                                            </tr>                                            <tr>                                                <td width="35%">                                                    <asp:RadioButton id="rbC" runat="server" Text='<%# Eval("answerC") %>' GroupName="Sl">                                                    </asp:RadioButton></td>                                                <td width="35%">                                                    <asp:RadioButton id="rbD" runat="server" Text='<%# Eval("answerD") %>' GroupName="Sl">                                                    </asp:RadioButton></td>                                                <td></td>                                            </tr>                                        </table>                                    </ItemTemplate>                                </asp:TemplateField>                            </Columns>                            <HeaderStyle Font-Size="12pt" HorizontalAlign="Left" />                        </asp:GridView>

后台代码:

遍历gridview的每一行,取得RadioButton的值。

 /// <summary>    /// 提交按钮    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void btnCommit_Click(object sender, EventArgs e)    {               ItemOperater io = new ItemOperater(Session);                int singlemark = 1;//取出单选题的每题分值        foreach (GridViewRow dr in GridView1.Rows)//对单选题每题进行判断用户选择答案        {            SelectedItem si = new SelectedItem();            string str = "";            if (((RadioButton)dr.FindControl("rbA")).Checked)            {                str = "A";                            }            else if (((RadioButton)dr.FindControl("rbB")).Checked)            {                str = "B";            }            else if (((RadioButton)dr.FindControl("rbC")).Checked)            {                str = "C";            }            else if (((RadioButton)dr.FindControl("rbD")).Checked)            {                str = "D";            }            if (((Label)dr.FindControl("Label3")).Text.Trim() == str)//将用户选择结果和答案进行比较            {                score = score + singlemark;            }            si.selected = str; //选中的答案            Label lbAnswer = (Label)dr.FindControl("Label3");            Label lbID = (Label)dr.FindControl("Label4");            si.answer = lbAnswer.Text.Trim();            si.ID = Convert.ToInt32(lbID.Text.Trim());            io.Add(si);        }        insert(io);  //加到数据库        lbScore.Text = score+"";    }    public void insert(ItemOperater io) {        int stu_id=-1;        if (Request["course_id"] != null)        {            course_id = Int32.Parse(Request["course_id"].ToString());        }        stu_id = Convert.ToInt32(Session["ID"].ToString());        io.InsertScore(stu_id, score, course_id);    }