gridview: show link, but edit a dropdownlist
I have a gridview, and it has an author column. I want to display the author name as a hyperlink, so when the user clicks on it he gets redirected to the author page. But when the user wishes to edit the author for current product, he should see a dropdownlist. I am trying to implement it using a template field:
<asp:TemplateField HeaderText="автор">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateURL='<%# "~/CMS/AuthorPage.aspx?a="+ Eval("AuthorID")%>' Text='<%#Eval("AuthorID")%>' /> 开发者_StackOverflow社区
</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>
But how do I specify the selected value, and how do I store the selected value after edit?
You need to do it in RowDataBound
event of GridView
and then in RowUpdating
event you can get the selected value
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1");
DropDownList1.SelectedValue = "SomeID";
}
and get the selected value by
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList DropDownList1 = (DropDownList)this.GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
string value = DropDownList1.SelectedValue;
}
精彩评论