views:

66

answers:

2

This is what I have implemented, for further code, how to send the text of the text box to the server to store in variable or database without post back?

It can be done by using Ajax and update plane, but I would like to implement it using a JavaScript script.

<div id="CommentID" style=" width:30%; height:30%">
   <asp:Button ID="Button1" runat="server"
        Text="Comment"
        OnClientClick="visibleDiv('id1'); return false;" />

   <div id="id1" runat="server" style="visibility: hidden; background-color:Green; width:100%; height:100%">
       <asp:TextBox ID="TextBox1" runat="server"
            AutoCompleteType="Disabled" Rows="3"
            TextMode="MultiLine" Width="98%">
       </asp:TextBox>
       <asp:Button ID="Button2" runat="server"
            Text="Post"
            onclick="Button2_Click" />
       <asp:Button ID="Button3" runat="server"
            Text="Cancel"
            OnClientClick="visibleDiv('id1'); return false;" />
    </div>
</div>
A: 

I suggest you to use jQuery:

<asp:Button ID="Button1" runat="server"
        Text="Comment" 
        OnClientClick="$.post("you_url", {text: $("#TextBox1").val()});"/>

See http://api.jquery.com/jQuery.post/ for details.

Roman Bataev
thank you am checking it out
do i have to download the juery?
Yes. Or you can use their CDN: <script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
Roman Bataev
A: 

Skip jQuery, just add use a PageMethod

This will expose a static server side method in javascript and facilitate all the Ajax stuff

Add this to your code behind

[System.Web.Services.WebMethod]
public static string SendString( string text ) {
    // store the string in the database
    return text;
}

Add a scriptmanager to your markup

<form runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"/>
    ...

And now you can call SendString from javascript using

 PageMethods.SendString("mystring", function(text){
     alert("wooot! " + text);
 });

Also note that you can use strongly typed parameters and return values.
ASP.NET AJAX will automatically create client side classes in javascript to emulate the server side ones, and will automatically serialize and deserialize them :)

Sean Kinsey
thnks did wht ur code saysbut errors please check it, as i do not know what it means?
<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled" Rows="3" TextMode="MultiLine" Width="98%"></asp:TextBox> <asp:Button ID="Button3" runat="server" Text="Cancel" OnClientClick="visibleDiv('id1'); return false;" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" > <ContentTemplate> <asp:Button ID="Button2" runat="server" Text="Post" onclick="Button2_Click" OnClientClick="PageMethods.SendString("mystring", function(){alert("wooot!");});/> </ContentTemplate> </asp:UpdatePanel>
public partial class _Default : System.Web.UI.Page { [System.Web.Services.WebMethod] public static string SendString(string text) { // store the string in the database } protected void Page_Load(object sender, EventArgs e) { }
Error 1 '_Default.SendString(string)': not all code paths return a valuethis is for .cs behind coding Warning 4 The attribute name and value must be separated by an equals (=) sign. this is for the asp coding
correct syntaxstring comments; [System.Web.Services.WebMethod] public static void SendString(string text) { // store the string in the database _Default asd = new _Default(); asd.comments = text; asd.Label2.Text = text; }and <asp:Button ID="Button2" runat="server" Text="Post" OnClientClick="PageMethods.SendString('testttttt')"/>but still failed,there is post back taking place.than you.
Have you even tried googling for 'pagemethods + asp.net'?
Sean Kinsey
See my updated answer for the warning. Its a method so it expects a return value. This should get you going.
Sean Kinsey