locked
hidden field cannot access in code behind RRS feed

  • Question

  • User-1821202846 posted

    I am trying to find a control which is a hidden field within a repearter I need to do this in a button that is within the repeater

    <asp:Repeater ID="rptDocumentsProposal" runat="server">
                                    <ItemTemplate>
                                        <div class="col-md-6 sidesthick">
                                            <div class="windowbox">
                                                <div class="pull-left">
                                                    <h2>
                                                        <asp:Label runat="server" Text='<%# Eval("documentTitle") %>' /></h2>
                                                </div>
                                                <div class="pull-right icon-doc">
                                                    <asp:Label ID="IconDocument" runat="server" />
                                                </div>
                                                <div style="clear: both"></div>
    
                                                <div>
                                                    <p>
                                                        Name:
                                        <asp:Label runat="server" Text='<%# Eval("documentTitle") %>' />
                                                        <br />
                                                        Description:
                                        <asp:Label runat="server" Text='<%# Eval("documentDescription") %>' />
                                                        <br />
    
                                                        Type:
                                                              <asp:Label ID="lblType" runat="server" Text='<%# Eval("type") %>' />
     
                                                                                      
                                                        <asp:HiddenField ID="currentFileName" Value='<%# Eval("serverPath") %>' runat="server" />
                                                        <br />
                                                    </p>
                                                </div>
                                                <div class="center">
                                                       <dx:ASPxButton ID="btnDownloadDocument" CssClass="btn btn-default" style="width: 45%;" runat="server" Text="Download Document" Native="true" ClientIDMode="Static"></dx:ASPxButton>
                                                       <dx:ASPxButton ID="btnViewDocumentBrowser"     CssClass="btn btn-default" style="width: 45%;" runat="server" OnClick="btnViewDocumentBrowser_Click" Text="View in Browser" Native="true" ClientIDMode="Static"></dx:ASPxButton>
                                               
                                                  
                                                </div>
                                            </div>
                                        </div>
                                    </ItemTemplate>
     </asp:Repeater>

    It is the value in the but no matter what i do                 

      <asp:HiddenField ID="currentFileName" Value='<%# Eval("serverPath") %>' runat="server" />

    protected void btnViewDocumentBrowser_Click(object sender, EventArgs e)
            {
     
                HiddenField hf = (HiddenField)rptDocumentsProposal.FindControl("currentFileName");
    
                if (hf != null)
                { 
                        string filename = hf.Value.ToString();
                        Response.Clear();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", "inline;filename=" + filename);
                        Response.TransmitFile(filename);
                        Response.End();
                }
    }

    I am just getting a null reference error at this ocassion.

    Regards

    Tuesday, September 13, 2016 2:36 PM

Answers

  • User1738843376 posted

    I dont reacal how to fetch the control inside the repeater, altough i have done it in the past, but  you can always use a commandARgument on the button with teh value that you are currently writting to the hidden field... that way you'll get the value much easier...

    <asp:button id="myBtn" text="myButton" CommandArgument="<%# Eval("serverPath") %>" OnCommand="MyCommandFunction" runat="server" />

    and the insitead of a simple Sub to handle the click event, you use a protected sub

    Protected Sub MyCommandFunction(sender as object, e as commandEventArgs)
        'Fetch the value and use it by calling e.commandArgument that will give you the value
    Response.write("This is the value that i previously was writting to an hidden field: " & e.commandArgument) End Sub
    
    

    much easier and much lighter code than having to iterate through all items inside the repeater

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 13, 2016 4:32 PM
  • User1738843376 posted

    I myself dont usualy use the binding form that you were using and i inlcuded in my example...

    <%# Eval("serverPath") %>

    but rather

    <%# DataBinder.Eval(Container.DataItem, "serverPath") %>

    not sure if it helps, though

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 12:17 PM

