我找不到 lblCategoryName 和 ddlBlogCategoty 的控件 编辑按钮在网格视图中单击

本文关键字:按钮 网格 视图 单击 编辑按 编辑 lblCategoryName 找不到 ddlBlogCategoty 控件 | 更新日期: 2023-09-27 18:34:22

如何找到控件lblCategoryNameddlBlogCategory

这是我的代码:

protected void grdRetailStore_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblAccount = (Label)e.Row.FindControl("lblCategoryName");
            string s = lblAccount.Text;
            DropDownList ddlBlogCategory =    (DropDownList)e.Row.FindControl("ddlBlogCategoty");
            DAO.DAO dao_category = new DAO.DAO();
            DataTable dt_blog_cat = dao_category.SelectCategory();
            if (ddlBlogCategory != null)
            {
                //  ddlBlogCategory.Items.Clear();
                ddlBlogCategory.DataSource = dao_category.SelectCategory();
                ddlBlogCategory.DataTextField = "CategoryName";
                ddlBlogCategory.DataValueField = "BlogCategoryID";
                ddlBlogCategory.DataBind();
            }
        }
    }

 <asp:GridView ID="grdRetailStore" runat="server" AutoGenerateColumns="false"   DataKeyNames="PortalId" OnRowEditing="grdRetailStore_RowEditing"
                 OnRowCancelingEdit="grdRetailStore_RowCancelingEdit"      OnRowDataBound="grdRetailStore_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                            <asp:Label ID="lblId" runat="server" Text='<%# Bind("PortalId") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblId" runat="server" Text='<%# Bind("PortalId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="AdvertiserName">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                            <asp:TextBox ID="txtAdvName" runat="server" Text='<%# Bind("AdvertiserName") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblAdvName" runat="server" Text='<%# Bind("AdvertiserName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="PromoCategory">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                           <%-- <asp:DropDownList ID="" runat="server" DataTextField="Account_Name" DataValueField="Account_Id"
                                AppendDataBoundItems="true">
                                <%-- <asp:ListItem Selected="True" Text="---Select---" Value="0"></asp:ListItem></asp:DropDownList> --%>
                          <asp:TextBox ID="txtPromoCat" runat="server" Text='<%# Bind("PromoCatId") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblPromoCat" runat="server" Text='<%# Bind("PromoCatId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Start Date">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                            <asp:TextBox ID="txtPromostartdate" runat="server" Text='<%# Bind("PromoStartDate") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblPromostartdate" runat="server" Text='<%# Bind("PromoStartDate") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="End Date">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                            <asp:TextBox ID="txtPromoenddate" runat="server" Text='<%# Bind("PromoEndDate") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblPromoenddate" runat="server" Text='<%# Bind("PromoEndDate") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Html Code">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                            <asp:TextBox ID="txtHtml" runat="server" Text='<%# Bind("HtmlCode") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblHtml" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CategoryName">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                           <asp:DropDownList ID="ddlBlogCategoty" runat="server" DataTextField="CategoryName" DataValueField="BlogCategoryID"
                                AppendDataBoundItems="true">
                                 <asp:ListItem Selected="True" Text="---Select---" Value="0"></asp:ListItem></asp:DropDownList>
                           </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Edit" ShowHeader="False">
                        <HeaderStyle Width="100px" Wrap="False" />
                        <EditItemTemplate>
                            <asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update" OnClientClick="return ValidateUpdate();"></asp:LinkButton>
                            <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="Edit"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                </asp:GridView>

我找不到 lblCategoryName 和 ddlBlogCategoty 的控件 编辑按钮在网格视图中单击

更改代码隐藏并包括对 RowState 的检查。

protected void grdRetailStore_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
      if (e.Row.RowState == DataControlRowState.Normal)
      {
          Label lblAccount = (Label)e.Row.FindControl("lblCategoryName");
          string s = lblAccount.Text;
      }
      if (e.Row.RowState == DataControlRowState.Edit)
      {
          DropDownList ddlBlogCategory = (DropDownList)e.Row.FindControl("ddlBlogCategoty");
          if (ddlBlogCategory != null)
          {
              //  ddlBlogCategory.Items.Clear();
              ddlBlogCategory.DataSource = dao_category.SelectCategory();
              ddlBlogCategory.DataTextField = "CategoryName";
              ddlBlogCategory.DataValueField = "BlogCategoryID";
              ddlBlogCategory.DataBind();
          }
      }
      DAO.DAO dao_category = new DAO.DAO();
      DataTable dt_blog_cat = dao_category.SelectCategory();
  }
}