OnServerClick not taking an argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bhavs
    New Member
    • Sep 2008
    • 16

    OnServerClick not taking an argument

    HI All,

    I am having a client side code :

    Code:
    <asp:HiddenField ID="hiddenfield1" runat="server" Value='<%# Eval("Id") %>' />
    <a runat="server" id="hlViewPropertyAddressDetail" onserverclick="logstats(hiddenfield1)">
    and in the Code Behind i have a logstats function as
    Code:
    public void logstats(string id)
    {
    ....
    .
    .
    .
    .
    }
    I am getting an error which says hiddenfield1 does not exist in current context, for the onclicksrrver event.
    CAn anyone help me with the error.
    Many Thanks,
    Bh
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Your HiddenField is an ASP.NET control.
    When it is rendered in the browser it may contain a different name than what you have typed. You are attempting to call Server Side code from an HTML hyperlink...pas sing that code a String... I don't understand what you are trying to do.

    What are you trying to do because the code you have posted isn't making sense?

    I would recommend using a LinkButton instead of an HTML "<a>" link since you want to execute Server Side code when the link is called. I think this would solve all of your problems.

    Comment

    • shweta123
      Recognized Expert Contributor
      • Nov 2006
      • 692

      #3
      Hi,

      In order to remove the error that you are getting please make the following modification in the code :

      Code:
       
      <script language="c#" runat="server"> 
      void click(Object s, EventArgs e) 
      {
        //call required function on the click of hyperlink
         logstats("hiddenfield1");
      } 
       
       
      public void logstats(string id)
      {
        //code for this function
      }
      </script>
      Now you can call the click function onserverclick of hyperlink control.
      Code:
       
       
      <a runat="server" id="hlViewPropertyAddressDetail" onserverclick="click"/>

      Comment

      • Bhavs
        New Member
        • Sep 2008
        • 16

        #4
        Thanks Shwetha,

        I am using the same solution now.
        But the problem is finding the value of the Hiddenfield control. As the hidden field is inside a repeater.

        I am using
        Hidden Field hide = (Hidden Field)Repeater. FindControl("hi ddenfield1
        ]but I am geeting the value as null
        Is ther any way to retrive a Hidden Field value placed inside a Repeater control??

        Comment

        • liawcv
          New Member
          • Jan 2009
          • 33

          #5
          Let make the concept right: A Repeater maintains a RepeaterItemCol lection which contains 0 or many RepeaterItem. And each RepeaterItem is created / rendered based on the <ItemTemplate > that we defined in the .aspx file.

          Let assume that we have a Repeater with a simple structure as:

          Code:
          <asp:Repeater runat="server" ID="Repeater1" ...>
          <ItemTemplate>
             <asp:Label runat="server" ID="Label1" ... />
          </ItemTemplate>
          </asp:Repeater>
          During runtime, depends on the data source, there may have 0 or many Label1 being generated in the Repeater. For this reason, the following line is not work:

          Repeater1.FindC ontrol("Label1" );

          Firstly, there are many Label1, ASP.NET doesn't know which Label1 we are asking for. Secondly, THE CONTEXT IS JUST NOT RIGHT! That's why you get a null all of the time.

          Recall that all RepeaterItem are maintained under the Repeater's RepaterItemColl ection, and our Label1 is in fact a control under its particular RepeaterItem. So, the hierarchy would be:

          Repeater --> RepeaterItemCol lection --> RepeaterItem --> Label1

          Back to the example: In order to find Label1 in the 1st RepeaterItem, we should do this:

          Repeater1.Items[0].FindControl("L abel1");

          In order to retrieve all the Label1's Text, we should do this:

          Code:
          Label lbl;
          foreach(RepeaterItem i in Repeater1.Items)
          {
             lbl = (Label)i.FindControl("Label1");
             Response.Write(lbl.Text + "<br />");
          }
          So, the key point is THE CONTEXT. I didn't really answer your question. But hope this could help you to understand "where is the right place" to find your HiddenField.

          By the way, I am wondering how you would know the right RepeaterItem index so that you can find the exact HiddenField that you want, out of many other HiddenFields in your Repeater.

          Consider to use LinkButton and Repeater's ItemCommand event if you still can't get your problem solved.

          Comment

          • liawcv
            New Member
            • Jan 2009
            • 33

            #6
            Let me guess: The purpose of your HiddenField is to hold the Id so that your logstats() function is able to figure out which Id to be processed. And your <a> is to provide an hypertext-liked interface to the user, so when it is clicked, the longstats() function will be called.

            If my guess is right, then let me give you few suggestions here:

            1. Remove your HiddenField
            It makes the job of getting the correct Id more troublesome only. There are better methods available to encapsulate the Id value in the button itself.

            2. Replace your <a> with LinkButton
            Practice: In ASP.NET, it is advisable to use standard Web Control whenever you want to access to the control programmaticall y. CommandArgument: As mentioned, it is easier if the Id is encapsulated in the button itself. CommandArgument property serves the purpose. Somehow, <a> doesn't have this property.

            How to make this work? The Repeater should now look like this:

            Code:
            <asp:Repeater ID="Repeater1" runat="server" ...>
               <ItemTemplate>
                  ...
                  <asp:LinkButton runat="server" ID="LinkButton1"
                     Text="LinkButton Text"
                     CommandArgument='<%# Eval("Id") %>'
                     OnClick="LinkButton1_Click" />
                  ...
               </ItemTemplate>
            </asp:Repeater>
            And LinkButton1_Cli ck event handler is to be created in your code-behind file:

            Code:
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
               // And you get your [B]Id[/B] with this line
               string id = ((LinkButton)sender).CommandArgument;
               ...
            }
            Simpler?

            Comment

            • Bhavs
              New Member
              • Sep 2008
              • 16

              #7
              Thanks Liawcv.

              I am now using the for each loop to fetch the value of the hidden field.
              The reason I am using a anchor control is I have to display a hyper link statement( and on clicking on this hyper link I need to take the control to log method)

              I have now got a better understanding about the repeaters. Thanks again.

              Comment

              Working...