无法使用Gridview事件在formview Edit模式中找到面板控件

时间:2014-09-19 17:50:09

标签: c# asp.net webforms

我有一个Panel visible = false。我有逻辑,如果满足条件,应该使面板可见。 我无法找到我猜测的面板,因为它不可见。也许我会尝试另一个事件,看看我是否应该在那里看到面板。像数据绑定事件一样?

UPDATE **************** 根据下面的评论使用display:none 优秀的建议。我当然认为它会解决我的问题。但是在做了好几件事后,我无法得到面板样式=" display:none"甚至隐藏面板。我知道我在编辑中的正确面板,因为我在里面更改了文本。

我已经完成了一些事情,例如禁用的viewstate,代码前面板中的样式硬编码,add.attribute(" style",block)位于itemcommand事件的顶部,放入一个testPanel认为我有某种东西覆盖了能见度......

更新#2 *************** 我完全难过......这一切都应该工作......所以,我做了一些进一步的谷歌搜索。现在我可能遗漏了一些重要的信息,不知道它是否重要。 - 我有一个网格视图 - 下面的表格由selectedindex事件产生 - 我尝试从以下帖子实施,但没有快乐 I cannot change control's visibility on C# code

测试结果: - 编辑命令正在被正确触发 - 我删除了除太平洋控制之外的任何控件 - 可以更改更新面板外部的面板 - 我更改了更新面板" UpdateMode"有条件地确保没有其他东西被覆盖 即使我知道这个面板仅在此时存在,因为我刚刚添加它。

这与gridview有关吗?我在下面添加了它

的.aspx

<asp:Panel ID="pnlCollectionEventAll" runat="server">
      <asp:GridView ID="gvCollectionEventDetail" runat="server" SkinID="annualReportGridview" AllowSorting="True"
      AutoGenerateColumns="False" CellPadding="4" DataSourceID="odsMRWCollectionDetailByReportId" EnableSortingAndPagingCallbacks="false" 
      DataKeyNames="MRWCollectionReportDetailId" ForeColor="#333333" GridLines="Vertical" Width="720px" 
      ShowFooter="True" OnRowDataBound="gvCollectionEventDetail_RowDataBound" OnSelectedIndexChanged="gvCollectionEventDetail_SelectedIndexChanged">
            <Columns>
                    <asp:CommandField ButtonType="button" ShowSelectButton="true" SelectText="Select"
                                        ItemStyle-HorizontalAlign="Left" ControlStyle-ForeColor="#1A3B69" />
                    <asp:TemplateField HeaderText="Disposal Method" SortExpression="DisposalMethodCode"
                                        FooterStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                              <asp:Label ID="lblDisposalMethod" runat="server" Text='<%# Eval("DisposalMethodCode") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Qty disposed (lbs)" FooterStyle-HorizontalAlign="Left"
                                        HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblWasteQty" runat="server" Text='<%# Eval("WasteQuantity") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left" />
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Contractor" FooterStyle-HorizontalAlign="Left"
                                        HeaderStyle-HorizontalAlign="Left">
             <ItemTemplate>
                    County:<asp:Label ID="lblSourceCounty" runat="server" Text='<%# Eval("CountyName") %>'></asp:Label>&nbsp;
                    State:<asp:Label ID="lblSourceState" runat="server" Text='<%# Eval("StateCode") %>'></asp:Label>&nbsp;
                    Country:<asp:Label ID="lblSourceCountry" runat="server" Text='<%# Eval("CountryName") %>'></asp:Label>&nbsp;<br />
             </ItemTemplate>
             <ItemStyle HorizontalAlign="Left" />
             </asp:TemplateField>
             </Columns>
             <EmptyDataTemplate>
                 No MRW Collection details have been entered for this report.
             </EmptyDataTemplate>
            </asp:GridView>
         </asp:Panel>
         <asp:Button ID="btn_addWasteCollectedRecord" runat="server" Text="Add a New Waste Collected Record" OnClick="btn_addWasteCollectedRecord_Click" />
         <asp:Button ID="btn_addWasteCollectedRecordDone" runat="server" Text="Done Adding Waste Collected Records" Visible="false" OnClick="btn_addWasteCollectedRecordDone_Click" />
                        <br /><br />
     <asp:FormView ID="fvWasteCollected" runat="server" SkinID="annualReportFormview" ViewStateMode="Disabled"
          HeaderText="Selected Waste Collected Detail" DataKeyNames="MRWCollectionReportDetailId" DefaultMode="Insert"
          DataSourceID="odsMRWWasteCollectedDetail" OnDataBound="fvWasteCollected_DataBound" 
          OnItemCommand="fvWasteCollected_ItemCommand" OnItemInserted="fvWasteCollected_ItemInserted"
          OnItemUpdated="fvWasteCollected_ItemUpdated" OnItemDeleted="fvWasteCollected_ItemDeleted">
          <EditItemTemplate>
               <table class="detailstable">
                     <asp:Panel ID="pnlOtherContractor" runat="server" Visible="false">
                          <tr>
                              <th></th>
                                   <td align="left">
                                       Blah Blah
                                   </td>
                           </tr>
                     </asp:Panel>
                 </table>
    </EditItemTemplate>

