Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var test = ((HiddenField)lstDetails.FindControl("HiddenField1")).Value;

But i get error "Object reference not set to an instance of an object."


Thanks in advance.



XML
<asp:ListView ID="lstDetails" runat="server" ItemPlaceholderID="itemPlaceHolder"
                                     OnSelectedIndexChanging="lstDetails_SelectedIndexChanging"
                                     OnSelectedIndexChanged="lstDetails_SelectedIndexChanged" >

                                    <LayoutTemplate>
                                        <table style="width:1074px;">
                                            <thead>
                                                <tr class="tableHead">
                                                    <th style="text-align: center; width: 35px;">Sl</th>
                                                    <th style="text-align: center; width: 850px;">Leave Approval</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
                                            </tbody>
                                        </table>
                                    </LayoutTemplate>
                                    <ItemTemplate>
                                        <table class="tablemain" style="position:relative; left:66px;">
                                            <tr>
                                                <td>

                                                </td>
                                            </tr>
                                            <tr >
                                                <td class="tablecontentTitleSmall">Leave #</td>
                                                <td class="tablecontent">
                                                    <asp:HiddenField ID="hfSearchID" Value='<%#Eval("EssDocAppNo") %>' runat="server" />
                                                    <asp:TextBox ID="txtID" runat="server" CssClass="formTextBox" Text='<%#Eval("leave#") %>' Enabled="false"></asp:TextBox>
                                                </td>
                                                <td class="tablecontentTitleSmall">Employee Name</td>
                                                <td colspan="5" class="tablecontent">
                                                    <asp:TextBox ID="txtEmployeeName" runat="server" CssClass="formTextBox" Text='<%#Eval("FullName") %>' Enabled="false" Width="668px"></asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="tablecontentTitleSmall">Date
                                                </td>
                                                <td class="tablecontent">
                                                    <asp:TextBox ID="txtApplyDate" runat="server" CssClass="formTextBox" Text='<%#Eval("ladate") %>' Enabled="false"></asp:TextBox>
                                                </td>
                                                <td class="tablecontentTitleSmall">From
                                                </td>
                                                <td class="tablecontent">
                                                    <asp:TextBox ID="txtFromDate" runat="server" CssClass="formTextBox" Text='<%#Eval("leavestart") %>' Enabled="false"></asp:TextBox>
                                                </td>
                                                <td class="tablecontentTitleSmall">To
                                                </td>
                                                <td class="tablecontent">
                                                    <asp:TextBox ID="txttoDate" runat="server" CssClass="formTextBox" Text='<%#Eval("leaveend") %>' Enabled="false"></asp:TextBox>
                                                </td>
                                                <td class="tablecontentTitleSmall">No of Days
                                                </td>
                                                <td class="tablecontent"  style="width:63px;">
                                                    <asp:TextBox ID="txtNoofDays" runat="server" CssClass="formTextBox" Text='<%#Eval("noofdays") %>' Enabled="false"></asp:TextBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="tablecontentTitleSmall"></td>
                                                <td class="tablecontent">
                                                     <span>
                                                <asp:ImageButton ID="imgApprove" Width="30" Height="30" ToolTip="Approve" OnClick="imgApprove_Click" CommandName="Select"  CommandArgument="approve" ImageUrl="~/Styles/images/Approve.png" PostBackUrl='#' runat="server" /></span>

                                            <span>
                                                <asp:ImageButton ID="imgDenied" Width="30" Height="30" ToolTip="Deny" CommandName="Select" CommandArgument="deny" ImageUrl="~/Styles/images/Denied.png" PostBackUrl='#' runat="server" /></span>
                                                </td>
                                                <td class="tablecontentTitleSmall">Reason
                                                </td>
                                                <td class="tablecontent">
                                                    <asp:DropDownList ID="ddlReason" runat="server" Width="130px" CssClass="formTextBox">
                                                        <asp:ListItem>Eligible</asp:ListItem>
                                                        <asp:ListItem>Not Eligible</asp:ListItem>
                                                    </asp:DropDownList>
                                                </td>
                                                <td colspan="4" class="tablecontent">
                                                    <asp:TextBox ID="txtComment" runat="server" CssClass="formTextBox" Width="520px" TextMode="MultiLine"></asp:TextBox>
                                                </td>

                                            </tr>
                                        </table>
                                        <div style="height: 1px; width: 1007px; background-color: black; position: relative; left: 67px">
                                        </div>
                                    </ItemTemplate>
                                </asp:ListView>







/////////////////////////////////////////////////////////////////////////


select d.EssDocAppNo,(e.EmpFName+' '+ e.EmpMName+' '+e.EmpLName)as FullName,(l.essdoccode+' '+CONVERT(varchar(10), l.EssLaNo))as leave# ,l.ladate,l.leavestart,l.leaveend,l.noofdays from MstEmployee e,EssTrnLeaveApplication l,EssTrnDocumentApproval d,UtlWorkFlow w where l.EmpCode=4092 and e.EmpCode=4092and e.EmpCode=4092 and d.ApprovalStatus='P' and w.Authority1=2068;
Posted
Updated 12-Dec-14 21:21pm
v3
Comments
DamithSL 13-Dec-14 2:41am    
where you data bind to listview?
Kochathu Thomas Tinu 13-Dec-14 2:48am    
Yes. bind values to list view. I need to know that the value in the hidden field is possible to get on page load. The hidden field in is kept in the ListView.
DamithSL 13-Dec-14 2:58am    
without see the code it is difficult to tell, if the control loaded and data bind operation completed and also you have added hidden field in correct template ( if you add in edit template then you can get the control in edit mode only etc..)
then there is a possibility of finding your control. but we don't have access to your code or read your mind :)
You posted the mark up. DamithSL is asking for binding code.

1 solution

you have hidden field called hfSearchID but you searching for HiddenField1. correct that first.
after you binding data try below
C#
var test = ((HiddenField)lstDetails.Items[0].FindControl("hfSearchID")).Value;


note that you have hidden fields for each item, above will give the first list item hidden control
 
Share this answer
 
Comments
Kochathu Thomas Tinu 13-Dec-14 3:34am    
i am just given as a test name not the actual name


var EssDocAppNo = ((HiddenField)lstDetails.Items[0].FindControl("hfSearchID")).Value;

i used the same code but the value is always showing null.
Kochathu Thomas Tinu 13-Dec-14 5:29am    
Thanks DamithSL for ur support. I solved it myself
DamithSL 13-Dec-14 5:32am    
Glad to hear that, Self learner :)
why don't you post your findings as new answer. in future it will help to someone with slimier issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900