当前位置:编程学习 > C#/ASP.NET >>

关于Repeater控件中,得到DropDownList的值等一系列问题[专为此问题注册帐号,散分]

各位老鸟,您好,小弟在使用Repeater控件中遇到了一些麻烦,请各位老鸟解答

这里可以看到,其中的菜品名称和菜品价格都是通过Label控件绑定显示出来的,后面加了一个点菜份数,是用的DropDownList,我选择好了需要的菜品后,点提交按钮后,只能提交最后一个值“鳝鱼”,而且需要的份数也显示不出来,我把该提交按钮的代码发上来,用的是Foreach遍历整个Repeater,但是好像这样不是我需要的。

我需要实现的是:
1.得到Repeater控件中的DropDownList的值,并成功的插入数据库中。
2.点提交按钮后,同时在数据库中插入多条我选择的菜品的信息,DropDownList值为0的,不提交到数据库中,提交到数据库中需要包含菜品名称,价格,份数,这样的三个信息,就是很郁闷,想了半天也不知道怎么实现,我把“提交”按钮的代码放上来,请各位帮帮忙,谢谢了!

protected void btnSure_Click(object sender, EventArgs e)
    {
        DianCai diancai = new DianCai();
        //菜品名称
        foreach (RepeaterItem MyItem1 in Repeater1.Items)
        {
           diancai.DeskId = this.DropDownList1.SelectedValue.ToString();
           diancai.Persons = this.txtPersons.Text.ToString();

           Label MyLabel1 = (Label)MyItem1.FindControl("Label3");
           diancai.MeatDishName = MyLabel1.Text.ToString();
           //菜品价格
           Label MyLabel = (Label)MyItem1.FindControl("Label4");
           diancai.MeatPrice = MyLabel.Text.ToString();
           //份数
           DropDownList MyDropDownList = (DropDownList)MyItem1.FindControl("DropDownList2");
           diancai.MeatAmount = MyDropDownList.SelectedValue.ToString();
        }
        if (DianCaiDao.CustomerTable(diancai) == true)
        {
            Response.Redirect("Successful.aspx");
        }
   }  


有追分 --------------------编程问答-------------------- 直接把DropDownList.SelectedValue进行Bind绑定不可以么? --------------------编程问答-------------------- 但是怎么样才能一次提交多条数据到数据库呢?
就是说一桌客人要对应一些点菜的菜单哈,谢谢各位了 --------------------编程问答-------------------- 关注下,这个我也不明白 --------------------编程问答-------------------- 我好久没有写这些个代码了,手痒帮你写了一个

<%@ Import Namespace="System.Data" %>
<!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:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
        <ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value=1>1</asp:ListItem>
<asp:ListItem Value=2>2</asp:ListItem>
</asp:DropDownList><br />         
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="Button1" CommandName="s" runat="server" Text="Button" /></FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class RepeaterDDL : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindRT();
        }
    }
    private void bindRT()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("num", typeof(int));
        DataRow dr = null;
        for (int i=1; i <= 10; i++)
        {
            dr = dt.NewRow();
            dr[0] = i;
            dt.Rows.Add(dr);
        }
        this.Repeater1.DataSource = dt.DefaultView;
        this.Repeater1.DataBind();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string list = string.Empty;
        if (e.CommandName == "s")
        {
            foreach (RepeaterItem ri in this.Repeater1.Items)
            {
                list += ((DropDownList)ri.FindControl("DropDownList1")).SelectedValue+"-";
            }
        }
        Response.Write(list);
    }
}
--------------------编程问答-------------------- 但是怎么样才能一次提交多条数据到数据库呢? 
就是说一桌客人要对应一些点菜的菜单哈,谢谢各位了
---------------------
直接把整个表插入库 --------------------编程问答-------------------- 你的思路有点问题!你应该这样!遍历Repeater控件后.找出droplist中值大0的.然后进行插入操作!

 发表于:2007-11-09 16:07:51 楼主 
各位老鸟,您好,小弟在使用Repeater控件中遇到了一些麻烦,请各位老鸟解答 
 
这里可以看到,其中的菜品名称和菜品价格都是通过Label控件绑定显示出来的,后面加了一个点菜份数,是用的DropDownList,我选择好了需要的菜品后,点提交按钮后,只能提交最后一个值“鳝鱼”,而且需要的份数也显示不出来,我把该提交按钮的代码发上来,用的是Foreach遍历整个Repeater,但是好像这样不是我需要的。 
 
