如何在Repeater内的DataGrid中编写ItemCommand和OnDeleteCommand

时间:2014-07-03 05:25:01

标签: c# asp.net gridview repeater

我在转发器中有3个控件,即标签,按钮和数据网格。 我已经为按钮写了点击事件。 但我不知道如何编写代码到Datagrid onitemcommand和ondeletecommand。

这是我的代码:

 <asp:Repeater runat="server" OnItemDataBound="repeaterSearchResult_ItemDataBound" ID="repeaterSearchResult">
     <ItemTemplate>
         <asp:Label ID="textBoxSearch"  ForeColor="White" width="25%" runat="server" 
              Text="<%#Container.DataItem%>"></asp:Label>
         <asp:Button ID="BTNAdd" runat="server" Text="Add"  OnClick="button_click"/>
         <br />
         <asp:DataGrid ID="dgLCL" runat="server" AutoGenerateColumns="False" 
              ShowFooter="FALSE" CellPadding="3" OnItemCommand="dgLCL_Select" 
              OnDeleteCommand="dgLCL_Delete">
              <asp:BoundColumn DataField="GrossUOMType" HeaderText="Type">
              </asp:BoundColumn>
              <asp:BoundColumn DataField="Volume" HeaderText="Volume">
              </asp:BoundColumn>
              <asp:TemplateColumn HeaderText="DELETE">
                 <ItemTemplate>
                     <asp:ImageButton runat="server" ID="IMGBTNDelete" 
                        ImageUrl="~/AppImages/grid-icon-delete.jpg"
                        ToolTip="Delete"  CommandName="DeleteItem" 
                        OnClientClick="javascript:return confirmDelete();"
                        AlternateText="Delete" />
                 </ItemTemplate>
              </asp:TemplateColumn>
              <asp:TemplateColumn HeaderText="Add">
                  <ItemTemplate>
                      <asp:ImageButton runat="server" ID="IMGBTNAdd" 
                           ImageUrl="~/AppImages/grid-icon-add.jpg"
                           ToolTip="Insert"  CommandName="InsertItem" 
                           AlternateText="Insert" />
                   </ItemTemplate>
              </asp:TemplateColumn>
          </Columns>
        <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" 
                    Mode="NumericPages">
        </PagerStyle>
                                                </asp:DataGrid><br />

这是我背后的代码:

 protected void repeaterSearchResult_ItemDataBound(object sender, RepeaterItemEventArgs e)
 {
      string Flag = Session["Flag"].ToString();
      if (Flag=="B") 
      {
            e.Item.FindControl("textBoxSearch").Visible = false;
            e.Item.FindControl("BTNAdd").Visible = false;
      }
      DataGrid gv = e.Item.FindControl("dgLCL") as DataGrid;
      //TextBox textBox = e.Item.FindControl("textBoxSearch") as TextBox;
      Label label = e.Item.FindControl("textBoxSearch") as Label;
      // BindGrid(textBox.Text,Session["Flag"].ToString());

      string SFRID = label.Text;
      if (Flag == "L")
      {
         sql = "Select MasterNo , MasterDate,GrossWt,GrossUOMType,Volume from 
                 VW_TransLCLMaster  where tBLG_NUIsActive=1 and PortOfDischargeName='" 
                 + SFRID + "'";
         //mobjGenlib.objDBLib.ExecuteNonQuery(sql);
         SqlCommand cmd = new SqlCommand(sql, con);
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         DataSet ds = new DataSet();
         da.Fill(ds);
         if (gv != null)
         {
             gv.DataSource = ds;
             gv.DataBind();
         }
       }
  }

问题是:

我在Datagrid中有选择,删除和插入按钮。我想知道如何编写onitem和ondelete命令。

任何人都可以帮我解决这个问题。 提前致谢

1 个答案:

答案 0 :(得分:0)

尝试在repeater_ItemDataBound中找到您的数据网格

DataGrid dg = (DataGrid)e.Item.FindControl("dgLCL");
如果你想onitemcommand,请点击你想要的事件。 比写下一行

 dg.ItemCommand +=

并按Tab键两次事件将在代码后面处理。形成的代码就像

    dg.ItemCommand += new DataGridItemCommandEventHandler(dg_ItemCommand)
}
void dg_ItemCommand(object sender, DataGridItemCommandEventArgs e)
{
    throw new NotImplementedException();
}
相关问题