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
  1. Home
  2. Web Development
  3. ASP.NET
  4. Fetching a hidden textbox value in datagrid....

Fetching a hidden textbox value in datagrid....

Scheduled Pinned Locked Moved ASP.NET
csssysadminquestion
7 Posts 3 Posters 1 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
    just4ulove7
    wrote on last edited by
    #1

    Hi, I have a datagrid and a textbox(hidden) as an ItemTemplate. There are number of other textboxes which are visible whose values are to be saved by iterating through the grid. Now I also want to store the value of the hidden textbox but when I hide it, it gives the value as "" i.e. empty string. But if I make it visible, it wl show the value and even save it. EnableViewState=True Runat="server"> EnableViewState=True Runat="server"> This is how I am fetching its value in the code-behind. t.TaskId=((TextBox)dgItem.FindControl("txtTaskId")).Text; But t.TaskId contains "" (empty string) if the textbox is hidden, else it shows a value. What can be done abt it? Thanks.

    G 1 Reply Last reply
    0
    • J just4ulove7

      Hi, I have a datagrid and a textbox(hidden) as an ItemTemplate. There are number of other textboxes which are visible whose values are to be saved by iterating through the grid. Now I also want to store the value of the hidden textbox but when I hide it, it gives the value as "" i.e. empty string. But if I make it visible, it wl show the value and even save it. EnableViewState=True Runat="server"> EnableViewState=True Runat="server"> This is how I am fetching its value in the code-behind. t.TaskId=((TextBox)dgItem.FindControl("txtTaskId")).Text; But t.TaskId contains "" (empty string) if the textbox is hidden, else it shows a value. What can be done abt it? Thanks.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      When you set Visible to false for any element, it won't be rendered. It won't exist in the html page. As it doesn't exist, it can't retain a value. If you want a hidden field in a form, use the System.Web.UI.HtmlControls.HtmlInputHidden control. E.g. a regular input tag with type="hidden" and runat="server". <input type="hidden" id="txtTaskId" runat="server" value="<%#DataBinder.Eval(Container.DataItem,'TaskId')%>"> --- b { font-weight: normal; }

      J 1 Reply Last reply
      0
      • G Guffa

        When you set Visible to false for any element, it won't be rendered. It won't exist in the html page. As it doesn't exist, it can't retain a value. If you want a hidden field in a form, use the System.Web.UI.HtmlControls.HtmlInputHidden control. E.g. a regular input tag with type="hidden" and runat="server". <input type="hidden" id="txtTaskId" runat="server" value="<%#DataBinder.Eval(Container.DataItem,'TaskId')%>"> --- b { font-weight: normal; }

        J Offline
        J Offline
        just4ulove7
        wrote on last edited by
        #3

        Thanks a lot, I put " runat=server> in aspx file, and to access it programatically, i used t.TaskId=((HtmlInputHidden)dgItem.FindControl("txtTask")).Value; But its throwing the error Object reference not set, its not able to search for the control. Where am I going wrong? Thanks

        M 1 Reply Last reply
        0
        • J just4ulove7

          Thanks a lot, I put " runat=server> in aspx file, and to access it programatically, i used t.TaskId=((HtmlInputHidden)dgItem.FindControl("txtTask")).Value; But its throwing the error Object reference not set, its not able to search for the control. Where am I going wrong? Thanks

          M Offline
          M Offline
          Marcus J Smith
          wrote on last edited by
          #4

          e.Item.Cells(18).Text Something like that has always worked in the code behind for me. Such as the delete command or the update command when you need that id value. Just remember that the cells are 0 based. Cleako

          J 1 Reply Last reply
          0
          • M Marcus J Smith

            e.Item.Cells(18).Text Something like that has always worked in the code behind for me. Such as the delete command or the update command when you need that id value. Just remember that the cells are 0 based. Cleako

            J Offline
            J Offline
            just4ulove7
            wrote on last edited by
            #5

            Yep, that wud work in ItemDatabound event where e is DataGridItemeventArgs I want to iterate through the Datagrid and save each value in the textbox. my code is as follows: dgTimesheet is the datagrid, this is the code behind public void Save() { foreach (DataGridItem dgItem in dgTimesheet.Items) { t.TaskId=((HtmlInputHidden)dgItem.FindControl("txtTask")).Value; saveTask...(some procedure)... } } Here is the aspx code " runat=server> So I need to retrieve txtTask value for each row... Thanks

            M 1 Reply Last reply
            0
            • J just4ulove7

              Yep, that wud work in ItemDatabound event where e is DataGridItemeventArgs I want to iterate through the Datagrid and save each value in the textbox. my code is as follows: dgTimesheet is the datagrid, this is the code behind public void Save() { foreach (DataGridItem dgItem in dgTimesheet.Items) { t.TaskId=((HtmlInputHidden)dgItem.FindControl("txtTask")).Value; saveTask...(some procedure)... } } Here is the aspx code " runat=server> So I need to retrieve txtTask value for each row... Thanks

              M Offline
              M Offline
              Marcus J Smith
              wrote on last edited by
              #6

              First of all what is the t.TaskId - I guess it looks like another class's method? Also what is the purpose of saving all of the values ahead of time? I do my programming in VB not C# but it shouldnt be that totally different. Obviously you cant use the e.item.cells(1).text b/c you arent arent sending or getting a sender and arguments. http://www.c-sharpcorner.com/Code/2003/Jan/AccessDataGridVal.asp[^] That link shows what I am trying to say. That column is bound - even though it is not called bound - you dont have to access it like a hidden text field or hidden label on the form. It is a value just like any other cell so grab the text from that cell when you iterate. Hope this helps, Cleako

              J 1 Reply Last reply
              0
              • M Marcus J Smith

                First of all what is the t.TaskId - I guess it looks like another class's method? Also what is the purpose of saving all of the values ahead of time? I do my programming in VB not C# but it shouldnt be that totally different. Obviously you cant use the e.item.cells(1).text b/c you arent arent sending or getting a sender and arguments. http://www.c-sharpcorner.com/Code/2003/Jan/AccessDataGridVal.asp[^] That link shows what I am trying to say. That column is bound - even though it is not called bound - you dont have to access it like a hidden text field or hidden label on the form. It is a value just like any other cell so grab the text from that cell when you iterate. Hope this helps, Cleako

                J Offline
                J Offline
                just4ulove7
                wrote on last edited by
                #7

                Yep, the article was pretty cool. As you said, I removed the hidden field, and placed the value i need in a cell. The code goes as below <%#DataBinder.Eval(Container.DataItem,"TaskId")%> Now by iterating I am trying to grab the cells value... foreach (DataGridItem dgItem in dgTimesheet.Items) { TaskId=dgItem.Cells[8].Text; } But still it doesnt show anything in the dgItem.cells[8].text, i checked the cell no. , have kept it visible. It shows in the grid. Can u let me know where I am going wrong? Thanks -- modified at 15:50 Thursday 15th September, 2005

                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