我需要实现的是: 
1.得到Repeater控件中的DropDownList的值,并成功的插入数据库中。 
2.点提交按钮后,同时在数据库中插入多条我选择的菜品的信息,DropDownList值为0的,不提交到数据库中,提交到数据库中需要包含菜品名称,价格,份数,这样的三个信息,就是很郁闷,想了半天也不知道怎么实现,我把“提交”按钮的代码放上来,请各位帮帮忙,谢谢了! 

C# code
protected void btnSure_Click(object sender, EventArgs e)
    {
        DianCai diancai = new DianCai();
        //菜品名称
        foreach (RepeaterItem MyItem1 in Repeater1.Items)
        {
           DropDownList MyDropDownList = (DropDownList)MyItem1.FindControl("DropDownList2");
           if(MyDropDownList.SelectedValue.ToString()>0)
             {
             diancai.MeatAmount = MyDropDownList.SelectedValue.ToString();
    
           diancai.DeskId = this.DropDownList1.SelectedValue.ToString();
           diancai.Persons = this.txtPersons.Text.ToString();

           Label MyLabel1 = (Label)MyItem1.FindControl("Label3");
           diancai.MeatDishName = MyLabel1.Text.ToString();
           //菜品价格
           Label MyLabel = (Label)MyItem1.FindControl("Label4");
         diancai.MeatPrice = MyLabel.Text.ToString();

         //这里要执行数据的插入工作
          //dosomething
           
                   }
             }
           
        if (DianCaiDao.CustomerTable(diancai) == true)
        {
            Response.Redirect("Successful.aspx");
        }
   }  

 
 
--------------------编程问答-------------------- 楼上代码肯定有问题哦

protected void btnSure_Click(object sender, EventArgs e)
    {
        DianCai diancai = new DianCai();
        //菜品名称
        foreach (RepeaterItem MyItem1 in Repeater1.Items)
        {
           DropDownList MyDropDownList = (DropDownList)MyItem1.FindControl("DropDownList2");
           if(MyDropDownList.SelectedValue.ToString()>0)//这样也行??????????????????
             {
             diancai.MeatAmount = MyDropDownList.SelectedValue.ToString();
    
           diancai.DeskId = this.DropDownList1.SelectedValue.ToString();
           diancai.Persons = this.txtPersons.Text.ToString();

           Label MyLabel1 = (Label)MyItem1.FindControl("Label3");
           diancai.MeatDishName = MyLabel1.Text.ToString();
           //菜品价格
           Label MyLabel = (Label)MyItem1.FindControl("Label4");
         diancai.MeatPrice = MyLabel.Text.ToString();

         //这里要执行数据的插入工作
          //dosomething
           
                   }
             }
           
        if (DianCaiDao.CustomerTable(diancai) == true)
        {
            Response.Redirect("Successful.aspx");
        }
   }
--------------------编程问答-------------------- 请问怎么把一张表插入到数据库呢?
能写出具体的SQL语句吗?
谢谢! --------------------编程问答--------------------

if(MyDropDownList.SelectedValue.ToString()>0)//这样也行??????????????????


这样应该不行吧?? --------------------编程问答-------------------- 问题解决了一半了
谢谢各位!!!!

在第一个问题中,我是忘了写:
if (!IsPostBack)
 {
 }
导致了每次提交数据的时候都返回了一次默认值,所以没有值提交到数据库

现在还有第二个问题了:
为何每次都只能提交最后选择的“鳝鱼”到数据表呢?
我想提交的是整个荤菜菜单到数据表中,请问应该怎么入手呢?
谢谢各位了!~
--------------------编程问答-------------------- 对不起,没注意.不过意思是那样的:
(MyDropDownList.SelectedValue.ToString()>0
改成----------->
convert.toInt32(MyDropDownList.SelectedItem.text)>0

为何每次都只能提交最后选择的“鳝鱼”到数据表呢? 
---------------------------------------------
你在这里:
    //这里要执行数据的插入工作
          //dosomething
写插入操作啊!!晕


我想提交的是整个荤菜菜单到数据表中,请问应该怎么入手呢? 
------------------------------------------------
什么意思?如果你要区分菜的种类.你要增加一个字段才分.要不然很难判断! --------------------编程问答-------------------- 请问怎么把一张表插入到数据库呢? 
能写出具体的SQL语句吗? 
谢谢! --------------------编程问答-------------------- 请问怎么把一张表插入到数据库呢?   
能写出具体的SQL语句吗?   
谢谢!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,