2013-06-25 74 views
1

在gridview中,爲了在視圖模式下顯示數據,我使用了一個標籤。在GridView的編輯模式下爲DropDownList設置選定的值或文本在asp.net中編輯模式

在編輯模式下,我有一個下拉列表。那麼,如何在gridview處於編輯模式時將該標籤中的文本設置爲dropdownlist的選定值?

這是我在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> 
+0

您可以在ItemDatabound函數中設置該值。 –

回答

1

我的意思是,當我把我的網格視圖在編輯模式下,我不希望我的下拉列表顯示「 - 選擇區域名稱 - 」。我希望它顯示「文本1」,2,3或4 ...這是顯示在我的標籤在視圖模式。你可以在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; 
} 
相關問題