H
Harry Keck
I had to turn off EnableViewState on my GridView, due to odd ViewState issues
involved with adding dynamic columns. Because there is no ViewState, I have
to rebind the grid on every postback. As long as I do the rebind during the
page load, everything seems to work, but I would rather not do it at this
point. Either during LoadComplete or PreRender makes more sense to me. This
way I have a chance to alter things based on user input before the bind.
However, if I do the bind during either of these phases, any buttons in my
template columns do not operate correctly. I have an OnClick event declared
for the button, but clicking it does not run the event code, though it does
cause a postback.
I am pasting the complete code for a web page below to demostrate this
behavior. If you copy the aspx and cs as is, clicking the buttons in the
grid will raise the event correctly and increment the number displayed in the
label. If you move the BindGridToData() out of the Page_Load handler and
into Page_LoadComplete, the page will appear to render correctly, but if you
click the grid button, nothing happens, because the button click event will
not get called.
According to all documentation I have read, I should be able to bind the
grid at any point up until and including PreRender, but that is clearly not
working. If you can explain this, or help me find a work around, I would
appreciate it.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace TestWebProject
{
public partial class MissingOnClick : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// This should happen everytime
BindGridToData();
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
}
private void BindGridToData()
{
DataSet datasetGrid = new DataSet();
datasetGrid.Tables.Add("GridDataTable");
datasetGrid.Tables[0].Columns.Add("1");
datasetGrid.Tables[0].Rows.Add("value 1");
datasetGrid.Tables[0].Rows.Add("value 2");
m_gridviewTest.DataSource = datasetGrid.Tables[0];
m_gridviewTest.DataBind();
}
protected void buttonTest_OnClick(object sender, EventArgs e)
{
m_labelNumber.Text =
Convert.ToString(Convert.ToInt32(m_labelNumber.Text) + 1);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="MissingOnClick.aspx.cs" Inherits="TestWebProject.MissingOnClick"
%>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
<asp:Label runat="server" Text="1"
ID="m_labelNumber"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:GridView ID="m_gridviewTest" runat="server"
AutoGenerateColumns="False" EnableViewState="False">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:Button ID="buttonSelect"
OnClick="buttonTest_OnClick" Text="Add One" runat="server"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
involved with adding dynamic columns. Because there is no ViewState, I have
to rebind the grid on every postback. As long as I do the rebind during the
page load, everything seems to work, but I would rather not do it at this
point. Either during LoadComplete or PreRender makes more sense to me. This
way I have a chance to alter things based on user input before the bind.
However, if I do the bind during either of these phases, any buttons in my
template columns do not operate correctly. I have an OnClick event declared
for the button, but clicking it does not run the event code, though it does
cause a postback.
I am pasting the complete code for a web page below to demostrate this
behavior. If you copy the aspx and cs as is, clicking the buttons in the
grid will raise the event correctly and increment the number displayed in the
label. If you move the BindGridToData() out of the Page_Load handler and
into Page_LoadComplete, the page will appear to render correctly, but if you
click the grid button, nothing happens, because the button click event will
not get called.
According to all documentation I have read, I should be able to bind the
grid at any point up until and including PreRender, but that is clearly not
working. If you can explain this, or help me find a work around, I would
appreciate it.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace TestWebProject
{
public partial class MissingOnClick : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// This should happen everytime
BindGridToData();
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
}
private void BindGridToData()
{
DataSet datasetGrid = new DataSet();
datasetGrid.Tables.Add("GridDataTable");
datasetGrid.Tables[0].Columns.Add("1");
datasetGrid.Tables[0].Rows.Add("value 1");
datasetGrid.Tables[0].Rows.Add("value 2");
m_gridviewTest.DataSource = datasetGrid.Tables[0];
m_gridviewTest.DataBind();
}
protected void buttonTest_OnClick(object sender, EventArgs e)
{
m_labelNumber.Text =
Convert.ToString(Convert.ToInt32(m_labelNumber.Text) + 1);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="MissingOnClick.aspx.cs" Inherits="TestWebProject.MissingOnClick"
%>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
<asp:Label runat="server" Text="1"
ID="m_labelNumber"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:GridView ID="m_gridviewTest" runat="server"
AutoGenerateColumns="False" EnableViewState="False">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:Button ID="buttonSelect"
OnClick="buttonTest_OnClick" Text="Add One" runat="server"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>