J
jason
Hello everyone. I am trying to write some custom command events into a
DataGrid. The command that is currently giving me trouble is an "Add"
custom command in the footer of a template column.
Question #1: In the Add button Command Event I am trying to access the
Text value of the TextBox in the other Footer columns. However, inside
the Event, the TextBox.Text values are all empty strings, even if they
had data typed into them. Is this a simple PostBack related error? I
have included a sample .aspx and .cs file that demonstrates my
difficulty. Here is the .aspx file:
<%@ Page language="c#" Codebehind="Test.aspx.cs"
AutoEventWireup="false" Inherits="polaris.Test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD><title>Test</title></HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" ShowFooter="True">
<columns>
<asp:templatecolumn headertext="Name">
<itemtemplate>
<asp:label id="Label1" runat="server" text='
<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
</itemtemplate>
<footertemplate>
<asp:textbox id="Textbox1" runat="server" />
</footertemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Action">
<itemtemplate>
<asp:linkbutton commandname="Edit" id="Button1"
runat="server" text="Edit" />
</itemtemplate>
<footertemplate>
<asp:linkbutton commandname="AddSchedule" id="Button2"
runat="server" text="Add" />
</footertemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<asp:Label id="Label2" runat="server" /><br />
<asp:Label id="Label3" runat="server" />
</form>
</body>
</HTML>
And here is the code behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace polaris
{
/// <summary>
/// Summary description for Test.
/// </summary>
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
static DataTable oTable = new DataTable();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
oTable.Columns.Add("Name");
object[] R1 = new object[] { "John" };
object[] R2 = new object[] { "Jane" };
object[] R3 = new object[] { "Jack" };
object[] R4 = new object[] { "Jill" };
oTable.Rows.Add(R1);
oTable.Rows.Add(R2);
oTable.Rows.Add(R3);
oTable.Rows.Add(R4);
}
DataGrid1.DataSource = oTable;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
// Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DataGrid1_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemCommand(Object source,
DataGridCommandEventArgs e)
{
TextBox T1 = (TextBox)e.Item.FindControl("Textbox1");
Label2.Text = "Type: " + T1.Text.GetType().ToString();
Label3.Text = "Value: " + T1.Text;
return;
}
}
}
This compiles and runs, and if you fill out a value in the footer
TextBox, then click the Add button, it should demonstrate the problem
with my first question. Any help is appreciated.
Question #2: Do I have to use a LinkButton control for this kind of
operation? When I switch the control to an asp:button type, it doesn't
seem to function at all.
Thanks in advance for any help,
Jason
DataGrid. The command that is currently giving me trouble is an "Add"
custom command in the footer of a template column.
Question #1: In the Add button Command Event I am trying to access the
Text value of the TextBox in the other Footer columns. However, inside
the Event, the TextBox.Text values are all empty strings, even if they
had data typed into them. Is this a simple PostBack related error? I
have included a sample .aspx and .cs file that demonstrates my
difficulty. Here is the .aspx file:
<%@ Page language="c#" Codebehind="Test.aspx.cs"
AutoEventWireup="false" Inherits="polaris.Test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD><title>Test</title></HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" ShowFooter="True">
<columns>
<asp:templatecolumn headertext="Name">
<itemtemplate>
<asp:label id="Label1" runat="server" text='
<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
</itemtemplate>
<footertemplate>
<asp:textbox id="Textbox1" runat="server" />
</footertemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Action">
<itemtemplate>
<asp:linkbutton commandname="Edit" id="Button1"
runat="server" text="Edit" />
</itemtemplate>
<footertemplate>
<asp:linkbutton commandname="AddSchedule" id="Button2"
runat="server" text="Add" />
</footertemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<asp:Label id="Label2" runat="server" /><br />
<asp:Label id="Label3" runat="server" />
</form>
</body>
</HTML>
And here is the code behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace polaris
{
/// <summary>
/// Summary description for Test.
/// </summary>
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
static DataTable oTable = new DataTable();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
oTable.Columns.Add("Name");
object[] R1 = new object[] { "John" };
object[] R2 = new object[] { "Jane" };
object[] R3 = new object[] { "Jack" };
object[] R4 = new object[] { "Jill" };
oTable.Rows.Add(R1);
oTable.Rows.Add(R2);
oTable.Rows.Add(R3);
oTable.Rows.Add(R4);
}
DataGrid1.DataSource = oTable;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
// Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DataGrid1_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemCommand(Object source,
DataGridCommandEventArgs e)
{
TextBox T1 = (TextBox)e.Item.FindControl("Textbox1");
Label2.Text = "Type: " + T1.Text.GetType().ToString();
Label3.Text = "Value: " + T1.Text;
return;
}
}
}
This compiles and runs, and if you fill out a value in the footer
TextBox, then click the Add button, it should demonstrate the problem
with my first question. Any help is appreciated.
Question #2: Do I have to use a LinkButton control for this kind of
operation? When I switch the control to an asp:button type, it doesn't
seem to function at all.
Thanks in advance for any help,
Jason