2014-07-01 114 views
0

我在GridView中有一個下拉列表。我不希望DDL中的值在編輯模式下與視圖模式下相同,當gridview進入編輯模式時,DDL set的值爲第一個值爲默認值。設置下拉列表的值OnRowEditing

我試圖在填充和綁定GridView的編輯模式之前獲取值,但後來我得到空(nFolderId)。當我在綁定後嘗試nFolderId片段時,我得到默認值(意思是第一個值)。

那麼我怎樣才能讓DDL值從視圖模式進入編輯模式呢?

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    GridView1.EditIndex = e.NewEditIndex; 
    GridViewRow row = GridView1.Rows[e.NewEditIndex]; 
    var nFolderId = Convert.ToInt32(((DropDownList)row.FindControl("DropDownListFolders1")).SelectedValue); 
    FillGridView(); 
    GridView1.DataBind(); 
} 

我的aspx:

<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing" DataKeyNames="strPositionId,tmVaRPosition,nFoldersId" 
      AllowSorting="True" AutoGenerateColumns="False" SkinID="gridviewGridlinesSkin" OnRowCancelingEdit="GridView1_CancelingEdit" 
      OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" 
      EmptyDataText="No positions found" OnRowDataBound="GridView1_RowDatabound"> 
      <Columns> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:ImageButton ID="ibEdit" runat="server" AlternateText="Edit" CommandName="Edit" 
          ImageUrl="~/Images/icon_edit.gif" /> 
         <asp:ImageButton ID="ibDelete" runat="server" AlternateText="Delete" CommandName="Delete" 
          ImageUrl="~/Images/icon_delete.gif" OnClientClick='<%# Eval("strPositionName", "return confirm(\"Are you sure you want to delete this entry {0}?\");") %>' /> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:ImageButton ID="ibUpdate" runat="server" AlternateText="Update" CommandName="Update" ImageUrl="~/Images/icon_ok.gif" /> 
         <asp:ImageButton ID="ibCancel" runat="server" AlternateText="Cancel" CommandName="Cancel" ImageUrl="~/Images/icon_cancel.gif" /> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Label ID="Label3" runat="server" Text='<%# Eval("strFolderName") %>'></asp:Label> 
        </ItemTemplate> 
        <ItemStyle Wrap="False" /> 
       </asp:TemplateField> 
       <asp:TemplateField> 
        <EditItemTemplate> 
         <BrummerComp:SortableDropDownList ID="DropDownListFolders1" Width="141px" runat="server" 
           SkinID="BCdropdownlistSkin"/> 
        </EditItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 
+0

問:那麼,在下拉菜單的外觀和在這兩種模式下同樣的經歷 - 視圖模式和編輯模式? – deostroll

+0

@deostroll在視圖模式下,它只是一個標籤*,顯示值 – MrProgram

回答

0

設置了selectedValue的價值

<div> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataSourceID="SqlDataSource1" 
    onrowdeleting="GridView1_RowDeleting"> 
    <Columns> 
     <asp:BoundField DataField="dd" HeaderText="dd" SortExpression="dd" /> 
     <asp:TemplateField HeaderText="ddd" SortExpression="ddd"> 
      <ItemTemplate> 
       <asp:Label ID="Label1" runat="server" Text='<%# Bind("ddd") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ddd") %>'></asp:TextBox> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="cate" SortExpression="cate"> 
      <EditItemTemplate> 
       <asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="SqlDataSource12" DataTextField="cate" DataValueField="id" 
        SelectedValue="<%# Bind('cate') %>"> 
       </asp:DropDownList> 
       <asp:SqlDataSource ID="SqlDataSource12" runat="server" 
        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
        SelectCommand="SELECT * FROM [cate]"></asp:SqlDataSource> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="Label2" runat="server" Text='<%# Bind("cate") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField ShowEditButton="True" /> 
    </Columns> 
</asp:GridView> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
    SelectCommand="SELECT * FROM [View_1]"></asp:SqlDataSource> 
     <br /> 
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
       style="height: 26px" />