列表视图中的下拉列表中的所选项目为空白

时间:2012-05-07 16:18:37

标签: asp.net listview drop-down-menu selectedindexchanged

我有一个复杂的项目,需要在列表视图中填充下拉列表。我能做到的。但是,当我从下拉列表中选择正确的值后单击更新时,它会在列表视图中返回一个空白值。 我附上了一个示例代码,其中“day”来自pull own,具体取决于月份。

任何人都可以在那里编辑我的代码以使其正常工作。根据我所做的研究,我认为它与选择的指数变化有关。

我正在使用Visual Web开发者2010快递 我也在使用框架3.5

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DispatchConnectionString %>" 
        SelectCommand="SELECT [day] FROM [TMP]"></asp:SqlDataSource>

</div>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="month" 
    DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">
    <AlternatingItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
                <asp:Label ID="monthLabel" runat="server" Text='<%# Eval("month") %>' />
            </td>
            <td>
                <asp:Label ID="dayLabel" runat="server" Text='<%# Eval("day") %>' />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <EditItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                    Text="Update" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Cancel" />
            </td>
            <td>
                <asp:Label ID="monthLabel1" runat="server" Text='<%# Eval("month") %>' />
            </td>
            <td>
                <%--<asp:TextBox ID="dayTextBox" runat="server" Text='<%# Bind("day") %>' />--%>
                <asp:DropDownList ID="list" runat="server" DataSourceID="SqlDataSource2" 
                      DataTextField="day" DataValueField="day">
                  </asp:DropDownList>
            </td>
        </tr>
    </EditItemTemplate>
    <EmptyDataTemplate>
        <table runat="server" style="">
            <tr>
                <td>
                    No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>
    <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                    Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Clear" />
            </td>
            <td>
                <asp:TextBox ID="monthTextBox" runat="server" Text='<%# Bind("month") %>' />
            </td>
            <td>
                <asp:TextBox ID="dayTextBox" runat="server" Text='<%# Bind("day") %>' />
            </td>
        </tr>
    </InsertItemTemplate>
    <ItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
                <asp:Label ID="monthLabel" runat="server" Text='<%# Eval("month") %>' />
            </td>
            <td>
                <asp:Label ID="dayLabel" runat="server" Text='<%# Eval("day") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                        <tr runat="server" style="">
                            <th runat="server">
                            </th>
                            <th runat="server">
                                month</th>
                            <th runat="server">
                                day</th>
                        </tr>
                        <tr ID="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr runat="server">
                <td runat="server" style="">
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <SelectedItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
                <asp:Label ID="monthLabel" runat="server" Text='<%# Eval("month") %>' />
            </td>
            <td>
                <asp:Label ID="dayLabel" runat="server" Text='<%# Eval("day") %>' />
            </td>
        </tr>
    </SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DispatchConnectionString %>" 
    DeleteCommand="DELETE FROM [TMP] WHERE [month] = @month" 
    InsertCommand="INSERT INTO [TMP] ([month], [day]) VALUES (@month, @day)" 
    SelectCommand="SELECT * FROM [TMP]" 
    UpdateCommand="UPDATE [TMP] SET [day] = @day WHERE [month] = @month">
    <DeleteParameters>
        <asp:Parameter Name="month" Type="String" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="month" Type="String" />
        <asp:Parameter Name="day" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="day" Type="String" />
        <asp:Parameter Name="month" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>
</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.Web.UI.WebControls.button;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
   {

   }
}

1 个答案:

答案 0 :(得分:0)

您必须在下拉列表中的日期更改后重新绑定列表视图。我看到你在EditItemTemplate中有下拉列表。处理列表视图的更新事件,并使用udpated日重新绑定数据源。