2008-10-05 61 views
7

下面是在運行時綁定一個GridView我的代碼:如何在EditItemTemplate字段中的GridView中綁定DropDownList?

... 
<asp:templatefield> 
    <edititemtemplate> 
     <asp:dropdownlist runat="server" id="ddgvOpp" /> 
    </edititemtemplate> 
    <itemtemplate> 
     <%# Eval("opponent.name") %> 
    </itemtemplate> 
</asp:templatefield> 
... 

我想在下拉列表「ddgvOpp」綁定,但我不知道怎麼辦。我應該,但我不知道。下面是我有什麼,但我不斷收到「對象引用」的錯誤,這是有道理的:

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) //skip header row 
    { 
     DropDownList ddOpp = (DropDownList)e.Row.Cells[5].FindControl("ddgvOpp"); 
     BindOpponentDD(ddOpp); 
    } 
} 

哪裏BindOpponentDD()只是將DropDownList被填充的位置。我在正確的事件中沒有這樣做嗎?如果沒有,我需要把它放進去?

在此先感謝這麼多...

回答

5

好吧,我想我只是愚蠢。我想到了。

在RowDataBound事件,只需添加以下條件:

if (myGridView.EditIndex == e.Row.RowIndex) 
{ 
    //do work 
} 
+0

我想你也並不需要(e.Row.RowType == DataControlRowType.DataRow),除非你已經在某種程度上砍死編輯標題行。 – quillbreaker 2009-06-19 18:48:06

1
protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (grdDevelopment.EditIndex == e.Row.RowIndex && e.Row.RowType==DataControlRowType.DataRow) 
    {  
     DropDownList drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("ddlBuildServers"); 
    } 
} 

試試這個

這將幫助ü

1

我有同樣的問題,但這個修復(賈森,這是添加條件處理程序)不適合我;編輯行從來沒有數據綁定,所以這個條件從來沒有評估爲真。 RowDataBound從來沒有和GridView.EditIndex一樣的RowIndex被調用。不過,我的設置有點不同,因爲它不是通過編程方式綁定下拉列表,而是綁定到頁面上的ObjectDataSource。儘管如此,下拉菜單仍然需要分別綁定,因爲其可能的值取決於該行中的其他信息。所以ObjectDataSource有一個SessionParameter,我確保在需要綁定時設置適當的會話變量。

<asp:ObjectDataSource ID="objInfo" runat="server" SelectMethod="GetData" TypeName="MyTypeName"> 
<SelectParameters> 
    <asp:SessionParameter Name="MyID" SessionField="MID" Type="Int32" /> 
</SelectParameters> 

及相關行中的下拉列表:

<asp:TemplateField HeaderText="My Info" SortExpression="MyInfo"> 
     <EditItemTemplate> 
      <asp:DropDownList ID="ddlEditMyInfo" runat="server" DataSourceID="objInfo" DataTextField="MyInfo" DataValueField="MyInfoID" SelectedValue='<%#Bind("ID") %>' /> 
     </EditItemTemplate> 
     <ItemTemplate> 
      <span><%#Eval("MyInfo") %></span> 
     </ItemTemplate> 
    </asp:TemplateField> 

我最終什麼事在GridView沒有使用一個CommandField生成我的編輯,刪除,更新和取消按鈕做;我使用TemplateField自己完成,通過適當地設置CommandNames,我可以在GridView上觸發內置的編輯/刪除/更新/取消操作。對於Edit按鈕,我將CommandArgument作爲綁定下拉列表所需的信息,而不是像通常那樣排列的PK。幸運的是,這並沒有阻止GridView編輯相應的行。

<asp:TemplateField> 
     <ItemTemplate> 
      <asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/delete.gif" AlternateText="Delete" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Delete" /> 
      <asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/edit.gif" AlternateText="Edit" CommandArgument='<%#Eval("MyID") %>' CommandName="Edit" /> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/update.gif" AlternateText="Update" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Update" /> 
      <asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/cancel.gif" AlternateText="Cancel" CommandName="Cancel" /> 
     </EditItemTemplate> 
    </asp:TemplateField> 

而在RowCommand處理:

void grdOverrides_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      if (e.CommandName == "Edit") 
       Session["MID"] = Int32.Parse(e.CommandArgument.ToString()); 
     } 

的RowCommand,當然,恰好行進入編輯模式,從而下拉databinds之前之前。所以一切正常。這有點破解,但我花了足夠的時間試圖弄清爲什麼編輯行已經不是數據綁定了。

2

感謝SAURABH帕蒂,

你爲我提供工作的解決方案。 在gridView_RowDataBound()事件中使用。

if(gridView.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow) 
{ 
    // FindControl 
    // And populate it 
} 

如果有人遇到同樣的問題,那就試試看。

乾杯。

0

該代碼將做你想要什麼:

<asp:TemplateField HeaderText="garantia" SortExpression="garantia"> 
<EditItemTemplate> 
    <asp:DropDownList ID="ddgvOpp" runat="server" SelectedValue='<%# Bind("opponent.name") %>'> 
     <asp:ListItem Text="Si" Value="True"></asp:ListItem> 
     <asp:ListItem Text="No" Value="False"></asp:ListItem> 
    </asp:DropDownList> 
</EditItemTemplate> 
<ItemTemplate> 
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("opponent.name") %>'></asp:Label> 
</ItemTemplate> 
相關問題