0

I used a asp:button to bind the data from a textbox into a grid, but my problem was getting the page to stop refreshing. So I changed the asp:button to html button, because it was server side causing the page to refresh. Now I dont know how to use the same c# code to bind the data using html button.

Here's an example of my c# code:

    //Site Class
    class Sites
    {
    //Strings and variables are created based off text box Id to make matter easier to read
        public string SiteName { get; set; }
        public string Slick { get; set; }
        public string UserID { get; set; }
    }

    //Created a new list based on class.
    //Items are added based on whats in the textboxes 
    //and events are fired to gridview by the click of the class button
    protected void SiteDetails_Click(object sender, EventArgs e)
    {
        List<Sites> list;

        if (Session["list"] == null)
        {
            list = new List<Sites>();
        }
        else
        {
            list = (List<Sites>)Session["list"];
        }
        list.Add(new Sites() { SiteName = Sname.Text, Slick = Slick.Text, UserID = User_ID.Text });
        Session["list"] = list;
        SiteGrid.DataSource = list;
        SiteGrid.DataBind();
    }

And This is my Html aspx page:

Site Details


SiteName:
                        <td style="width: 102px; height: 8px;">Slic:</td>
                        <td style="width: 153px; height: 8px;">
                            <asp:TextBox ID="Slick" runat="server"></asp:TextBox>
                        </td>
                    </tr>

                    <tr>
                        <td style="width: 129px; margin-left: 40px; height: 23px;">User ID:</td>
                        <td style="width: 152px; height: 23px;">
                            <asp:TextBox ID="User_ID" runat="server" style="font-weight: bold"></asp:TextBox>
                            </td>

                        <td style="width: 102px; height: 23px;">
                            Password:</td>
                        <td style="width: 153px; height: 23px;">
                            <asp:TextBox ID="Pass" runat="server" TextMode="Password" ></asp:TextBox>
                        </td>

                    </tr>
                   <%-- //Site Button--%>
                    <tr>
                                                                  <td></td>
                        <td>
                  <button ID="SiteDetails" OnClick="SiteDetails_Click()">AddSites</button>
                        </td>
                    </tr>
                    </table>

                  <%-- //Site GridView--%>
       <asp:GridView ID="SiteGrid" class="table-bordered table-hover tableGridLines="None">
                      </asp:GridView>
  • Wouldn't it be a better solution to check what is causing your page to refresh every time? Now you are trying to find a fix for your initial fix, rather than fixing the initial problem. – Matthijs May 30 '14 at 15:52
  • You're looking for a way to call the server without refreshing the entire page? – Mike Christensen May 30 '14 at 15:52
  • are you sure you are using it doesn't appear that you are also you will need to have the `runat=server` as well if you are if you are also wanting to invoke the method look up how to decorate the method with the [WebMethod] attribute as well also in the using header you would want to add this `using System.Web.Services;` – MethodMan May 30 '14 at 15:54
  • You want to call server side code without calling the server????? – Gusman May 30 '14 at 15:54
  • possible duplicate of [Invoke C# code from JavaScript in a Document in a WebBrowser](http://stackoverflow.com/questions/3694028/invoke-c-sharp-code-from-javascript-in-a-document-in-a-webbrowser) – clarkitect May 30 '14 at 15:54
  • The refresh you are experiencing is part of ASP.NET, it is called a Postback. You are posting back the values of your page to the server. If you want to avoid a full page refresh, you may want to look at ASP.NET's UpdatePanel's. They allow a partial refresh of whatever is inside them, without refreshing the whole page. – BobbyDazzler May 30 '14 at 15:59
  • I know its the asp:button doing a postback. Im just trying to find a way to get the data with out the post back. now UpdatePanel is a option is there any more options? @MightyLampshade – user3691924 May 30 '14 at 17:59
  • @jeffdot Not a duplicate – user3691924 May 30 '14 at 18:00
  • I was think of making a html button client side with the onclick: and possibely making a javascript funtion to fire the c# code? Any other suggestions? – user3691924 May 30 '14 at 18:02
  • @Gusman I want to call the c# code with out having to post back. The problem was the asp button it does a automatic postback because its a submit type and runat="server". Now im using a html button to try to call the c# code but i want to know if thats possible if so how? – user3691924 May 30 '14 at 18:09
  • If you don't inform to the server that you want to execute something it will do nothing. To inform the server you want to do something you must do a post back, or at least a call back, so you will be asking to the server always. If what you want is to do the call but not refreshing the page's content, then you should use ajax and a [ScriptableMethod]. If that is what you want let me know and I will post an example. – Gusman May 30 '14 at 18:12
  • @Gusman yes I would like a example thank you. – user3691924 May 30 '14 at 18:15
  • Here it is: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ – Gusman May 30 '14 at 18:23

0 Answers0