【问题标题】:gridview: show link, but edit a dropdownlistgridview:显示链接,但编辑下拉列表
【发布时间】:2011-04-03 10:26:42
【问题描述】:

我有一个gridview,它有一个作者栏。我想将作者姓名显示为超链接,因此当用户单击它时,他会被重定向到作者页面。但是当用户希望编辑当前产品的作者时,他应该会看到一个下拉列表。我正在尝试使用模板字段来实现它:

<asp:TemplateField HeaderText="автор">
        <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateURL='<%# "~/CMS/AuthorPage.aspx?a="+ Eval("AuthorID")%>' Text='<%#Eval("AuthorID")%>' />                                    
        </ItemTemplate>
        <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource3" 
        DataTextField="Name" DataValueField="ID"/>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString1 %>" 
        SelectCommand="SELECT [ID], [Name] FROM [Authors] ORDER BY [Name]"></asp:SqlDataSource>                
</EditItemTemplate>
</asp:TemplateField>       

但是如何指定选中的值,编辑后如何保存选中的值呢?

【问题讨论】:

  • 我尝试过使用 RowEditing 事件,但是当此事件发生时,下拉列表不存在于单元格的控件集合中。
  • 我也试过 ' /> ,但是 ASP.NET 说 DropDownList 中没有 SelectedItemValue 属性,虽然有!!!

标签: asp.net templates gridview drop-down-menu


【解决方案1】:

您需要在GridViewRowDataBound 事件中执行此操作,然后在RowUpdating 事件中您可以获得选定的值

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1");
    DropDownList1.SelectedValue = "SomeID";
}

并通过

获取所选值
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    DropDownList DropDownList1 = (DropDownList)this.GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
    string value = DropDownList1.SelectedValue;
}

【讨论】:

  • 谢谢@Waqas Raja!我可以使用 GridView1_RowDataBound 设置下拉列表选定项,但我认为应该以不同的方式进行更新。此外,您的第二段代码不起作用。
  • 正确的 RowUpdating 处理程序是:protected void DropDownList DropDownList1 = (DropDownList)this.GridView1.Rows[e.RowIndex].FindControl("DropDownList1"); e.NewValues.Add("AuthorID", DropDownList1.SelectedValue);
  • @Bogdan0x400,感谢您的 cmets,是的,您是对的,更新时应该有一些不同的方法来获取选定的值。我相应地更正了我的代码。
猜你喜欢
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 2016-04-11
  • 2014-10-04
  • 1970-01-01
  • 2011-11-21
相关资源
最近更新 更多