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. dynamically add/change controls to datagrid

dynamically add/change controls to datagrid

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorialquestion
4 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.
  • S Offline
    S Offline
    SABhatti
    wrote on last edited by
    #1

    I have a datagrid with textboxes and dropdownlists. In one column I want to replace textbox with dropdownlist with same ID and vice versa depending on how many records found in database. I am using this code.. datagriditem.cells(index).controls.remove("textboxcontrol") and datagriditem.cells(index).controls.add("dropdownlistcontrol") this works fine, but the problem is that on next postback, it shows a textbox instead of dropdownlist. It does maintain viewstate becuase whatever i select in dropdownlist it shows in textbox. Can someone help me to keep dropdownlist after the next postback? Maybe someone can guide me another way to handle this problem? Thanks in advance... -----

    D 1 Reply Last reply
    0
    • S SABhatti

      I have a datagrid with textboxes and dropdownlists. In one column I want to replace textbox with dropdownlist with same ID and vice versa depending on how many records found in database. I am using this code.. datagriditem.cells(index).controls.remove("textboxcontrol") and datagriditem.cells(index).controls.add("dropdownlistcontrol") this works fine, but the problem is that on next postback, it shows a textbox instead of dropdownlist. It does maintain viewstate becuase whatever i select in dropdownlist it shows in textbox. Can someone help me to keep dropdownlist after the next postback? Maybe someone can guide me another way to handle this problem? Thanks in advance... -----

      D Offline
      D Offline
      Daniel Santillanes
      wrote on last edited by
      #2

      This depends on where you are removing / adding the controls... is it on ItemDataBound or ItemCreated event? I believe you are doing it in ItemDataBound since you say you do this depending on the number of records? the problem is this initialization is not done in the postback if you're doing this on the ItemDataBound event, it's not a viewstate issue. If you want to keep your customization, this has to be done in the ItemCreated event, because this event is generated when databinding and in every postback (ItemDataBound event is only raised when the DataBind method is called from the DataGrid). I hope this idea helps you some how. daniero

      S 1 Reply Last reply
      0
      • D Daniel Santillanes

        This depends on where you are removing / adding the controls... is it on ItemDataBound or ItemCreated event? I believe you are doing it in ItemDataBound since you say you do this depending on the number of records? the problem is this initialization is not done in the postback if you're doing this on the ItemDataBound event, it's not a viewstate issue. If you want to keep your customization, this has to be done in the ItemCreated event, because this event is generated when databinding and in every postback (ItemDataBound event is only raised when the DataBind method is called from the DataGrid). I hope this idea helps you some how. daniero

        S Offline
        S Offline
        SABhatti
        wrote on last edited by
        #3

        well thanks for your response, I got you, but I don't really know how to handle my problem, her it what I am trying to do: I have a datagrid with all template columns, first two columns contain textboxes (100 rows with empty textboxes and dropdownlists). Now first time user will enter value in first text box or second text box. Then user hits a button, and I am retrieving value for the empty text box from database based on the value entered in the other text box. If more than one values exist then I have to show those values in dropdownlist. So now I have to replace the empty text box with the dropdown list and on the next postback have to keep the dropdownlist. On hitting the button I loop through the datagrid items and remove the textbox control and add dropdownlist. Now here how to rebind the datagrid, I tried to get all items in an array then work with array and rebind the datagrid to that array after all tasks but get the worst results than without rebinding, datagrid goes to default view with all empty textboxes...:( see if you can help... -----

        D 1 Reply Last reply
        0
        • S SABhatti

          well thanks for your response, I got you, but I don't really know how to handle my problem, her it what I am trying to do: I have a datagrid with all template columns, first two columns contain textboxes (100 rows with empty textboxes and dropdownlists). Now first time user will enter value in first text box or second text box. Then user hits a button, and I am retrieving value for the empty text box from database based on the value entered in the other text box. If more than one values exist then I have to show those values in dropdownlist. So now I have to replace the empty text box with the dropdown list and on the next postback have to keep the dropdownlist. On hitting the button I loop through the datagrid items and remove the textbox control and add dropdownlist. Now here how to rebind the datagrid, I tried to get all items in an array then work with array and rebind the datagrid to that array after all tasks but get the worst results than without rebinding, datagrid goes to default view with all empty textboxes...:( see if you can help... -----

          D Offline
          D Offline
          Daniel Santillanes
          wrote on last edited by
          #4

          Ok, question: you repeat the same result for every textbox / dropdown after the search? If that's the case, you can keep your search text in a session variable or some other mean, and make that search and textbox / dropdown filling in the ItemCreated event. Does this help you? Are you using the ItemCreated event?

          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