Hi all, I have a gridview in my project.

There is a cell that contains a dropdownlist, textbox and button.
My task is when the use clicks "others" in the dropdownlist, the textbox and button should show so that they can add new value into the dropdownlist.

But I got an exception.
"Object reference not set to an instance of an object. " at tbxName.Visible = true

How can I show that?
Please help.

protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropDown = sender as DropDownList;
        int selectedIndex = dropDown.SelectedIndex;
        GridViewRow gvr = dropDown.NamingContainer as GridViewRow;

       // GridViewRow gvr = (GridViewRow)gv_retrieve.Rows[index];

        DropDownList ddl = gvr.FindControl("ddlName") as DropDownList;
        if (ddl.SelectedValue == "others")
        {
            TextBox tbxName = (TextBox)gv_retrieve.FindControl("tbxName") as TextBox;            
            tbxName.Visible = true;
        }
    }

Sorry sorry~~ I managed to do it already! it's my careless mistake. but then,
now the problem is i have to click twice at my button then the value in the textbox will be added.

is it i have to add something in page_load()?

below is my code.

protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropDown = sender as DropDownList;
        int selectedIndex = dropDown.SelectedIndex;
        GridViewRow gvr = dropDown.NamingContainer as GridViewRow;
              
        DropDownList ddl = gvr.FindControl("ddlName") as DropDownList;
        if (ddl.SelectedValue == "others")
        {
            gvr.FindControl("tbxName").Visible = true;
            gvr.FindControl("btnAddName").Visible = true;
        }
        else
        {
            gvr.FindControl("tbxName").Visible = false;
            gvr.FindControl("btnAddName").Visible = false;
        }
    }

protected void gv_retrieve_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //add name
        if (e.CommandName == "addName")
        {
            int index = Convert.ToInt32(e.CommandArgument);           
            GridViewRow gvr = (GridViewRow)gv_retrieve.Rows[index];

            if (gvr.RowType == DataControlRowType.DataRow)
            {
                if (DataControlRowState.Edit > 0 && gvr.RowState > 0)
                {
                    DropDownList ddl = gvr.FindControl("ddlName") as DropDownList;
                    TextBox tbx = (TextBox)gvr.FindControl("tbxName") as TextBox;

                    if (ddl != null)
                    {
                        if (tbx.Text != "")
                        {
                            //check if name exist
                            if (shareDAO.isNameExist(tbx.Text) == false)
                            {
                                ddl.Items.Add(tbx.Text);
                                shareDAO.insertName(tbx.Text);
                                ddl.SelectedValue = tbx.Text;
                                gvr.FindControl("tbxName").Visible = false;
                                gvr.FindControl("btnAddName").Visible = false;
                            }
                            else
                            {
                                Response.Write("<Script language=javascript>alert('Approval Party Name already exist!')</Script>");
                            }
                        }
                    }

                }
            }
        } // end of adding name
}

and this is my aspx.

<asp:TemplateField HeaderText="Name">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlName" runat="server"
                                Width="172px" DataSourceID="names" AppendDataBoundItems = "true" DataTextField="names" OnSelectedIndexChanged="ddlName_SelectedIndexChanged" AutoPostBack="True">
                            <asp:ListItem Text = "------------"></asp:ListItem>
                            <asp:ListItem Text = "Others" Value ="others" ></asp:ListItem>
                            </asp:DropDownList><br />
                            <asp:TextBox ID="tbxName" runat="server" Visible = "false" AutoPostBack="True">
                            </asp:TextBox>&nbsp;
                            <asp:Button ID="btnAddName" runat="server" Text="Add" Visible="false" CommandName="addName"  CommandArgument="<%# Container.DataItemIndex %>" />

please help. thanks~

This article has been dead for over six months. Start a new discussion instead.