【问题标题】:Binding dropdownlist inside gridview edititemtemplate在gridview edititemtemplate中绑定下拉列表
【发布时间】:2012-08-28 03:45:31
【问题描述】:

我无法绑定 edititem 模板中的下拉列表。当我尝试访问它时,我得到空引用。

我的设计:

<asp:TemplateField HeaderText ="Category">
    <ItemTemplate >
    <asp:Label ID="drpcategory" Text ='<%#Bind("category") %>' runat ="server" />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList ID="drpcategory1"  AppendDataBoundItems="True" runat="server" >
        </asp:DropDownList>
    </EditItemTemplate>
</asp:TemplateField> 

我的代码在后面:

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
    gv_table1.EditIndex = e.NewEditIndex;
    DropDownList drpcategory1 = ((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1"));
    //BindDropDown(drpcategory1);
    dt = con.GetData("Select category_name from category");

    String str = gv_table1.Rows[e.NewEditIndex].FindControl("drpcategory1").GetType().ToString();
    //((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1")).DataSource = dt;
    drpcategory1.DataSource = dt;
    drpcategory1.DataTextField = "category_name";
    drpcategory1.DataValueField = "category_name";
    drpcategory1.DataBind();

    this.setgrid();
}

我已经尝试在网上查找并尝试了很多东西都徒劳无功。我是asp新手。提前致谢。我希望仅在用户进入编辑模式时绑定下拉菜单。

【问题讨论】:

  • 什么是 gv_table1 ?我认为这可能是问题所在。请检查
  • 不要使用 Cells[8].FindControl("drpcategory1")row.FindControl("drpcategory1") 因为 NamingContainer 是行而不是单元格。你的方式更容易出错。
  • 我只使用 row.findcontrol。我也使用了单元格。我都尝试了但没有得到想要的结果。
  • VeeKeyBee 那是我的 gridview id man。

标签: c# asp.net gridview


【解决方案1】:

事件RowEditing 恰好在编辑行之前发生。

您应该改用RowDataBound 事件。

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (gv.EditIndex == e.Row.RowIndex && 
       e.Row.RowType==DataControlRowType.DataRow) 
   {       
       DropDownList drpcategory1 = (DropDownList)e.Row.FindControl("drpcategory1"); 
       //bind the control
   }
}

【讨论】:

  • Drpcategory1 值只有在我放置断点并检查值时才为空。我按照你说的做了。
【解决方案2】:

您必须使用 RowDataBound 事件来绑定已编辑行的下拉控件。请在 RowDataBound 事件中使用以下方法。

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList drpcategory1 = (DropDownList)e.Row.FindControl("drpcategory1");
            DataTable dt = con.GetData("Select category_name from category");
            drpcategory1.DataSource = dt;
            drpcategory1.DataTextField = "category_name";
            drpcategory1.DataValueField = "category_name";
            drpcategory1.DataBind();
        }
    }

Hope this will help you.

【讨论】:

    【解决方案3】:

    后面的代码:测试代码并在编辑模式下设置下拉列表选择值

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddList= (DropDownList)e.Row.FindControl("drpcategory1");
                //bind dropdown-list
                DataTable dt = con.GetData("Select category_name from category");
                ddList.DataSource = dt;
                ddList.DataTextField = "category_name";
                ddList.DataValueField = "category_name";
                ddList.DataBind();
                
                DataRowView dr = e.Row.DataItem as DataRowView;
                //ddList.SelectedItem.Text = dr["category_name"].ToString();
                ddList.SelectedValue = dr["category_name"].ToString();
            }
        }
    }
        
    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv.EditIndex = e.NewEditIndex;
        gridviewBind();// your gridview binding function
    }
    

    【讨论】:

    • 得到了想要的东西 man.But DataRowView dr = e.Row.DataItem as DataRowView; ddList.SelectedItem.Text = dr["category_name"].ToString();这两行显示错误删除我执行的这两行并得到输出老兄。谢谢兄弟
    • @Prashanth:如果有帮助的话很好,你可以粘贴错误消息吗?
    • 伙计,我纠正了这个错误,这只是一个命名问题,但现在兄弟一切都很好,但现在下拉列表中的第一个元素在编辑模式下被重写为标签值 man(即)我有重复一个元素,也失去另一个元素。希望你明白我的意思。
    • @Prashanth:兄弟,检查我的更新答案是否已解决该问题,如果有帮助,请标记 upvote :)
    • :伙计,我刚刚也修复了它,伙计。我正要更新你的答案,但你更新了伙计,谢谢伙计。
    【解决方案4】:
    protected void gvProject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                string Active = "";
                if (e.Row.DataItem != null)
                { 
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    {
                        Label lblEditActive = (Label)e.Row.FindControl("lblUP_ET_ActiveStatus");
                        if (lblEditActive.Text != string.Empty)
                        {
                            Active = lblEditActive.Text.Trim();
                        }
    
                        DropDownList ddlActive = (DropDownList)e.Row.FindControl("ddlUP_ET_ActiveStatus");
                        ddlActive.Items.Clear();
                        ddlActive.Items.Add("True");
                        ddlActive.Items.Add("False"); 
                        ddlActive.DataBind(); 
                        ddlActive.Items.FindByText(Active).Selected = true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }       
    

    【讨论】:

      【解决方案5】:

      我是这样做的。其中,Name 和 Id 是 Company 对象的两个字段:

      HTML 代码:

      <asp:TemplateField HeaderText="Công ty">
          <EditItemTemplate>
              <asp:DropDownList ID="ddlCompanyEdit" DataSource="<%# PopulateddlCompanyEdit() %>" DataValueField="Id" DataTextField="Name" runat="server"></asp:DropDownList>
          </EditItemTemplate>
          <ItemTemplate>
              <asp:Label ID="lbCompany" runat="server" Text='<%#Bind("Company") %>'></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
      

      后面的C#代码:

      protected IEnumerable<Company> PopulateddlCompanyEdit()
      {
          using (var bkDb = new BrickKilnDb())
          {
              return bkDb.Companies.ToList();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-28
        • 2011-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        相关资源
        最近更新 更多