从gridview获取C#get user selected复选框 [英] C# get user selected checkbox from gridview

查看:57
本文介绍了从gridview获取C#get user selected复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
{
    DataTable question_x = new DataTable("Question");
    question_x.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
    question_x.Columns.Add(new DataColumn("Name"));
    question_x.Rows.Add(1, "1) Training deliver can be understood?");
    question_x.Rows.Add(1, "2) The way instructor deliver the training is satisfaction?");
    question_x.Rows.Add(1, "3) The notes are easy to understand?");
    question_x.Rows.Add(1, "4) The training room is appropriate?");
    question_x.Rows.Add(1, "5) The training time is satisfactory?");
    gvEdit.DataSource = question_x;
    gvEdit.DataBind();

}




    <asp:GridView runat="server" CssClass="table table-striped table-bordered table-hover" ID="gvEdit" DataKeyNames="ID" AutoGenerateColumns="false" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Question" ItemStyle-ForeColor="White" ItemStyle-HorizontalAlign="Left" ItemStyle-BackColor="graytext" />
        <asp:TemplateField HeaderText="Poor">
            <ItemStyle HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:RadioButton runat="server" ID="chkP" onclick="javascript:GridSelectAllColumn(this, 'chkPoor');" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Good">
            <ItemStyle HorizontalAlign="Center" />
            <ItemTemplate>
                <asp:RadioButton runat="server" ID="chkG" onclick="javascript:GridSelectAllColumn(this, 'chkGood');" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>







i在我的gridview中几乎没有调查问题,用户只能为每个问题选择一个选项。我有困难在c#中获取用户选择的狂热,请指导我这个感谢..我想点击提交按钮后输出这样的东西:



1. checkP

2. checkG

3. checkG

4. checkG

5. checkP



我尝试了什么:



请提前帮助,谢谢。




i have few survey question in my gridview,user can select one selection only for each question.I have difficulty in getting user selection in c#,please guide me on this thanks.. i want to have output something like this after i click submit button:

1. checkP
2. checkG
3. checkG
4. checkG
5. checkP

What I have tried:

please help,thanks in advance.

推荐答案

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chk = new CheckBox();
        chk.EnableViewState = true;
        chk.Enabled = true;
        chk.ID = "chkb";
        e.Row.Cells[1].Controls.Add(chk);
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var chk = (CheckBox)e.Row.FindControl("chkb");
        // databind it here according to the DataSource in e.Row.DataItem
    }
}


从网格视图中获取值时,您已遍历网格视图的每一行,因为

While getting the value from grid view you have loop through each row of Grid view as
foreach(GridViewRow row in GridView2.Rows)
{
    for(int i = 0; i < GridView2.Columns.Count; i++)
    {
        string header = GridView2.Columns[i].HeaderText;
        CheckBox chkp=(Checkbox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("chkp");
        CheckBox chkg=(Checkbox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("chkg");
       //Get the .checked values of chkp and chkg show it as you desire
       string Ans= chkp.checked==True?"CheckP":chkg.checked==True?"CheckG":"No Answer"
    }
}





代码可能无法编译,因为未经过测试。



Code might not compile as its not tested.


试试这个





try this


<head>
    <script src="jquery.js"></script>
    <script>

        function GridSelectAllColumn(obj, val) {
            var


这篇关于从gridview获取C#get user selected复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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