0

aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

}
protected void Button1_Click1(object sender, EventArgs e)
{

    GridView1.DataSource = SqlDataSource1;
    GridView1.DataBind();

}
}

Aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
        Text="Button" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName 
%>" 
        SelectCommand="SELECT * FROM [Machine]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    </asp:GridView>
    </form>
</body>
</html>

Initially DataSourceID of Grid view is not assigned to SQLDataSource. I want Table to be displayed on button click only on same page.

Reference - execute sqldatasource with button

3
  • Did you even Google GridView? msdn.microsoft.com/en-us/library/… Commented Jan 27, 2018 at 5:55
  • In case we set DataSourceID in properties to SQLDataSource, the data is getting displayed, by default. I want to show data on button click. Commented Jan 27, 2018 at 6:02
  • You do know that a GridView is a html table with columns. And of course you know that columns have to be added in order to show data, either manually or ... Commented Jan 27, 2018 at 10:15

0

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.