Re: TemplateField Buttons Ignoring OnClick Event
- From: Phil H <google@xxxxxxxxxxxxxxx>
- Date: Fri, 29 Feb 2008 13:06:24 -0800 (PST)
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>
.
- References:
- TemplateField Buttons Ignoring OnClick Event
- From: Harry Keck
- TemplateField Buttons Ignoring OnClick Event
- Prev by Date: Re: WebControl adding files to project
- Next by Date: Re: Problem with webcontrol state using PlaceHolder within ASCX file
- Previous by thread: TemplateField Buttons Ignoring OnClick Event
- Next by thread: WebControl adding files to project
- Index(es):
Relevant Pages
|