pawar_deepak
asked on
How do I display gridview on Button click without posting page to server in ASP.NET, C#?
Hi,
I want to display gridview after user clicks button without page being posted back to server.
On my asp.net form, I have textbox with Required Field Validatior, if I click the button to dispaly gridview, the required field validator is asking me to enter data. But the data to be entered in Textbox depends on the data in Gridview. So I want display gridview only after Button Click not on page_load.
I tried to use Ajax Update Panel but it doesn't work.
I am attaching my files, I will really appreciate if you can make this work or suggest me an easy way to accomplish my task.
I also tried --- Gridview.visible = false; on Page_Load and Gridview.visible = true; on Button click but it doesn't work because of validation on TextBox.
I am using Visual Studio 2008, SQL Server 2008, C#.
Thank you.
I want to display gridview after user clicks button without page being posted back to server.
On my asp.net form, I have textbox with Required Field Validatior, if I click the button to dispaly gridview, the required field validator is asking me to enter data. But the data to be entered in Textbox depends on the data in Gridview. So I want display gridview only after Button Click not on page_load.
I tried to use Ajax Update Panel but it doesn't work.
I am attaching my files, I will really appreciate if you can make this work or suggest me an easy way to accomplish my task.
I also tried --- Gridview.visible = false; on Page_Load and Gridview.visible = true; on Button click but it doesn't work because of validation on TextBox.
I am using Visual Studio 2008, SQL Server 2008, C#.
Thank you.
.aspx
-------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
<br />
<asp:Label ID="Label1" runat="Server"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False"
UpdateMode="Conditional">
<ContentTemplate>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Click" OnClick="Button2_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<br />
<br />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator">Required!</asp:RequiredFieldValidator>
</div>
</form>
</body>
</html>
===========================================================================
.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=DeepakPawar-PC; database=Test; Integrated Security=SSPI; User Id = sa; Password=sqladmin ");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
string str = "select * from Books";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
===========================================================================
CREATE TABLE [dbo].[Books](
[ID] [int] NULL,
[Title] [varchar](50) NULL,
[AuthorID] [int] NULL
)
INSERT INTO [Books] VALUES (1, 'AAA', 22)
INSERT INTO [Books] VALUES (2, 'BBB', 22)
INSERT INTO [Books] VALUES (3, 'CCC', 22)
1.jpg
One option would be to do manual required field checking on this field instead of using the RequiredFieldValidation control. Or could you disable the validation control until you are ready for it to run (until the gridview has been displayed)?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you everyone for your suggestions.... I found the easiest solution to my problem.... I just changed the Button as
<asp:Button ID="Button2" runat="server" Text="Click" CausesValidation="false" OnClick="Button2_Click"/>
CausesValidation="false" solved my problem. I can display Gridview without entering the data in TextBox.
I was just beating around the bushes... I don't even need Update Panel for this..!
Thanks... Have a great day !!
<asp:Button ID="Button2" runat="server" Text="Click" CausesValidation="false" OnClick="Button2_Click"/>
CausesValidation="false" solved my problem. I can display Gridview without entering the data in TextBox.
I was just beating around the bushes... I don't even need Update Panel for this..!
Thanks... Have a great day !!
cong8 that you found solution :)
happy coding...
happy coding...
pawar_deepak,
Since you have found the solution yourself, please accept your solution as the accepted solution for this question, so that other users also will get it
Raj
Since you have found the solution yourself, please accept your solution as the accepted solution for this question, so that other users also will get it
Raj
pawar_deepak,
Why did you accept my last comment as the solution of your question, which contains no solution !?
Or do you mean my comment http#a31831118 is the solution ?
Or do you accidently selected my comment instead of your comment http#a31870447 as the solution ?
Raj
Why did you accept my last comment as the solution of your question, which contains no solution !?
Or do you mean my comment http#a31831118 is the solution ?
Or do you accidently selected my comment instead of your comment http#a31870447 as the solution ?
Raj
ASKER
Hello Raj,
I accidentaly selected ID: 32242882 as a solution.
ID: 31870447 is the solution.... ID: 31832423 and ID: 31831118 are two other solutions...
How can I edit my accepted solution?
Thanks.
I accidentaly selected ID: 32242882 as a solution.
ID: 31870447 is the solution.... ID: 31832423 and ID: 31831118 are two other solutions...
How can I edit my accepted solution?
Thanks.
Please click 'Request Attention' button to the bottom of your question and request moderator to reopen this question, so that you can re-assign points.
Raj
Raj