By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

DataGrid ItemCommand not fired

P: 2
I am trying to accept two input strings and concatinate and display the result in a datagrid. Following is the HTML and code. ItemCommand does not get fired what is that I am missing?

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

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();
}



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;

}
}
}

}
}
Sep 13 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.