gridView获得每行的值

2018-06-17 19:25:44来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

前台代码:

<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);
    }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C#中部分方法返回值类型为什么只能是void?

下一篇:《c# 从入门经典》 (第6版) - c# 简介