Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

c# - Binding dropdownlist inside gridview edititemtemplate

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

My design:

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

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();
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...