Fetching a hidden textbox value in datagrid....
-
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.
-
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.
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; } -
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; }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
-
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
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 -
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. CleakoYep, 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
-
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
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
-
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
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