aspx.cs

protected void gvCollectionEventDetail_SelectedIndexChanged(object sender, EventArgs e)
    {
        fvWasteCollected.ChangeMode(FormViewMode.ReadOnly);
        fvWasteCollected.DataBind();
        pnlMWRCOllectionForm.Visible = true;

        //hide the compost produced add linkbutton
        btn_addWasteCollectedRecord.Visible = false;
        btn_addWasteCollectedRecordDone.Visible = false;

        // hide the edit delete button on fvFeedstockDetail if the annual report already submitted 
        //and the logon user is not ecy staff authorized to edit report ##################################
        Label myLblSubmitDate1 = (Label)fvRptHeader.FindControl("lblSubmitDate");

        LinkButton lbtnWasteCollectedEdit = (LinkButton)fvWasteCollected.FindControl("lbtnWasteCollectedEdit");
        LinkButton lbtnWasteCollectedDelete = (LinkButton)fvWasteCollected.FindControl("lbtnWasteCollectedDelete");

        if (!String.IsNullOrEmpty(myLblSubmitDate1.Text.Trim()))
        {
            lbtnWasteCollectedEdit.Visible = false;
            lbtnWasteCollectedDelete.Visible = false;
            btn_addWasteCollectedRecord.Visible = false;
        }
    }
protected void fvWasteCollected_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
      if (e.CommandName.Equals("Edit"))
        {

            ValidationSummaryWasteDetail.ValidationGroup = "WasteReceivedDetail";
            fvWasteCollected.ChangeMode(FormViewMode.Edit);
            gvCollectionEventDetail.Columns[0].Visible = false;
            DataRowView rowView = (DataRowView)fvWasteCollected.DataItem;
            DropDownList ddl = (DropDownList)fvWasteCollected.FindControl("ddl_MRWContractor");
            //Tried Panel pnlOtherContractor = (Panel)fvWasteCollected.FindControl("pnlOtherContractor"); 
            FormViewRow myFormRow = fvWasteCollected.Row;
            Panel pnlOtherContractor = (Panel)myFormRow.FindControl("pnlOtherContractor");

            var s_contractorId = rowView["MRWContractorId"].ToString();
            if (s_contractorId == "0")
            {
                pnlOtherContractor.Visible = true;
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

应该有一个按钮触发表单视图上的编辑/更新。检查它是否具有CommandName =&#34;编辑&#34;作为其属性。

答案 1 :(得分:0)

尝试100%工作和测试:

即使您设置了Visible="false"Visible="true"

 Style="display: none" 

无关紧要您肯定会从FormView's行获得Panel 使用这个

 FormViewRow myFormRow = fvWasteCollected.Row;
 Panel pnlOtherContractor1 = (Panel)myFormRow.FindControl("pnlOtherContractor");

注意:您的问题在我的电脑上工作正常。我建议您设置断点并检查每个控件是否为空或myFormRow是否为空{{1事件。

MSDN REFERENCE

答案 2 :(得分:0)

解决了它。表单在生命周期中还为时过早。在Databound事件中,检查currentMode是否在编辑中,然后我对数据进行了检查

protected void fvWasteCollected_DataBound(object sender, EventArgs e)
    {
        FormView formview = fvWasteCollected;
        FormViewRow row = fvWasteCollected.Row;
        DataRowView rowview = (DataRowView)fvWasteCollected.DataItem;
        Panel pnlOtherContractor = (Panel)fvWasteCollected.FindControl("pnlOtherContractor2");

        if (fvWasteCollected.CurrentMode == FormViewMode.Edit)
        {
            var s_contractorId = rowview["MRWContractorId"].ToString();
            if (s_contractorId == "0")
            {
                pnlOtherContractor.Visible = true;
            }
            else
            {
                pnlOtherContractor.Visible = false;
            }
        }

    }