Pages

Wednesday, 16 May 2012

Details view example in Asp.net



<asp:DetailsView ID="DetailsView1" runat="server" Width="100%"
        AutoGenerateRows="False" EmptyDataText="No Data Found"
        GridLines="None" onitemcommand="DetailsView1_ItemCommand"
        onitemdeleting="DetailsView1_ItemDeleting"
        onitemupdating="DetailsView1_ItemUpdating"
        onmodechanging="DetailsView1_ModeChanging">
        <FooterStyle Font-Bold="True" />
        <CommandRowStyle Font-Bold="True" />
        <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" />
        <FieldHeaderStyle Font-Bold="True" />
        <EmptyDataRowStyle Font-Bold="True" />
        <PagerStyle HorizontalAlign="Left" />
        <Fields>          
            <asp:TemplateField ShowHeader="false" >
                <EditItemTemplate>
                    <asp:Label ID="lblIdEdit" runat="server" Text='<%# Bind("CntId") %>' Visible="false"></asp:Label>
                            <cc2:Editor ID="EditorEdit" runat="server" Content='<%# Bind("Description") %>' />
                </EditItemTemplate>              
                <ItemTemplate>
                    <asp:Label ID="lblId" runat="server" Text='<%# Bind("CntId") %>' Visible="false"></asp:Label>
                            <asp:Label ID="lblDetails" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ButtonType="Image" EditImageUrl="~/Images/Buttons/Admin_edit_btn.gif" UpdateImageUrl="~/Images/Buttons/admin_update_btn.gif" CancelImageUrl="~/Images/Buttons/Cancel_btn.gif" ShowEditButton="true" DeleteImageUrl="~/Images/Buttons/delete_btn.gif" ShowDeleteButton="true" />      
        </Fields>
        <HeaderStyle Font-Bold="True" ForeColor="White" />
        <EditRowStyle />      
    </asp:DetailsView>


C# code:-----------------------------
-----------------------------------------------------------------------------------------------------------
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            viewcontact();
        }
    }
    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        string lblid = ((Label)DetailsView1.FindControl("lblId")).Text.ToString();
        obj1.deletecontactus(Convert.ToInt32(lblid));
        viewcontact();
        pnlCnt.Visible = true;
        pnlgrd.Visible = false;
       
    }
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        try
        {
            string Id = ((Label)DetailsView1.FindControl("lblIdEdit")).Text.ToString();
            string Remarks = ((Editor)DetailsView1.FindControl("EditorEdit")).Content.ToString();
            obj1.updatecontactus(Convert.ToInt32(Id), Remarks);
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);          
            viewcontact();
        }
        catch (Exception ex)
        {
           
        }
    }
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        if (e.CancelingEdit == true)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            viewcontact();
        }
    }  

No comments:

Post a Comment