Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Datagrid edit command textbox reference

Datagrid edit command textbox reference

Scheduled Pinned Locked Moved ASP.NET
helpdesign
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    janetb99
    wrote on last edited by
    #1

    I'm trying to replace some text in a datagrid's textbox on edit. I've done this a thousand times, but I'm not seeing the problem. Stops on "tada" line with Object reference not set to an instance of an object. Any help appreciated. Protected Sub dgAnswers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgAnswers.ItemCommand If e.CommandName = "Edit" Then dgAnswers.EditItemIndex = e.Item.ItemIndex CType(dgAnswers.Items(e.Item.ItemIndex).FindControl("txtAnsEdit"), TextBox).Text = "tada"

    U 1 Reply Last reply
    0
    • J janetb99

      I'm trying to replace some text in a datagrid's textbox on edit. I've done this a thousand times, but I'm not seeing the problem. Stops on "tada" line with Object reference not set to an instance of an object. Any help appreciated. Protected Sub dgAnswers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgAnswers.ItemCommand If e.CommandName = "Edit" Then dgAnswers.EditItemIndex = e.Item.ItemIndex CType(dgAnswers.Items(e.Item.ItemIndex).FindControl("txtAnsEdit"), TextBox).Text = "tada"

      U Offline
      U Offline
      uoods
      wrote on last edited by
      #2

      I think you should break your statements into different parts. Do you feel pressured to stick all those functions on one single line? Doing this will allow you to see the problem more quickly, fix it and move on. This example probably has bugs in it, but try something like this:

      If e.CommandName = "Edit" Then
      dgAnswers.EditItemIndex = e.Item.ItemIndex
      Dim txt as Textbox
      txt = CType(dgAnswers.Items(e.Item.ItemIndex).FindControl("txtAnsEdit"),TextBox)
      If not isnothing(txt) then
      'since you have the object reference assigned to txt, assign the value using this alias
      txt.Text = "tada"
      Else
      'Could not find the textbox.
      End if
      End If

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups