0

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

那里的任何人都可以编辑我的代码以使其正常工作。从我所做的研究来看,我认为这与 Selected Index 的变化有关。

我正在使用visual web developer 2010 express 我也在使用framework 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)
   {

   }
}
4

1 回答 1

0

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

于 2012-05-07T19:21:04.797 回答