DataGrid ItemCommand not fired

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jigsawcube1
    New Member
    • Sep 2007
    • 2

    DataGrid ItemCommand not fired

    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="#99ccf f" 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="Silv er" ForeColor="Blue " Font-Size="Small" Font-Names="Courier New"
    HorizontalAlign ="Center" AutoGenerateCol umns="False" BorderColor="Bl ue" Visible="True" EnableViewState =True >
    <EditItemStyl e Font-Size="Small" Font-Names="Times New Roman" ForeColor="Red" BackColor="Silv er"></EditItemStyle>
    <Columns>
    <asp:TemplateCo lumn HeaderText="Str ing1">
    <ItemTemplate >
    <asp:TextBox id="TextBox1" runat="server" AutoPostBack="T rue" Visible="True" Text= '<%# DataBinder.Eval (Container.Data Item,"val1")%>' EnableViewState = True >
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateCol umn>
    <asp:TemplateCo lumn HeaderText="Str ing2">
    <ItemTemplate >
    <asp:TextBox id=TextBox2 runat="server" AutoPostBack="T rue" Text= '<%# DataBinder.Eval (Container.Data Item,"val2")%>' Visible = True EnableViewState = True >
    </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateCol umn>
    <asp:TemplateCo lumn HeaderText="Con cat Buttons">
    <ItemTemplate >

    <asp:LinkButt on ID="Button1" Runat="server" Text="Concatina te" CommandName="Co ncat" EnableViewState =True > </asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateCol umn>
    <asp:TemplateCo lumn HeaderText="Res ult">
    <ItemTemplate >
    <asp:Label id="Label1" Visible="True" ForeColor="#C00 0C0" BackColor="#FFF FC0" Runat="server" EnableViewState =True></asp:Label>
    </ItemTemplate>
    </asp:TemplateCol umn>
    </Columns>
    </asp:datagrid></form>
    </body>

    namespace DataGridApp1
    {
    /// <summary>
    /// Summary description for ReCalculateColu mn.
    /// </summary>
    public class ReCalculateColu mn : System.Web.UI.P age
    {
    protected System.Web.UI.W ebControls.Data Grid DataGrid1;
    DataTable StringConcat;
    DataView dv1;
    private void Page_Load(objec t sender, System.EventArg s e)
    {
    // Put user code to initialize the page here
    if (!Page.IsPostBa ck)
    {

    BindGrid();


    }

    StringConcat = new DataTable();

    StringConcat.Co lumns.Add( new DataColumn("val 1", typeof(string)) );
    StringConcat.Co lumns.Add( new DataColumn("val 2", typeof(string)) );





    }

    public void BindGrid()
    {
    DataTable dt = new DataTable();
    dt.Columns.Add( new DataColumn("val 1", typeof(string)) );
    dt.Columns.Add( new DataColumn("val 2", 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.DataS ource = dv;
    DataGrid1.DataB ind();
    }



    private void DataGrid1_Selec tedIndexChanged (object sender, System.EventArg s e)
    {

    }

    private void DataGrid1_ItemD ataBound(object sender, System.Web.UI.W ebControls.Data GridItemEventAr gs e)
    {
    Response.Write( String.Concat(" <b>ItemDataBoun d</b>: The = ", e.Item.ItemType .ToString(), "<br />"));

    if(e.Item.ItemT ype == ListItemType.It em)
    {
    //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 ControlCollecti on 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_ItemC ommand(object sender, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    Response.Write( String.Concat(" <b>ItemComman d</b>: The CommandName = ", e.CommandName, "<br />"));

    if(e.CommandNam e == "Concat")
    {
    if(e.Item.ItemT ype==ListItemTy pe.Item || e.Item.ItemType ==ListItemType. AlternatingItem )
    {
    Label l=(Label)e.Item .Cells[3].Controls[1];
    string str1 = ((TextBox)e.Ite m.Cells[0].Controls[1]).Text;
    string str2 = ((TextBox)e.Ite m.Cells[1].Controls[1]).Text;
    string str3 = str1 + str2;
    l.Text = str3;

    }
    }
    }

    }
    }
Working...