0

我有一个填充了从数据库中的表中提取的元素的网格视图。每行包含 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;
                }
4

1 回答 1

0

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

<asp:EntityDataSource ID="DataSource" runat="server" 
    ConnectionString="name=..."
    DefaultContainerName="..." 

    EntitySetName="users" 
    EnableDelete="True" EnableInsert="True" EnableUpdate="True"/>
于 2013-03-25T16:23:55.047 回答