2011-12-20 17 views
0

我按照此示例http://www.codeproject.com/KB/webforms/Editable_GridView.aspx構建可編輯的GridView控件。 我有這樣的代碼在我的GridView:在RowDataBound事件中找不到下拉列表

<asp:TemplateField HeaderText="Negócio"> 
<ItemTemplate> 
    <asp:Label ID="lblNegocio" runat="server" Text='<%# Eval("Negocio") %>'></asp:Label> 
</ItemTemplate> 
<EditItemTemplate> 
    <asp:DropDownList ID="ddlNegocio" runat="server" /> 
</EditItemTemplate> 
<FooterTemplate> 
    <asp:DropDownList ID="ddlNewNegocio" runat="server" /> 
</FooterTemplate> 

現在,我試圖填補EditItemTemplate中的下拉菜單,也是同樣的例子說,一些動態值,在網格的RowDataBound事件。但是,當我這樣做,FindControl方法總是返回Nothing:

Protected Sub gdvRegraRotationDefault_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdvRegraRotationDefault.RowDataBound 
If e.Row.RowType = DataControlRowType.DataRow Then 
    Dim ddlNegocio As DropDownList = e.Row.FindControl("ddlNegocio") 
End If 

末次

如果我不能找到下拉我不能加載它的價值,當我要去編輯de條目它將是空的。

有人可以幫助我嗎?

謝謝(:

回答

3

請使用RowEditing-Event,因爲只有在單擊Edit時纔會顯示您的DropDownList。 但首先,你必須重新綁定的GridView的GridView控件現在需要呈現不同的控件編輯行:

protected void gdvRegraRotationDefault_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    gdvRegraRotationDefault.EditIndex = e.NewEditIndex; 
    gdvRegraRotationDefault.DataBind(); 

    GridViewRow row = gdvRegraRotationDefault.Rows[e.NewEditIndex]; 
    DropDownList ddl = row.FindControl("ddlNegocio") as DropDownList; 

    //now do databinding for DropDownList 
} 
+0

非常感謝。(: – gabsferreira 2011-12-20 10:48:21

+0

歡迎您! – AGuyCalledGerald 2011-12-20 10:50:28

2

的FindControl已總是返回null,因爲當你在RowDataBound事件如果你想填補的DropDownList當你點擊編輯按鈕就可以得到標籤只能

。 。網格,那麼你必須使用GridViewRowEditing事件

+0

權,但在這種情況下,我不能使用FindControl方法,因爲該類GridViewEditEventArgs有不能訪問Row屬性。我應該怎麼做? – gabsferreira 2011-12-20 09:54:45

+0

您有兩個選擇,要麼將發件人解析爲GridViewRow,要麼使用作爲正在編輯的行的索引的e.NewEditIndex,然後在該行中找到該控件。 – 2011-12-20 09:59:29

+0

Dim row As GridViewRow = gdvRegraRotationDefault.Rows(e.NewEditIndex)Dim ddlNegocio As DropDownList = row.FindControl(「ddlNegocio」)//仍然返回Nothing): – gabsferreira 2011-12-20 10:13:25

0

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

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

嗯,我認爲你不明白我的問題的人,這不會幫助我。謝謝無論如何。 – gabsferreira 2011-12-20 10:36:13

+0

你無法找到你的下拉控制權 – Neha 2011-12-20 10:38:22

+0

是的,但Jan-Frederik卡爾和yahya kh已經幫助我解決我的問題,謝謝 – gabsferreira 2011-12-20 10:50:15