GridView cell textbox.Visible = true

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 53
Reputation: dotNetDummi is an unknown quantity at this point 
Solved Threads: 0
dotNetDummi's Avatar
dotNetDummi dotNetDummi is offline Offline
Junior Poster in Training

GridView cell textbox.Visible = true

 
0
  #1
Dec 12th, 2007
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.

  1. protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. DropDownList dropDown = sender as DropDownList;
  4. int selectedIndex = dropDown.SelectedIndex;
  5. GridViewRow gvr = dropDown.NamingContainer as GridViewRow;
  6.  
  7. // GridViewRow gvr = (GridViewRow)gv_retrieve.Rows[index];
  8.  
  9. DropDownList ddl = gvr.FindControl("ddlName") as DropDownList;
  10. if (ddl.SelectedValue == "others")
  11. {
  12. TextBox tbxName = (TextBox)gv_retrieve.FindControl("tbxName") as TextBox;
  13. tbxName.Visible = true;
  14. }
  15. }
Serene Joey
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dotNetDummi is an unknown quantity at this point 
Solved Threads: 0
dotNetDummi's Avatar
dotNetDummi dotNetDummi is offline Offline
Junior Poster in Training

Re: GridView cell textbox.Visible = true

 
0
  #2
Dec 12th, 2007
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.



  1. protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. DropDownList dropDown = sender as DropDownList;
  4. int selectedIndex = dropDown.SelectedIndex;
  5. GridViewRow gvr = dropDown.NamingContainer as GridViewRow;
  6.  
  7. DropDownList ddl = gvr.FindControl("ddlName") as DropDownList;
  8. if (ddl.SelectedValue == "others")
  9. {
  10. gvr.FindControl("tbxName").Visible = true;
  11. gvr.FindControl("btnAddName").Visible = true;
  12. }
  13. else
  14. {
  15. gvr.FindControl("tbxName").Visible = false;
  16. gvr.FindControl("btnAddName").Visible = false;
  17. }
  18. }
  19.  
  20. protected void gv_retrieve_RowCommand(object sender, GridViewCommandEventArgs e)
  21. {
  22. //add name
  23. if (e.CommandName == "addName")
  24. {
  25. int index = Convert.ToInt32(e.CommandArgument);
  26. GridViewRow gvr = (GridViewRow)gv_retrieve.Rows[index];
  27.  
  28. if (gvr.RowType == DataControlRowType.DataRow)
  29. {
  30. if (DataControlRowState.Edit > 0 && gvr.RowState > 0)
  31. {
  32. DropDownList ddl = gvr.FindControl("ddlName") as DropDownList;
  33. TextBox tbx = (TextBox)gvr.FindControl("tbxName") as TextBox;
  34.  
  35. if (ddl != null)
  36. {
  37. if (tbx.Text != "")
  38. {
  39. //check if name exist
  40. if (shareDAO.isNameExist(tbx.Text) == false)
  41. {
  42. ddl.Items.Add(tbx.Text);
  43. shareDAO.insertName(tbx.Text);
  44. ddl.SelectedValue = tbx.Text;
  45. gvr.FindControl("tbxName").Visible = false;
  46. gvr.FindControl("btnAddName").Visible = false;
  47. }
  48. else
  49. {
  50. Response.Write("<Script language=javascript>alert('Approval Party Name already exist!')</Script>");
  51. }
  52. }
  53. }
  54.  
  55. }
  56. }
  57. } // end of adding name
  58. }


and this is my aspx.

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

please help. thanks~
Serene Joey
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC