C# 下拉列表不会在gridview中更新
我有一个gridview,其中填充了从数据库表中提取的元素。每行包含2个文本框和1个下拉列表。加载页面时,将填充下拉列表 我的问题是:当我编辑行时,从下拉列表中选择另一项,然后单击更新按钮,没有任何更改。下拉列表仍然返回其默认值,数据库中的修改值都不会更新(删除不起作用等)。我不明白原因。请帮帮我 我的网格视图:C# 下拉列表不会在gridview中更新,c#,asp.net,gridview,drop-down-menu,C#,Asp.net,Gridview,Drop Down Menu,我有一个gridview,其中填充了从数据库表中提取的元素。每行包含2个文本框和1个下拉列表。加载页面时,将填充下拉列表 我的问题是:当我编辑行时,从下拉列表中选择另一项,然后单击更新按钮,没有任何更改。下拉列表仍然返回其默认值,数据库中的修改值都不会更新(删除不起作用等)。我不明白原因。请帮帮我 我的网格视图: <asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false" OnRowDataBoun
<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;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
MyGridView.DataBind();
}
受保护的void GridViewCausali_OnRowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//在gridview中初始化ddl
DropDownList ddl=(DropDownList)e.Row.FindControl(“ddlCity”);
列表城市=新列表();
cities=GetFromDB();
ddl.DataSource=城市;
ddl.DataBind();
ddl.Items.FindByValue((e.Row.FindControl(“lblcurrentCity”)作为
标签)。文本)。选中=真;
}
}
受保护的无效GridView\u onRowUpdate(对象发送方,GridViewUpdateEventArgs e)
{
DropDownList ddl=(DropDownList)MyGridView.Rows[e.RowIndex].FindControl(“ddlCity”);
字符串测试=ddl.SelectedValue;
Label lbl=(Label)MyGridView.Rows[e.RowIndex].FindControl(“lblcurrentCity”);
lbl.Text=ddl.SelectedValue;
ddl.Items.FindByValue((MyGridView.Rows[e.RowIndex].FindControl(“lblcurrentCity”)作为标签)。Text)。Selected=true;
}
我建议您添加DefaultContainerName属性
,添加stringconnection
<asp:EntityDataSource ID="DataSource" runat="server"
ConnectionString="name=..."
DefaultContainerName="..."
EntitySetName="users"
EnableDelete="True" EnableInsert="True" EnableUpdate="True"/>
您的RowUpdate活动在哪里?ddl是否设置为autopostback false?是,autopostback设置为false。我编辑OnRowUpdate事件在此过程中,您必须处理几个事件。发布行更新事件的代码。您有一个行编辑事件,即行更新和行更新。我认为您的问题在于更新事件。您应该从代码隐藏文件中为datagrid做所有事情。你得到了最大的控制,这是如此容易。