2010-08-13 87 views
8

我想按照前面的方法做this。我發現的唯一區別是上面代碼中包含的附加列表項。如何在GridView的EditTemplate中設置DropDownList的SelectedValue

我試圖使用AppendDataBoundItems=true,但它仍然無法正常工作。我還希望將其默認值設置爲在itemtemplate的標籤中顯示的值,即DropDownList的SelectedValue='<%# Eval("DepartmentName") %>',但是在下拉列表中,我的屬性不可用。 可能是什麼原因。 ??

<EditItemTemplate> 
    <asp:DropDownList ID="ddlDepartment_Edit" runat="server" 
     DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName" 
     DataValueField="PK_DepartmentId"> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" 
     ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>" 
     ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" 
     SelectCommandType="StoredProcedure"> 
    </asp:SqlDataSource>         
</EditItemTemplate> 
<ItemTemplate> 
    <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' > 
    </asp:Label> 
</ItemTemplate> 

我使用GridView

回答

8

DataValueField似乎是錯的 - 它不應該是DepartmentId?同樣,您需要有SelectedValue='<%# Eval("**DepartmentId**") %>' - DepartmentName將是SeletectText

+2

這是我陷入困境的地方。沒有這樣的屬性對我來說是可見的,即SelectedValue或SelectedText,以便我可以爲它們分配一些值。關於DataValueField,是的,你是對的,它應該是主鍵。但直到這兩個屬性可用於我之前,不是討論的一部分。 – 2010-08-13 10:28:48

+2

VS設計師可能不會以智能的方式顯示您的財產,但它的存在。有兩個屬性 - SelectedValue和SelectedIndex。如果您編寫SelectedIndex ='<%#Eval('DepartmentId')%>'會發生什麼? – VinayC 2010-08-13 11:29:11

+0

我的錯誤 - 應該是SelectedValue ='<%#Eval('DepartmentId')%>'。 – VinayC 2010-08-13 11:37:43

0

在您的網格上有一個事件叫做ItemCommand。爲它創建一個方法:

protected void Grid1_ItemCommand(object source, GridCommandEventArgs e) 

現在簡單地創建,當用戶點擊了電網的編輯按鈕,將認識的情況下聲明:

case Grid.EditCommandName:  
//set a member variable to the string of the cell you are editing. 
//something like: mString = e.item..["Column"].toString();     
        break; 

現在你有一個成員變量設置爲要在下拉列表被加載/預渲染之前選擇字符串。對dropdownbox使用事件OnPrerenderOnLoad,並將所選項目設置爲此字符串。

3

事件處理函數的使用解決了這個問題。

在你的情況,你將需要添加一個HiddenField存儲PK_DepartmentId值:

<asp:GridView ID="gvExample" runat="server" AutoGenerateColumns="False" OnDataBound="gvExample_DataBound"> 
    <Columns> 
    <asp:TemplateField HeaderText="Department"> 
     <EditItemTemplate> 
     <asp:DropDownList ID="ddlDepartment_Edit" runat="server" DataSourceID="dsDepartment_Edit" 
      DataTextField="DepartmentName" DataValueField="PK_DepartmentId"> 
     </asp:DropDownList> 
     <asp:HiddenField ID="hfDepartmentId" runat="server" Value='<%# Bind("PK_DepartmentId") %>' /> 
     <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>" 
      ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" SelectCommandType="StoredProcedure"> 
     </asp:SqlDataSource> 
     </EditItemTemplate> 
     <ItemTemplate> 
     <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>'> 
     </asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:CommandField ShowEditButton="True" ButtonType="Button" /> 
    </Columns> 
</asp:GridView> 

protected void gvExample_DataBound(object sender, EventArgs e) 
{ 
    foreach (GridViewRow gvRow in gvExample.Rows) 
    { 
    DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList; 
    HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField; 

    if (ddlDepartment != null && hfDepartmentId != null) 
    { 
     ddlDepartment.SelectedValue = hfDepartmentId.Value; 
    } 
    } 
} 
+0

謝謝!解決了這個問題 – 2016-06-09 15:56:54

0

這是最好的我已經找到....

protected void GridView1_DataBound(object sender, EventArgs e) 
{ 
    foreach (GridViewRow gvRow in GridView1.Rows) 
    { 
     RadioButtonList rbl = gvRow.FindControl("rblPromptType") as RadioButtonList; 
     HiddenField hf = gvRow.FindControl("hidPromptType") as HiddenField; 

     if (rbl != null && hf != null) 
     { 
      if (hf.Value != "") 
      { 
       //clear the default selection if there is one 
       rbl.ClearSelection(); 
      } 

      rbl.SelectedValue = hf.Value; 
     } 
    } 
} 
0

爲什麼你們建議要使用循環,當有一個GridView方法專門用於當行的條件發生變化時 - RowDataBound()

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      GridViewRow gvRow = (GridViewRow)e.Row; 
      HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID"); 
      if (hfAgentID != null) 
      { 
       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent"); 
        ddlAgent.SelectedValue = hfAgentID.Value; 
       } 
      } 
     } 
相關問題