0
事件沒有觸發我在InsertItemTemplate中有一個LinkButton,當點擊時,應該在InsertItemTemplate中顯示一個隱藏的DropDownList。但是,它似乎並沒有工作,但它會說,單擊LinkButton時在Formview外改變標籤的文本。事件正在觸發,但在InsertItemTemplate中使DropDownList可見的部分沒有任何作用。代碼如下:事件沒有按預期控制在formview asp.net
的.aspx:
<asp:FormView ID="formViewNewRecord" runat="server">
<InsertItemTemplate>
<asp:DropDownList ID="ddlAddSelection2" runat="server" DataSourceID="dSource1" DataTextField="Users" DataValueField="Users" AppendDataBoundItems="true" Visible="false">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:LinkButton runat="server" ID="lbAddAnother" OnClick="lbAddAnother_Click">+Add Another</asp:LinkButton>
</InsertItemTemplate>
</asp:FormView>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
C#:
protected void lbAddAnother_Click(object sender, EventArgs e)
{
DropDownList addSelection2 = (DropDownList)formViewNewItem.Row.Cells[0].FindControl("ddlAddSelection2");
addSelection2.Visible = true;
Label2.Text = addSelection2.ID;
}
嗨@Tombala我更新了我的代碼。我添加了.Row.Cells [0],但它仍然不起作用。我還添加了標籤來顯示控件的ID。單擊LinkButton時,標籤顯示「ddlAddSelection2」。無論我保留還是忽略.Row.Cell [0]一塊,但仍然沒有發生控制,我試圖使其可見。 –
我會做的是在鏈接的討論中還提出了什麼建議:在該行上中斷並使用監視窗口或立即窗口來確定您需要的控件位於控件樹中的位置。然後,您可以使用正確的路徑來控制。 – Tombala
如果我正確地理解了你,你似乎暗示FindControl只是找不到控件,因爲FindControl()中正在使用的路徑(控件名稱)。對我來說這沒有意義,因爲它顯示控件的ID,它會在我放在formview之外的標籤中找到。如果我把一個不正確的控件名稱,例如'.FindControl('ddlAddSelection23')',它會給我一個'對象引用未設置爲對象的實例'。錯誤。我在想,我遇到的這個問題與頁面生命週期有關? –