TemplateField Buttons Ignoring OnClick Event

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>
 
P

Phil H

Hi

Without knowing more about what you are trying to do here, I have
generated the following as a way of demonstrating how you can achieve
the same behaviour as in your example but using the normal features of
a GridView tied to a datasource.

The buttons get their settings from an XML file attached to an
XmlDataSource. Notice the use of CommandName and CommandArgument
properties of the buttons in the ItemTemplate. When clicked they raise
the RowCommand event of the GridView.

aspx page source:
<!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>
<tr>
<td style="width: 100px">
<asp:Label ID="m_labelnumber" runat="server"
Text="1"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
<asp:GridView ID="m_gridview" runat="server"
AutoGenerateColumns="False" OnRowCommand="m_gridview_RowCommand"
DataSourceID="ButtonsXmlDataSource">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:Button ID="Button1"
runat="server" CommandArgument='<%# Eval("button_value") %>'
CommandName="AddNumber" Text='<
%# Eval("button_label") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="ButtonsXmlDataSource"
runat="server" DataFile="~/App_Data/buttondata.xml"
XPath="/buttondata/button"></
asp:XmlDataSource>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>

..cs file:

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;

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

}
protected void m_gridview_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "AddNumber")
{
int m = int.Parse(m_labelnumber.Text);
int d = int.Parse(e.CommandArgument.ToString());
int s = m + d;
m_labelnumber.Text = s.ToString();
}
}
}

XML file:

<?xml version="1.0" encoding="utf-8" ?>
<buttondata>
<button button_label="Add one" button_value="1" />
<button button_label="Add two" button_value="2" />
<button button_label="Add three" button_value="3" />
</buttondata>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top