Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
810
How to use WebDataGrid.ItemCommand?
posted

Hi,

Can someone give me sample codes, contains instructions on how to use the ObjectDataSource + WebDataGrid, how to use the button in TemplateDataFiled updates and the refresh webdatagrid and Page_Load event began Codes?

I have some problems:
1) When I click btnStop/btnDelete, but ItemCommand have not fired.
2) When the button btnStop/btnDelete be fired, how to refresh WebDataGrid1?
3) When I select next page or click btnStop/btnDelete, WebDataGrid1 can not display any information
.

The following is my code:

In .aspx
<asp:Content ID="MainContent" runat="server" contentplaceholderid="MainContent">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Input Group Name:<asp:TextBox ID="txtSysGroupName" runat="server"></asp:TextBox>
<asp:Button ID="btnSelect" runat="server" onclick="btnSelect_Click" Text="Search" />
<ig:WebDataGrid ID="WebDataGrid1" runat="server" AutoGenerateColumns="False" DataKeyFields="sysGroupID" oninitializerow="WebDataGrid1_InitializeRow" onitemcommand="WebDataGrid1_ItemCommand" >
  <Columns>
    <ig:TemplateDataField Key="tempAction" Width="100px">
      <ItemTemplate>
        <asp:Button ID="btnStop" runat="server" CommandName="myStop" Text="Stop" />
        <asp:Button ID="btnDelete" runat="server" CommandName="myDelete" Text="Delete" />
      </ItemTemplate>
    <ig:TemplateDataField>
    <ig:BoundDataField DataFieldName="sysGroupName" Key="SysGroupName">
      <Header Text="Group Name" />
    </ig:BoundDataField>
    <ig:BoundDataField DataFieldName="sysGroupName" Key="SysGroupID">
      <Header Text="System Group ID" />
    </ig:BoundDataField>
    <ig:BoundDataField DataFieldName="sysGroupStatus" Key="sysGroupStatus" Hidden="True">
       <Header Text="System Group Status" />
     </ig:BoundDataField>
    <ig:BoundDataField Key="tempSysStatusDisplay">
        <ItemTemplate>
          <asp:Label ID="lblSysGroupStatusDisplay" runat="server"></asp:Label>
        </ItemTemplate>
        <Header Text="Status Display" />
      </ig:BoundDataField>
  </Columns>
  <Behaviors>
    <ig:EditingCore>
      <Behaviors>
      <ig:CellEditing>
        <ColumnSettings>
          <ig:EditingColumnSetting ColumnKey="sysGroupID" />
        </ColumnSettings>
      </ig:CellEditing>
      </Behaviors>
    </ig:EditingCore>
    <ig:Paging PageSize="10">
    </ig:Paging>
 </Behaviors>
</ig:WebDataGrid>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="WebApplication_DMS.dsDMSSTableAdapters.sysGroupTableAdapter" UpdateMethod="Update" onupdating="odsSysGroup_Updating">
  <DeleteParameters>
    <asp:Parameter Name="Original_sysGroupID" Type="String" />
  </DeleteParameters>
  <InsertParameters>
    <asp:Parameter Name="sysGroupID" Type="String" />
    <asp:Parameter Name="sysGroupName" Type="String" />
    <asp:Parameter Name="sysGroupStatus" Type="Int32" />
    <asp:Parameter Name="sysComID" Type="String" />
    <asp:Parameter Name="memo" Type="String" />
    <asp:Parameter Name="actUser" Type="String" />
    <asp:Parameter Name="actTime" Type="DateTime" />
  </InsertParameters>
  <UpdateParameters>
    <asp:Parameter Name="sysGroupID" Type="String" />
    <asp:Parameter Name="sysGroupName" Type="String" />
    <asp:Parameter Name="sysGroupStatus" Type="Int32" />
    <asp:Parameter Name="sysComID" Type="String" />
    <asp:Parameter Name="memo" Type="String" />
    <asp:Parameter Name="actUser" Type="String" />
    <asp:Parameter Name="Original_sysGroupID" Type="String" />
  </UpdateParameters>
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

In .cs
Dictionary<string, string> dicStatus;
protected void Page_Load(object sender, EventArgs e)        
{
  if (!IsPostBack)
  {
      GetData();
  }
}

private void GetData()
{
  MParameter mParameter = new MParameter();
  dicStatus = mParameter.GetAuthority("normStatus");
  ObjectDataSource1.SelectParameters.Clear();
  if (string.IsNullOrEmpty(txtSysGroupName.Text))
  {
    ObjectDataSource1.SelectMethod = "GetDataBySysUserID";
    ObjectDataSource1.SelectParameters.Add("SysUserID", User.Identity.Name);
  }
  else
  {
    ObjectDataSource1.SelectMethod = "GetDataBySysGroupName";
    ObjectDataSource1.SelectParameters.Add("SysGroupName", txtSysGroupName.Text);
    ObjectDataSource1.SelectParameters.Add("SysUserID", User.Identity.Name);
  }
  WebDataGrid1.DataSource = ObjectDataSource1;
  WebDataGrid1.DataBind();
}

protected void WebDataGrid1_InitializeRow(object sender, Infragistics.Web.UI.GridControls.RowEventArgs e)
{
  DataRowView drv = (DataRowView)e.Row.DataItem;
  Label lblSysGroupStatusDisplay= (Label)e.Row.Items.FindItemByKey("tempSysStatusDisplay").FindControl("lblSysGroupStatusDisplay");
  lblSysGroupStatusDisplay.Text = dicStatus[drv["SysGroupStatus"].ToString()];
  Button btnStop = (Button)e.Row.Items.FindItemByKey("tempAction").FindControl("btnStop");
  Button btnDelete = (Button)e.Row.Items.FindItemByKey("tempAction").FindControl("btnDelete");
  btnStop.CommandArgument = drv["SysGroupID"].ToString();
  btnDelete.CommandArgument = drv["SysGroupID"].ToString();
}

protected void WebDataGrid1_ItemCommand(object sender, Infragistics.Web.UI.GridControls.HandleCommandEventArgs e)
{
  string arg = e.CommandArgument.ToString();
  switch (e.CommandName)
  {
    case "myStop":
      mSysGroup.UpdateSysGroupBySysID(new ESysGroup
      {
        SysGroupID = arg,
        SysGroupStatus = 60,
        ActUser = User.Identity.Name
      });
      break;
    case "myStop":
      mSysGroup.UpdateSysGroupBySysID(new ESysGroup
      {
        SysGroupID = arg,
        SysGroupStatus = 80,
        ActUser = User.Identity.Name
      });
      break;
  }
}