Re: TemplateField Buttons Ignoring OnClick Event

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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



Relevant Pages

  • Re: question about datasets that get empty
    ... > When I click on Button1 than I load an xml file in a dataset. ... > private void Button1_Click(object sender, ... > int i = AdressenDataSet.Tables.Rows.Count; ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: can this be done
    ... But when you have a known table and column structure (or rather, ... such as using a 'raw' XML file that I define? ... StateRuleID (int) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Rookie question : Writing edited form data (gridview) to XML
    ... > have not worked on GridView in a while. ... > Your first step "Populate a GridView from an xml file" could be a simple as ... > databinding the GridView control to an XmlDataSource control (if you are ... > "Write contents of GridView to new xml file". ...
    (microsoft.public.dotnet.xml)
  • Gridview bound to Object and Checkbox
    ... I've got a middle tier data object that I'm binding a gridview to, ... private int _Age; ... public DateTime CreateDate ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Rookie question : Writing edited form data (gridview) to XML
    ... >> have not worked on GridView in a while. ... >> databinding the GridView control to an XmlDataSource control (if you are ... >> If writing the Gridview to a new xml file, and not persisting changes to ...
    (microsoft.public.dotnet.xml)