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

怎样将一个DataList中一个DropDownList控件里的值插入数据库里?

一个DataList中一个行为DropDownList控件,怎样获取里面的列值,点击DataList里的一个Button时,将DropDownList里的所选择的值插入数据库中?
<asp:DataList ID="DataList1" runat="server"  RepeatColumns="4" RepeatDirection="Horizontal" Width="150px">
   <ItemTemplate>
      <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem>5</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" BackColor="Red" ForeColor="#FFFFCC" 
                            Text="订购"/>
 </ItemTemplate>
</asp:DataList>
后台代码:
 protected void Button1_Click(object sender, EventArgs e)
    {
       string strsql = "insert into TakeOut() values( 这里的DropDownList1里的值怎么取得?)";
        BaseClass1.execsql(strsql);      
    } DataList DropDownList1 Button  C# --------------------编程问答-------------------- string value = ((DropDownList)this.DataList2.Items[i].FindControl("DropDownList")).SelectedValue; 

参考http://blog.csdn.net/xianfajushi/article/details/3413317 --------------------编程问答-------------------- 我里面的ListItem已经固定了,所以就没有Items[i]啦,这个不行啊 --------------------编程问答-------------------- Refer:
http://www.cnblogs.com/insus/archive/2013/01/20/2868403.html --------------------编程问答-------------------- 用FindControl("控件id")找到控件。在获取SelectedValue属性的值 --------------------编程问答-------------------- 我的后台
 <asp:DataList ID="DataList1" runat="server"  RepeatColumns="4" 
        RepeatDirection="Horizontal" Width="150px">
        <ItemTemplate>
            <table class="style1">
                <tr>
                    <td colspan="2">
                         <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("photo")%> ' Height="150px" Width="180px" /> 
                </tr>
                <tr>
                    <td colspan="2" align="center">               
                         <%# Eval("name")%></td>
                </tr>
                <tr>
                    <td  align="right" style="color: #FF0000">
                           <%# Eval("price")%></td>
                    <td align="left">
                       元/份</td>
                </tr>
                <tr>
                    <td colspan="2">
                        我要<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem>5</asp:ListItem>
                        </asp:DropDownList>
                       份<asp:Button ID="Button1" runat="server" BackColor="Red" ForeColor="#FFFFCC" Text="订购"  
                       CommandArgument='<%# Eval("id")+","+Eval("name")+","+Eval("price")+","+ DropDownList1.Text%>' onclick="Button1_Click" CausesValidation="False"/>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:DataList>
前台:
 protected void Button1_Click(object sender, EventArgs e)
    {      
        string[] estr = ((Button)sender).CommandArgument.ToString().Split(',');  
        string strsql = "insert into TakeOut1(username,cname,price,num) values( '" + Session["name"].ToString() + "','" + estr[1].ToString() + "','" + estr[2] + "','" + estr[3] + "')";
        BaseClass1.execsql(strsql);      
    }

 DropDownList1.Text 的值没有得到啊,数据库里为空呢 --------------------编程问答-------------------- 朋友们帮忙改改呀,哪里出错了,值得不到呀 --------------------编程问答-------------------- 前台设计界面:
--------------------编程问答-------------------- 前台代码:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" 
            onitemcommand="DataList1_ItemCommand">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack=false>
                    <asp:ListItem Text="1t" Value="1v">
                    </asp:ListItem>
                    <asp:ListItem Text="2t" Value="2v">
                    </asp:ListItem>
                    <asp:ListItem Text="3t" Value="3v">
                    </asp:ListItem>
                    <asp:ListItem Text="4t" Value="4v">
                    </asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </ItemTemplate>
        </asp:DataList>


后台代码:
protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script>alert('{0}');</script>", ((DropDownList)DataList1.Items[0].FindControl("DropDownList1")).SelectedItem.Value));
        }


--------------------编程问答--------------------
引用 8 楼 zhengceHH 的回复:
前台代码:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" 
            onitemcommand="DataList1_ItemCommand">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack=false>
                    <asp:ListItem Text="1t" Value="1v">
                    </asp:ListItem>
                    <asp:ListItem Text="2t" Value="2v">
                    </asp:ListItem>
                    <asp:ListItem Text="3t" Value="3v">
                    </asp:ListItem>
                    <asp:ListItem Text="4t" Value="4v">
                    </asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </ItemTemplate>
        </asp:DataList>


后台代码:
protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script>alert('{0}');</script>", ((DropDownList)DataList1.Items[0].FindControl("DropDownList1")).SelectedItem.Value));
        }




按钮事件有些行不通 --------------------编程问答-------------------- 是哇  那个下拉列表的数值不能直接读出来的啊?要绑定数值啊,我简单的以为可以直接读出来呢 --------------------编程问答-------------------- 有没有其他方法呀?按照我的前后台代码来实现呀,帮忙修改一下啊 --------------------编程问答-------------------- DropDownList里的值没有绑定数据源,而是我自己编辑的项 --------------------编程问答-------------------- 前台代码:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="DataList1_ItemCommand">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="false">
                    <asp:ListItem Text="1t" Value="1v">
                    </asp:ListItem>
                    <asp:ListItem Text="2t" Value="2v">
                    </asp:ListItem>
                    <asp:ListItem Text="3t" Value="3v">
                    </asp:ListItem>
                    <asp:ListItem Text="4t" Value="4v">
                    </asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
        </asp:DataList>


后台代码:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script>alert('ItemCommand{0}');</script>", ((DropDownList)e.Item.FindControl("DropDownList1")).SelectedItem.Value));

        }


通过DataList 的DataList1_ItemCommand 事件执行自己需要的内容,同时也能获取相应数据 --------------------编程问答-------------------- Insus.NET在#3提供的链接没有参考价值吗?
如果觉得是否定的话,那你再看下面这个,虽然是Gridview控件,但方法完全可以使用。

http://www.cnblogs.com/insus/archive/2012/09/22/2697862.html --------------------编程问答--------------------
引用 13 楼 zhengceHH 的回复:
前台代码:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="DataList1_ItemCommand">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="false">
                    <asp:ListItem Text="1t" Value="1v">
                    </asp:ListItem>
                    <asp:ListItem Text="2t" Value="2v">
                    </asp:ListItem>
                    <asp:ListItem Text="3t" Value="3v">
                    </asp:ListItem>
                    <asp:ListItem Text="4t" Value="4v">
                    </asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
        </asp:DataList>


后台代码:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script>alert('ItemCommand{0}');</script>", ((DropDownList)e.Item.FindControl("DropDownList1")).SelectedItem.Value));

        }


通过DataList 的DataList1_ItemCommand 事件执行自己需要的内容,同时也能获取相应数据


把Button的OnClick事件去掉,或是不作处理,在OnItemCommand事件里做处理,可以获得所需要的数据 --------------------编程问答-------------------- 喂大的程序员 :请问怎么把警告对话框里的值取到Label里啊?我不要对话框的形式 --------------------编程问答-------------------- 问题解决了,但由于我插入语句设成了string类型,还是有点小问题的。不管怎样,谢谢大家的帮忙
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,