asp.net - 在gridview asp.net的编辑模式下设置DropDownList的选定值或文本

标签 asp.net gridview drop-down-menu editmode

在 GridView 中,为了在 View 模式下显示数据,我使用标签。

在编辑模式下,我有一个下拉列表。那么,当 gridview 处于编辑模式时,如何将该标签中的文本设置为下拉列表的选定值?

这是我在 aspx 页面中的代码:

<Columns>             
<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="true" />
<asp:TemplateField HeaderText="Zone Name">
<HeaderStyle Width="220px"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="lbDisplayName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>

<EditItemTemplate>
<asp:DropDownList ID="ddlName" runat="server" CssClass="customddlZoneName">
<asp:ListItem Value="">--Select Zone Name--</asp:ListItem>
<asp:ListItem Value="Value1">Text 1</asp:ListItem>
<asp:ListItem Value="Value2">Text 2</asp:ListItem>
<asp:ListItem Value="Value3">Text 3</asp:ListItem>
<asp:ListItem Value="Value4">Text 4</asp:ListItem>
<asp:ListItem Value="Value5">Text 5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlInsertName" runat="server">
<asp:ListItem Value="">--Select Zone Name--</asp:ListItem>
<asp:ListItem Value="Value 1">Text 1</asp:ListItem>
<asp:ListItem Value="Value 2">Text 2</asp:ListItem>
<asp:ListItem Value="Value 3">Text 3</asp:ListItem>
<asp:ListItem Value="Value 4">Text 4</asp:ListItem>
<asp:ListItem Value="Value 5">Text 5</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>

最佳答案

我的意思是,当我将 GridView 置于编辑模式时,我不希望下拉列表显示“--选择区域名称--”。我希望它显示“文本 1”、2、3 或 4...在 View 模式下显示在我的标签中。您可以在 Gridview_RowEditing 事件中执行此操作。请参阅下面的代码:

protected void Gridview_RowEditing(object sender, GridViewEditEventArgs e)
{
    Label lbDisplayName = (Label)Gridview.Rows[e.NewEditIndex].FindControl("lbDisplayName");
    string name = lbDisplayName.Text;

    GridViewRow gvr = Gridview.Rows[e.NewEditIndex];
    var dr = (DropDownList)gvr.FindControl("ddlName");
    dr.SelectedItem.Text = name;
}

关于asp.net - 在gridview asp.net的编辑模式下设置DropDownList的选定值或文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289556/

相关文章:

javascript - 下拉悬停 CSS

javascript - 如何按值选择下拉列表 dom-repeat Dropdown

asp.net - 使用 ASP.NET Web API 2.1 配置依赖注入(inject)

c# - System.IO.DirectoryInfo.Exists 是否有任何替代方案

c# - 在 asp.net MVC5 上更改文本框大小

android - GridView 在按下后退按钮时被清除(当键盘可见时)

CSS float 和嵌套列表

c# - 从列表集合中填充下拉列表 c#/asp.net

c# - 打印 GridView

android - 从 ListView 传递到 GridView