下拉列表未在网格视图中更新

本文关键字:更新 视图 网格 下拉列表 | 更新日期: 2023-09-27 18:05:59

我有一个gridview填充从数据库中的表中提取的元素。每行包含2个文本框和1个下拉列表。在加载页面时填充下拉列表。

我的问题是:当我编辑行时,从下拉列表中选择另一个项目,然后单击更新按钮,没有任何变化。下拉列表仍然返回到其默认值,修改后的值也不会在数据库中更新(删除不起作用等)。我不明白原因。请帮帮我。

我的显示数据表格:

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false"    OnRowDataBound="GridView_OnRowDataBound" OnRowUpdating="GridView_OnRowUpdating" 
 DataKeyNames="id" DataSourceID="DataSource" ><AlternatingRowStyle BackColor="White" />
<Columns>
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowCancelButton="True" />
 <asp:TemplateField HeaderText="currentCity" SortExpression="currentCity" ItemStyle-  HorizontalAlign="Center" Visible="false">
        <ItemTemplate>
            <asp:Label runat="server" ID="lblcurrentCity" Text='<%#   Bind("CodiceContrattoRisorsa") %>' Visible="false" ></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="id" SortExpression="id" ItemStyle-HorizontalAlign="Center" >
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtId" Text='<%# Bind("id") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
 <asp:TemplateField HeaderText="name" SortExpression="name" ItemStyle-  HorizontalAlign="Center" >
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtName" Text='<%# Bind("name") %>'>    </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="city" SortExpression="city" ItemStyle-  HorizontalAlign="Center">
        <EditItemTemplate>
            <asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:DropDownList ID="ddlCity" runat="server" Enabled="false"  >
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
 </Columns>
 <RowStyle BackColor="#EFF3FB" />
 <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 </asp:GridView>
 <asp:EntityDataSource ID="DataSource"
    runat="server" ContextTypeName="Context"
    EntitySetName="users" EntityTypeFilter="user"
    EnableDelete="True" EnableUpdate="True" />

加载页面…

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            MyGridView.DataBind();
    }
protected void GridViewCausali_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // initialize ddl in gridview
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCity");
            List<city> cities = new List<city>();
            cities = GetFromDB();
            ddl.DataSource = cities;
            ddl.DataBind();
            ddl.Items.FindByValue((e.Row.FindControl("lblcurrentCity") as                                                     
            Label).Text).Selected = true;
        }
    }
protected void GridView_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddl = (DropDownList)MyGridView.Rows[e.RowIndex].FindControl("ddlCity");
        string test = ddl.SelectedValue;
        Label lbl = (Label)MyGridView.Rows[e.RowIndex].FindControl("lblcurrentCity");
        lbl.Text = ddl.SelectedValue;
        ddl.Items.FindByValue((MyGridView.Rows[e.RowIndex].FindControl("lblcurrentCity") as Label).Text).Selected = true;
                }

下拉列表未在网格视图中更新

我建议您添加DefaultContainerName property,添加stringconnection

<asp:EntityDataSource ID="DataSource" runat="server" 
    ConnectionString="name=..."
    DefaultContainerName="..." 
    EntitySetName="users" 
    EnableDelete="True" EnableInsert="True" EnableUpdate="True"/>