0

A common way of displaying a list is by making a table in html. I have made a table in html and using the <% %> tags to put the values for the list. like so (example),

<table>

<tr><td>First Name</td><td>LastName</td><td>Phone</td></tr>
<% for each item in employees %>
<tr>
<td><% =item.FirstName %></td><td><% =item.LastName %></td><td><% =item.Phone %></td>
</tr>
<% next %>

</table>

Now I don't want to do this each time I post back or click buttons. Therefore I used if autopostback = false then execute the code.

What I want to do is hit a specific button to execute this piece of client-side code. I am new to using asp.net, vb.net is my primary language but I have also done some php.

6
  • 2
    Your question is not clear, are you saying you're trying to hide a specific button from submitting a form? Commented Jul 8, 2014 at 3:51
  • @Rook I think he meant hit. Commented Jul 8, 2014 at 3:51
  • Sorry I am not american I'am from the Netherlands. I meant hit. Commented Jul 8, 2014 at 3:57
  • I guess he wants a button, once clicked, fire the ajax call to load that table to the html page. Commented Jul 8, 2014 at 3:59
  • yes that what I need but do you have a sample for me. Commented Jul 8, 2014 at 4:01

2 Answers 2

1

The best thing, if you want to load the HTML code dynamically when click on a button is to use AJAX. Here are the steps you may consider.

  • Keep the dynamic content in a separate .ASPX or .HTML file.
  • In Main file, keep a button and call the script.
  • The script should call the AJAX method to the earlier created page.
  • Display the output anywhere (prefer take some DIV and fill its innerHTML)

Alternately, You can check the IsPostBack method and check whether the server side control innerHTML is blank or not and fill it up (without using AJAX).

Sign up to request clarification or add additional context in comments.

2 Comments

if I choose to go with In Main file, keep a button and call the script. how could I code this. I'am not femilliar with ajax at al.
Just create an empty HTML file/ASPX file from File->New and put your desired static/dynamic content. For AJAX, just use simple method like $.GET, after including the jquery.js after downloading from jquery.org website.
1

The way you are doing it is not client side but here is how I would do it

ASP Markup

<asp:Table runat="server" name="EmployeesTable">
 <asp:TableRow>
     <asp:TableCell>First Name</asp:TableCell>
     <asp:TableCell>Last Name</asp:TableCell>
     <asp:TableCell>Phone</asp:TableCell>
   </asp:TableRow>
<table>

Code Behind

Now you should put this in the page load event, in addition you can wrap it in a if statement to check for postBack

If Not Page.IsPostBack Then

    for each item in employees

        Dim tr = New TableRow()
        Dim tdFname = New TableCell()
        Dim tdLname = New TableCell()
        Dim tdPhone = New TableCell()

        tdFname.Text = item.FirstName
        tdLname.Text = item.LastName
        tdPhone.Text = item.Phone

        tr.Cells.Add(tdFname)
        tr.Cells.Add(tdLname)
        tr.Cells.Add(tdPhone)

        EmployeesTable.Add(tr)

    next

End If

3 Comments

why can't I put this in a button click event?
I will try tomorrow. if it works you will get credit.
@PieterdeVries sure let me know how it works out for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.