gridview:显示链接,但编辑下拉列表

时间:2011-04-03 10:26:43

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

我有一个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>       

但是如何指定所选值,以及如何在编辑后存储所选值?

1 个答案:

答案 0 :(得分:2)

您需要在RowDataBound GridView事件中执行此操作,然后在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;
}
相关问题