Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I make a gridview execute with a button press?

ASP.NET
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:GridView ID="GridView1"
            DataSourceID="SqlDataSource1"
            ...
            runat="server">
<asp:SqlDataSource 
            ID="SqlDataSource1" 
            ConnectionString="<%$ ConnectionStrings:reg %>"
            SelectCommand="SELECT * FROM [table];"
            ...
            runat="server">
</asp:SqlDataSource>
Posted
Updated 13-Dec-14 5:38am
v3

remove the data source from aspx and you can create data source from code behind and bind to gridview on button click event like below
click event code:
VB
Dim SqlDataSource1 As New SqlDataSource()
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("reg").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * FROM [table]"
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
 
Share this answer
 
v2
Just add:

GridView1.DataBind()

to the Button_Click function and it will update it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900