ItemDataBound and Item Command in ASP.Net
DataGrid, Datalist, Reapter ItemDataBound is an event that fires once on your server for every record bound to the control.
DataGrid, Datalist, Reapter ItemCommand is the event that will fire if you click a command button that is associated with the record.
In this example, we show you how to use the ItemCommand , ItemDatabound Event, Syntax is added below to find the control inside the datagrid or datalist
<asp:DataGrid ID="dgrdFile" runat="server" ShowHeader="False" AutoGenerateColumns ="False" Width="100%" DataKeyField="PRODUCT_ID" CellPadding="4" OnItemCommand="dgrdFile_ItemCommand" OnItemDataBound="dgrdFile_ItemDataBound">
<ItemStyle BackColor="#ffffff" CssClass="blacktext" VerticalAlign="Middle" HorizontalAlign="left"
Height="20px"></ItemStyle>
<AlternatingItemStyle BackColor="#ffffff" CssClass="blacktext" VerticalAlign="Middle"
HorizontalAlign="left" Height="20px"></AlternatingItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="File" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="20%"
ItemStyle-CssClass="blacktext">
<ItemTemplate>
<asp:LinkButton ID="lnkFileOpen" runat="server" CommandName="Open" CssClass="links"
Text='<%# (DataBinder.Eval(Container, "DataItem.PRODUCT_NAME")) %>' CommandArgument='<%# DataBinder.Eval(Container, "DataItem.PRODUCT_ID") %>'> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="MODIFIEDON" HeaderText="Last Updated" ItemStyle-CssClass="blacktext" ItemStyle-HorizontalAlign="left" ItemStyle-Width="13%" />
<asp:BoundColumn DataField="MODIFIEDBY" HeaderText="Last Updated By" ItemStyle-CssClass="blacktext" ItemStyle-HorizontalAlign="left" ItemStyle-Width="12%" />
<asp:BoundColumn DataField="Users" HeaderText="Users" ItemStyle-CssClass="blacktext" ItemStyle-HorizontalAlign="left" ItemStyle-Width="38%" />
<asp:BoundColumn DataField="UPLOADED_FILE" Visible="False" />
<asp:TemplateColumn ItemStyle-HorizontalAlign="Center" ItemStyle-Width="2%">
<ItemTemplate>
<asp:LinkButton ID="lnkLock" CommandName="Lock" runat="server" Width="18px" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.PRODUCT_ID") %>'>
<asp:ImageButton ID="imgLock" runat="server" ImageUrl="images/lock.gif" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn ItemStyle-HorizontalAlign="Center" ItemStyle-Width="2%">
<ItemTemplate>
<asp:Label ID="lblEditLink" runat="server"><a class="popCRMADDFILE AncBtnFE" href="AddUpdateFile.aspx?mode=U&categoryId=<%=categoryId %>&ID=<%# DataBinder.Eval(Container, "DataItem.PRODUCT_ID") %>">
<img src="images/edit.gif" title="Edit File"/></a></asp:Label>
<asp:Literal ID="lblDisableLink" runat="server"></asp:Literal>
<asp:HiddenField ID="hdnEditStatus" runat="server" Visible='false' Value='<%# DataBinder.Eval(Container, "DataItem.Rights") %>' />
<asp:HiddenField ID="hdnFolderPath" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.Folderpath") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn ItemStyle-HorizontalAlign="Center" ItemStyle-Width="2%">
<ItemTemplate>
<asp:Label ID="lblCopyFile" runat="server"><a class="popCRMADDFILE AncBtnFE" href="FileCopy.aspx?mode=U&categoryId=<%=categoryId %>&ID=<%# DataBinder.Eval(Container, "DataItem.PRODUCT_ID") %>">
<img src="images/copy.png" title="Copy File to other folder"/></a></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn ItemStyle-HorizontalAlign="Center" ItemStyle-Width="3%">
<ItemTemplate>
<asp:LinkButton ID="lnkDeleteFile" ToolTip="Here here to delete file." CommandName="deleteFile"
runat="server" Width="18px" Text="." CssClass="buttonsDelete" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.PRODUCT_ID") %>'> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
protected void dgrdFile_ItemCommand(object source, DataGridCommandEventArgs e)
{
ltrDelMsg.Text = "";
if (e.CommandName == "Lock")
{
objFile.P_FileId = Convert.ToInt16(e.CommandArgument);
if ((((HiddenField)e.Item.FindControl("hdnEditStatus")).Value != "R"))
{
objFile.LockUnlockFile("Y", Session["UID"].ToString());
}
}
if (e.CommandName == "Open")
{
if ((objFile.CheckLockedFile(e.CommandArgument.ToString()) == true) && (((HiddenField)e.Item.FindControl("hdnEditStatus")).Value != "R"))
ltrErrorMsg.Text = "File is in Use";
else
{
}}}
protected void dgrdData_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
if (Convert.ToString(DataBinder.Eval(e.Item.DataItem, "RIGHTS")).ToUpper() == "R")
{
((Label)e.Item.FindControl("lblEditLink")).Visible = false;
((Literal)e.Item.FindControl("lblDisableLink")).Text = "<img src=\"images/folder.gif\" style='cursor:pointer;' Title=\"You are not authorized user to edit folder permissions.\"/>";
((LinkButton)e.Item.FindControl("lnkDeleteFolder")).Enabled = false;
((LinkButton)e.Item.FindControl("lnkDeleteFolder")).ToolTip = "You are not authorised to Delete Folder.";
}
else
{
LinkButton lnk;
lnk = (LinkButton)e.Item.FindControl("lnkDeleteFolder");
lnk.Attributes.Add("onclick", "return confirmDeleteFolder();");
((Label)e.Item.FindControl("lblEditLink")).Visible = true;
}
}
}
Related Alrticles