****ASPX PAGE SAMPLE***
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!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>GridView Tricks</title>
<script type="text/javascript">
function initGrid()
{
//Once Page Loaded Setup GridView DataSource/Bind
//The timeout is completely unnecessary, but helpful in showing that it works since otherwise
//in my environment it will run too quickly and I don't want to setup an animation :)
setTimeout(function(){__doPostBack($get('up').id,'true');},2000);
//__doPostBack($get('up').id,'true');
}
</script>
</head>
<body onload="initGrid();">
<form id="form1" runat="server">
<h5>Just Something To See While I Wait For The Grid</h5>
<asp:ScriptManager ID="sm" runat="server" Enablepartialrendering="true" />
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<%--NOTE: There is no datasourceid set on the gridview initially, we set that in code-behind--%>
<asp:GridView ID="GridView1" runat="server" />
<asp:SqlDataSource ID="sqlDS" runat="server"
ConnectionString="Data Source=WT101\SQLEXPRESS;Integrated Security=True"
SelectCommand="SELECT * FROM DevTable" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
****CODE BEHIND****
protected void Page_Load(object sender, EventArgs e)
{
//I got a little carried away on the "null" checking
//more out of habit than necessary for this scenario
if(!string.IsNullOrEmpty(Request.Params.Get("__EVENTARGUMENT")))
{
//If I sent the postback from JavaScript initGrid() function then
//Assign the gridview its datasource and all the rest takes care of itself
if(Request.Params.Get("__EVENTARGUMENT") == "true")
{
GridView1.DataSourceID="sqlDS";
}
}
}
|