DataGrid ItemCommand not fired
I have created a DataGrid with 4 columns. In the ItemTemplate tag I have two textboxes to accept inputs. 1 command button and one Result column. While loading the page I use a DataTable and Dataview is datasource for the DataGrid. Then I bind the grid. What I want to know is do I have to bind a grid in ItemCommand as well. My main problem is the control does not go in the ItemCommand event.
HTML and Code is as follows:
<body bgColor="#99ccff" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 288px; POSITION: absolute; TOP: 80px"
runat="server" BackColor="Silver" ForeColor="Blue" Font-Size="Small" Font-Names="Courier New"
HorizontalAlign="Center" AutoGenerateColumns="False" BorderColor="Blue" Visible="True" EnableViewState =True >
<EditItemStyle Font-Size="Small" Font-Names="Times New Roman" ForeColor="Red" BackColor="Silver"></EditItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="String1">
<ItemTemplate>
<asp:TextBox id="TextBox1" runat="server" AutoPostBack="True" Visible="True" Text= '<%# DataBinder.Eval(Container.DataItem,"val1")%>' EnableViewState = True >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="String2">
<ItemTemplate>
<asp:TextBox id=TextBox2 runat="server" AutoPostBack="True" Text= '<%# DataBinder.Eval(Container.DataItem,"val2")%>' Visible = True EnableViewState = True >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Concat Buttons">
<ItemTemplate>
<asp:LinkButton ID="Button1" Runat="server" Text="Concatinate" CommandName="Concat" EnableViewState =True > </asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Result">
<ItemTemplate>
<asp:Label id="Label1" Visible="True" ForeColor="#C000C0" BackColor="#FFFFC0" Runat="server" EnableViewState =True></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></form>
</body>
Code is As follows:
namespace DataGridApp1
{
/// <summary>
/// Summary description for ReCalculateColumn.
/// </summary>
public class ReCalculateColumn : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
DataTable StringConcat;
DataView dv1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
BindGrid();
}
StringConcat = new DataTable();
StringConcat.Columns.Add( new DataColumn("val1", typeof(string)));
StringConcat.Columns.Add( new DataColumn("val2", typeof(string)));
}
public void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn("val1", typeof(string)));
dt.Columns.Add( new DataColumn("val2", typeof(string)));
DataRow dr1 = dt.NewRow();
dr1[0] = "1";
dr1[1] = "2";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2[0] = "3";
dr2[1] = "4";
dt.Rows.Add(dr2);
DataView dv = new DataView(dt);
DataGrid1.DataSource = dv;
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.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
Response.Write(String.Concat("<b>ItemDataBound</b>: The = ", e.Item.ItemType.ToString(), "<br />"));
if(e.Item.ItemType == ListItemType.Item)
{
//e.Item represents a DataRow
//Cells Gets a collection of TableCell objects that represent the cells of a row in a Table control.
//The Controls property allows you programmatic access to the instance of the ControlCollection class for any server control. You can add controls to the collection, remove controls from the collection, or iterate through the server controls in the collection.
string str1 = e.Item.Cells[0].Text;
}
}
private void DataGrid1_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(String.Concat("<b>ItemCommand</b>: The CommandName = ", e.CommandName, "<br />"));
if(e.CommandName == "Concat")
{
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
Label l=(Label)e.Item.Cells[3].Controls[1];
string str1 = ((TextBox)e.Item.Cells[0].Controls[1]).Text;
string str2 = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string str3 = str1 + str2;
l.Text = str3;
DataRow dr = StringConcat.NewRow();
dr[0] = str1;
dr[1] = str2;
dr[2] = str3;
StringConcat.Rows.Add(dr);
dv1 = new DataView(StringConcat);
DataGrid1.DataSource = dv1;
DataGrid1.DataBind();
}
}
}
}
}
|