how to call c# function from javascript

Last post 05-18-2007 7:41 AM by NC01. 5 replies.

Sort Posts:

  • how to call c# function from javascript

    05-16-2007, 10:41 AM
    • Member
      20 point Member
    • kulanthaivelu
    • Member since 08-18-2006, 5:05 AM
    • Coimbatore
    • Posts 80

    Dear Friends,
         I wrote a C# function. I want to call the function from keypress event of asp:textbox. Can anybody tell me the  way.

    Regards,
    Kulanthaivelu.V

    Kulanthaivelu .V
  • Re: how to call c# function from javascript

    05-16-2007, 11:05 AM
    Answer
    • All-Star
      77,451 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 14,444
    • TrustedFriends-MVPs

    I don't know why you're doing this as every time that you have a key stroke in the TextBox you're going to have a PostBack, but here goes:

    private void Page_Load(object sender, System.EventArgs e)
    {
     TextBox1.Attributes.Add("onkeypress", this.GetPostBackEventReference(TextBox1, "CallSpecialCSharpMethod"));

     if ( this.IsPostBack )
     {
      string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ?
       string.Empty : this.Request["__EVENTARGUMENT"];

      if ( eventArgument.Trim() == "CallSpecialCSharpMethod" )
      {
       // Call your method here...
      }
     }
    }

    NC...

  • Re: how to call c# function from javascript

    05-16-2007, 11:47 PM
    • Member
      20 point Member
    • kulanthaivelu
    • Member since 08-18-2006, 5:05 AM
    • Coimbatore
    • Posts 80

    Hi NC01,

               I will explain the situation. I have records in gridview control. I need to filter the records based on textbox input. If you have any othe idea please tell me.

    Thanking you,

    Kulanthaivelu.V

    Kulanthaivelu .V
    Filed under:
  • Re: how to call c# function from javascript

    05-17-2007, 7:27 AM
    • All-Star
      77,451 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 14,444
    • TrustedFriends-MVPs

    Sorry my friend, but there is no way to design/re-design an application through this forum. Forums like this are mainly good for solving simple coding problems, not re-designing how an application works.

    Firing a PostBack on each keystroke is definitely not a good solution though. You could use the onchange event of the TextBox, which would fire when the text changes in it and the user leaves the control.

    I guess that I would have to know more of what does >>filter the records based on textbox input<< mean?

    NC...

  • Re: how to call c# function from javascript

    05-18-2007, 12:13 AM
    Answer
    • Member
      20 point Member
    • kulanthaivelu
    • Member since 08-18-2006, 5:05 AM
    • Coimbatore
    • Posts 80

    Hi friend thanks for your reply.

    That means, I have some thousands of records in my grid view If I press the "A" in my textbox the gridview control filters the records starts with "A" and if I type "AB" in my textbox the gridview control should filters the records starts with "AB". I hope now you understand the situation.

    Thanking you,

    Kulanthaivelu .V
    Filed under:
  • Re: how to call c# function from javascript

    05-18-2007, 7:41 AM
    • All-Star
      77,451 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 14,444
    • TrustedFriends-MVPs

    That means that when you type "AB" in your TextBox, you're going to get a PostBack for "A" and one for "B". Not a very good solution, plus the fact that you're not going to get the results that you want, because you're then going to filter on "A" and then "B", never "AB".

    Why not just place a button by the TextBox and when pressed, it reads the TextBox value and filters on that?

    In the aspx file:
     <asp:label id="filterLabel" runat="server">Filter by:</asp:label>
     <asp:TextBox id="filterTextBox" runat="server" Width="100px"></asp:TextBox>
     <asp:button id="filterButton" runat="server" text="Go" height="24px" width="25px" OnClick="filterButton_Click"></asp:button>

    in the aspx.cs file
    protected void filterButton_Click(object sender, System.EventArgs e)
    {
     string filterText = filterTextBox.Text.Trim();

     if ( filterTextBox.Text.Trim().Length > 0 )
     {
      // Filter your Grid on the value filterTextBox.Text.Trim()
     }
    }

    NC...

Page 1 of 1 (6 items)