在一个菜单中实现像下图的功能最后会有一个button提交结果
那位能帮帮我?

解决方案 »

  1.   

       for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox cb = (checkbox)GridView.Rows[i].Cell[0].FindControl("CheckboxID");//这里是checkbox的ID
                    if (cb.Checked == true)
                    {
                        //取出点菜的ID 存入数组
                    }
                    else
                    {
                        //不做任何操作
                    }
                }
                //把数组取出 执行操作~
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {    }
    }
    敢问如何在这段代码中写?
      

  3.   

      for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox cb = (checkbox)GridView.Rows[i].Cell[0].FindControl("CheckboxID");//这里是checkbox的ID
                    if (cb.Checked == true)
                    {
                        //取出点菜的ID 存入数组
                    }
                    else
                    {
                        //不做任何操作
                    }
                }
     循环出来
      

  4.   


     checkbox改成CheckBox 
      

  5.   

    前半部分的给你一个参考:
    http://www.cnblogs.com/chenping-987123/archive/2010/12/06/1897796.html
      

  6.   

    后面的GridView是什么一回事呢?
      

  7.   

    呵呵,我公司有这样的,但是方法比较复杂
    对dataGrid进行了继承重写吧...我还菜鸟,看不太懂
      

  8.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script src="JS/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">    <script type="text/javascript">
            function checkJs(IsCheck) {
                if (IsCheck) {
                    $("#gv_info tr:gt(0)").css("background", "blue");
                    $("#gv_info tr:gt(0)").each(function() {
                        $(this).find(":checkbox").attr("checked", true);
                    })
                } else {
                    $("#gv_info tr").css("background", "");
                    $("#gv_info tr:gt(0)").each(function() {
                        $(this).find(":checkbox").attr("checked", false);
                    })
                }
            }        $(function() {
                var flag = 0;
                $("#gv_info tr:gt(0)").click(function() {
                    flag = 0;
                    if ($(this).css("background") == "blue") {
                        $(this).css("background", "");
                        $(this).find(":checkbox").attr("checked", false);
                    } else {
                        $(this).css("background", "blue");
                        $(this).find(":checkbox").attr("checked", true);
                    }
                    $("#gv_info tr:gt(0)").each(function() {
                        if ($(this).css("background") != "blue") {
                            flag = flag + 1;
                        }
                    })
                    if (flag == 0) {
                        $("#gv_info tr:eq(0)").find(":checkbox").attr("checked", true);
                    } else {
                        $("#gv_info tr:eq(0)").find(":checkbox").attr("checked", false);
                    }
                })
               
            })
            
            
            
            
            
            
        </script>    <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="div" align="center">
            <asp:GridView ID="gv_info" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <input type='checkbox' id='chkAll' name='chkAll' onclick='checkJs(this.checked);' />
                        </HeaderTemplate>
                        <ItemStyle HorizontalAlign="Center" Width="60px" />
                        <ItemTemplate>
                            <input type="checkbox" id="chkSelect" name="chkSelect" value='<%# Eval("Col")  %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="id">
                        <ItemTemplate>
                            <asp:Label ID="Label1" Text='<% #Eval("Col") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="name">
                        <ItemTemplate>
                            <asp:Label ID="Label2" Text='<% #Eval("PID") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        </form>
    </body>
    </html> protected void Page_Load(object sender, EventArgs e)
        {
            string constring = "Data Source=.;Initial Catalog=Test;User ID=sa;Password=";
            SqlConnection con = new SqlConnection(constring);
            string selectstring="SELECT top 10 * FROM  tb";
            SqlDataAdapter sds = new SqlDataAdapter(selectstring, con);
            DataSet ds = new DataSet();
            sds.Fill(ds);
            gv_info.DataSource = ds.Tables[0];
            gv_info.DataBind();
        }好久没用girdview 正好没事干,最近也在学JQ。写个玩下.取值方法在each里处理下. 放到隐藏控件应该能实现你的功能
      

  9.   

    干脆直接编个.cs发上来算了,没学过JavaScript,看得眼都花了
      

  10.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script src="JS/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">    <script type="text/javascript">
            var allID = "";
            function checkJs(IsCheck) {
                allID = ""
                if (IsCheck) {
                    $("#gv_info tr:gt(0)").css("background", "blue");
                    $("#gv_info tr:gt(0)").each(function() {
                        $(this).find(":checkbox").attr("checked", true);
                        allID += $(this).find(":checkbox").val()+",";
                    })
                } else {
                    $("#gv_info tr").css("background", "");
                    $("#gv_info tr:gt(0)").each(function() {
                        $(this).find(":checkbox").attr("checked", false);
                        allID = "";
                    })
                    $("#hdID").val(allID);
                }
               
            }        $(function() {
                var flag = 0;
                $("#gv_info tr:gt(0)").click(function() {
                    flag = 0;
                    var thirdRe = "";
                    thirdRe = $(this).find(":checkbox").val() + ",";
                    alert(thirdRe);
                    if ($(this).css("background") == "blue") {
                        $(this).css("background", "");
                        $(this).find(":checkbox").attr("checked", false);                    allID = allID.replace(thirdRe, '');
                    } else {
                        $(this).css("background", "blue");
                        $(this).find(":checkbox").attr("checked", true);
                        allID += thirdRe;
                    }
                    $("#gv_info tr:gt(0)").each(function() {
                        if ($(this).css("background") != "blue") {
                            flag = flag + 1;
                        }
                    })
                    if (flag == 0) {
                        $("#gv_info tr:eq(0)").find(":checkbox").attr("checked", true);
                    } else {
                        $("#gv_info tr:eq(0)").find(":checkbox").attr("checked", false);
                    }
                    $("#hdID").val(allID);
                })        })
            
            
            
            
            
            
        </script>    <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="div" align="center">
            <asp:HiddenField ID="hdID" runat="Server" />
            <asp:GridView ID="gv_info" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <input type='checkbox' id='chkAll' name='chkAll' onclick='checkJs(this.checked);' />
                        </HeaderTemplate>
                        <ItemStyle HorizontalAlign="Center" Width="60px" />
                        <ItemTemplate>
                            <input type="checkbox" id="chkSelect" name="chkSelect" value='<%# Eval("Col")  %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="id">
                        <ItemTemplate>
                            <asp:Label ID="Label1" Text='<% #Eval("Col") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="name">
                        <ItemTemplate>
                            <asp:Label ID="Label2" Text='<% #Eval("PID") %>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            <br />
        </div>
        </form>
    </body>
    </html>using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string constring = "Data Source=.;Initial Catalog=Test;User ID=sa;Password=";
            SqlConnection con = new SqlConnection(constring);
            string selectstring="SELECT top 10 * FROM  tb";
            SqlDataAdapter sds = new SqlDataAdapter(selectstring, con);
            DataSet ds = new DataSet();
            sds.Fill(ds);
            gv_info.DataSource = ds.Tables[0];
            gv_info.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string a = hdID.Value;
        }
    }
    string a 就是 所有菜的 ID ,以" , " 隔开的形式存进去的, 在后台 用split(',') 处理下,循环 就可以了。
      

  11.   


    实例:GridView实现CheckBox的多选或单选,并根据所选择的行进行数据操作
      

  12.   

    问下:那个 gv_info是什么意思啊?怎么在那儿报错了
      

  13.   


     <asp:GridView ID="gv_info" runat="server" AutoGenerateColumns="False">
    GridView 的ID
      

  14.   

    直接复制 页面名称 是 caicai前台页面:
    caicai.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="caicai.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField HeaderText="菜号"  DataField="菜号"/>
                    <asp:BoundField HeaderText="菜名" DataField="菜名"/>
                    <asp:BoundField HeaderText="菜价" DataField="菜价"/>
                    <asp:TemplateField HeaderText="选择">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <br />
            您选的菜是:<br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <br />
    &nbsp;&nbsp; 总价是:<asp:Label ID="Label2" runat="server"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    后台页面
    caicai.aspx.csusing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = CreateDataSource();
                GridView1.DataBind();
            }
           
        }
        ICollection CreateDataSource()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            System.Data.DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("菜号", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("菜名", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("菜价", typeof(System.Decimal)));        for (int i = 0; i < 5; i++)
            {
                System.Random rd = new System.Random(Environment.TickCount * i); ;
                dr = dt.NewRow();
                dr[0] = "H" + i.ToString();
                dr[1] = "土豆丝" + i.ToString();
                dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
                dt.Rows.Add(dr);
            }
            System.Data.DataView dv = new System.Data.DataView(dt);
            return dv;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Double cj = 0.00;
            Label1.Text = "";
           
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                if (cb.Checked == true)
                {
                    //取出点菜的ID 存入数组
                    Label1.Text += "   " + GridView1.Rows[i].Cells[1].Text+"<br/>";
                    cj += Convert.ToDouble(GridView1.Rows[i].Cells[2].Text);
                }
                else
                {
                    //不做任何操作
                }        }
            Label2.Text = cj.ToString();
        }
    }
      

  15.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script src="JS/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script><html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">    <script type="text/javascript">
            var allID = "";
            function checkJs(IsCheck) {
                allID = ""
                if (IsCheck) {
                    $("#gv_info tr:gt(0)").css("background", "blue");
                    $("#gv_info tr:gt(0)").each(function() {
                        $(this).find(":checkbox").attr("checked", true);
                        allID += $(this).find(":checkbox").val()+",";
                    })
                } else {
                    $("#gv_info tr").css("background", "");
                    $("#gv_info tr:gt(0)").each(function() {
                        $(this).find(":checkbox").attr("checked", false);
                        allID = "";
                    })
                    $("#hdID").val(allID);
                }
               
            }        $(function() {
                var flag = 0;
                $("#gv_info tr:gt(0)").click(function() {
                    flag = 0;
                    var thirdRe = "";
                    thirdRe = $(this).find(":checkbox").val() + ",";
                    alert(thirdRe);
                    if ($(this).css("background") == "blue") {
                        $(this).css("background", "");
                        $(this).find(":checkbox").attr("checked", false);                    allID = allID.replace(thirdRe, '');
                    } else {
                        $(this).css("background", "blue");
                        $(this).find(":checkbox").attr("checked", true);
                        allID += thirdRe;
                    }
                    $("#gv_info tr:gt(0)").each(function() {
                        if ($(this).css("background") != "blue") {
                            flag = flag + 1;
                        }
                    })
                    if (flag == 0) {
                        $("#gv_info tr:eq(0)").find(":checkbox").attr("checked", true);
                    } else {
                        $("#gv_info tr:eq(0)").find(":checkbox").attr("checked", false);
                    }
                    $("#hdID").val(allID);
                })        })
            
            
            
            
            
            
        </script>    <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="div" align="center">
            <asp:HiddenField ID="hdID" runat="Server" onvaluechanged="hdID_ValueChanged" />
            <asp:GridView ID="gv_info" runat="server" AutoGenerateColumns="False" 
                AllowPaging="True" AllowSorting="True" DataKeyNames="菜号" 
                DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="菜号" HeaderText="菜号" ReadOnly="True" 
                        SortExpression="菜号" />
                    <asp:BoundField DataField="菜名" HeaderText="菜名" SortExpression="菜名" />
                    <asp:BoundField DataField="菜价" HeaderText="菜价" SortExpression="菜价" />
                    <asp:CheckBoxField DataField="bool" HeaderText="xuanze" SortExpression="bool" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:点菜系统ConnectionString %>" 
                DeleteCommand="DELETE FROM [菜单] WHERE [菜号] = @菜号" 
                InsertCommand="INSERT INTO [菜单] ([菜号], [菜名], [菜价]) VALUES (@菜号, @菜名, @菜价)" 
                SelectCommand="SELECT * FROM [菜单]" 
                UpdateCommand="UPDATE [菜单] SET [菜名] = @菜名, [菜价] = @菜价 WHERE [菜号] = @菜号">
                <DeleteParameters>
                    <asp:Parameter Name="菜号" Type="String" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="菜名" Type="String" />
                    <asp:Parameter Name="菜价" Type="Decimal" />
                    <asp:Parameter Name="菜号" Type="String" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="菜号" Type="String" />
                    <asp:Parameter Name="菜名" Type="String" />
                    <asp:Parameter Name="菜价" Type="Decimal" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            <br />
        </div>
        </form>
    </body>
    </html> 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;public partial class Default5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string constring = "Data Source=.;Initial Catalog=Test;User ID=sa;Password=";
            string str = ConfigurationManager.ConnectionStrings["点菜系统ConnectionString"].ConnectionString; 
            SqlConnection con = new SqlConnection(str);
            string selectstring = "SELECT  * FROM  菜单";
            SqlDataAdapter sds = new SqlDataAdapter(selectstring, con);
            DataSet ds = new DataSet();
            sds.Fill(ds);
            gv_info.DataSource = ds.Tables[0];
            gv_info.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string a = hdID.Value;
        }
        protected void hdID_ValueChanged(object sender, EventArgs e)
        {    }
    }
      

  16.   

    看后台代码     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = CreateDataSource();//这里写你的数据源,即从数据库读出来的数据
                GridView1.DataBind();
            }
           
        }
       //这个方法只是为了虚拟一个数据源,如果你连接数据库可以把这个方法删了,然后把上边的红色部分换成你的数据源
        ICollection CreateDataSource()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            System.Data.DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("菜号", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("菜名", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("菜价", typeof(System.Decimal)));        for (int i = 0; i < 5; i++)
            {
                System.Random rd = new System.Random(Environment.TickCount * i); ;
                dr = dt.NewRow();
                dr[0] = "H" + i.ToString();
                dr[1] = "土豆丝" + i.ToString();
                dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
                dt.Rows.Add(dr);
            }
            System.Data.DataView dv = new System.Data.DataView(dt);
            return dv;
        }