All replies

  • User-718146471 posted

    Rogue, I found an answer to your question on Stack Overflow. According to "Phill"

    Change AdminGetAllRPT.FindControl...
    
    to
    
    e.Item.FindControl...
    
    Edit: Thanks for the votedown on a correct answer.
    
    But yes e.Item.FindControl is what you want because what you're trying to do is not an event on the button, it's an event on the ItemCommand of the repeater.
    
    <asp:Repeater OnItemCommand="MyButtonCommandEvent" ID="AdminGetAllRPT" runat="server" OnLoad="AdminGetAllRPT_Load">
    
    Then in the code-behind you can setup the event
    void MyButtonCommandEvent(Object src, RepeaterCommandEventArgs e) { //My event that is raised for each button pressed in the RepeaterItem var poo = e.Item.FindControl("myhiddenfieldid") as HiddenField; } Done... To iterate over all RepeaterItems, you can have a button outside of the repeater (or maybe in the footer or something,
    this is not a repeater ItemCommand)
    foreach(var item in AdminGetAllRPT.Items) { if (item.ItemType == RepeaterItemType.Item || item.ItemType == RepeaterItemType.AlternatingItem) { var hiddenField = item.FindControl("hiddenFieldid") as HiddenField; //Do Stuff } } From: http://stackoverflow.com/questions/4435766/repeater-with-hiddenfield

    Tuesday, September 13, 2016 4:27 PM
  • User1738843376 posted

    I dont reacal how to fetch the control inside the repeater, altough i have done it in the past, but  you can always use a commandARgument on the button with teh value that you are currently writting to the hidden field... that way you'll get the value much easier...

    <asp:button id="myBtn" text="myButton" CommandArgument="<%# Eval("serverPath") %>" OnCommand="MyCommandFunction" runat="server" />

    and the insitead of a simple Sub to handle the click event, you use a protected sub

    Protected Sub MyCommandFunction(sender as object, e as commandEventArgs)
        'Fetch the value and use it by calling e.commandArgument that will give you the value
    Response.write("This is the value that i previously was writting to an hidden field: " & e.commandArgument) End Sub
    
    

    much easier and much lighter code than having to iterate through all items inside the repeater

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 13, 2016 4:32 PM
  • User-1821202846 posted

    Would command argument not be visible if the person went view source i thought point of hidden field was that it got encrypted into the page view state ?

    Wednesday, September 14, 2016 7:42 AM
  • User1738843376 posted

    Not sure about the viewstate, but an hidden field is always present on the page, even though is not visible and you can see its value if you choose page source. If you want the value to be unreadable to users, then mayb you should encrypt it before assigning it to either the hidden field or the commandARgument, and then when you fetch it, decrypt it, otherwise users will always have access to it (they'll always have access to it, just wont make anything of it since its encrypted).

    To put it into other words, an hidden field is hidden from the layout, but not from the source.

    Wednesday, September 14, 2016 9:47 AM
  • User-1821202846 posted

    I got this

    Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.


    So i dont think i want to be switching this off to be honest.

    Wednesday, September 14, 2016 3:03 PM
  • User1738843376 posted

    Dont know exactely why that is being fired, ut i suspect thre must be a value that is being postback that contains something that the ASP engine is considering malicious, maybe some content that has greater thatn or smaller then symbols that might be interpreted as htlm being posted... this might have to to with the way you are binding the value to the hidden field or the command Argument on the button

    Thursday, September 15, 2016 12:15 PM
  • User1738843376 posted

    I myself dont usualy use the binding form that you were using and i inlcuded in my example...

    <%# Eval("serverPath") %>

    but rather

    <%# DataBinder.Eval(Container.DataItem, "serverPath") %>

    not sure if it helps, though

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2016 12:17 PM
  • User-1821202846 posted

    Thanks for the repy I am trying to render a pdf in the page on the button click

     protected void btnViewDocumentBrowser_Click(object sender, EventArgs e)
      {
     
                HiddenField hf = (HiddenField)rptDocumentsProposal.FindControl("currentFileName");
    
                if (hf != null)
                { 
                        string filename = hf.Value.ToString();
                        Response.Clear();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", "inline;filename=" + filename);
                        Response.TransmitFile(filename);
                        Response.End();
                }
        }

    Thursday, September 15, 2016 12:24 PM
  • User1738843376 posted

    have you tried commenting all the PDF rendering code and test the page to see if it still fires the exception you mentioned earlier? Also, i believ ethat for you to render a PDF, you need to do it with an external library... there are several free ones around the web... i believe the one i've used in the past is called itextsharp

    Thursday, September 15, 2016 1:00 PM