GridView控件的EditItemTemplate里面绑定的DropDownList [英] Binding dropdownlist inside gridview edititemtemplate

查看:167
本文介绍了GridView控件的EditItemTemplate里面绑定的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法对我的DropDownList present绑定在edititem模板。我得到空引用,当我尝试访问它。

I'm not able to bind my dropdownlist present in edititem template . I am getting null reference when i try to access it.

我的设计:

<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> 

我的code背后:

My code behind:

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的。提前致谢。我想下拉列表,只有当用户进入编辑模式的约束。

I've tried looking on the net and tried many things in vain. I am new to asp. Thanks in advance. I would like the dropdown to be bound only when user enters edit mode.

推荐答案

code背后:测试code,也可以设置下拉列表中选择值在编辑模式

Code Behind: Tested Code and also set dropdown-list selected value on edit mode

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
}

详细博文:如何绑定掇下来的GridView编辑模板中列表

Detailed Blog Article : How to bind drop-down list inside gridview edit template

这篇关于GridView控件的EditItemTemplate里面绑定的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