標籤:style blog color ar 代碼 div new log
在GridView中,我們 有時候要綁定值。
前台綁定的代碼可以這樣
<asp:TemplateField HeaderText="目前狀態" ItemStyle-HorizontalAlign="Center"> <EditItemTemplate> <asp:DropDownList ID="dStatus" DataSource=‘<%#BindStatus()%>‘ DataTextField="Name" DataValueField="ID" runat="server"> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lStatus" runat="server" Text=‘<%#Bind("Status") %>‘></asp:Label> </ItemTemplate><ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:TemplateField>
為了在後台選擇編輯的時候,DropDownList預設選中的值為 <asp:Label ID="lStatus" runat="server" Text=‘<%#Bind("Status") %>‘></asp:Label>的值
在後台代碼中,點擊編輯實踐中添加以下代碼:
protected void gvProject_RowEditing(object sender, GridViewEditEventArgs e) { Label lstus = (Label)gvProject.Rows[e.NewEditIndex].FindControl("lStatus"); gvProject.EditIndex = e.NewEditIndex; Bind(); DropDownList ddList=(DropDownList)gvProject.Rows[e.NewEditIndex].FindControl("dStatus"); ddList.SelectedItem.Text = lstus.Text; }