Thursday 27 June 2013

GridView row delete using row command event

Below Example is very useful for How to delete a row in gridview . Let me know In case you are facing any problem

Design :

GridView Row Command Event for sorting in asp.net ?

Example for Gridview Row Command Event : 
=================================
 This below code is useful for Gridview sorting Purpose .  Let me know if you are facing any problem .

Design :

<asp:GridView ID="gvNew" runat="server" AutoGenerateColumns="false" OnRowCancelingEdit="gvNew_RowCancelingEdit"
                                                OnRowCommand="gvNew_RowCommand" OnRowDataBound="gvNew_RowDataBound" OnRowEditing="gvNew_RowEditing"
                                                OnRowUpdated="gvNew_RowUpdated" OnRowUpdating="gvNew_RowUpdating" AllowPaging="true"
                                                PageSize="30" DataKeyNames="ecm_id" OnPageIndexChanging="gvNew_PageIndexChanging"
                                                ShowFooter="false" OnRowDeleting="gvNew_RowDeleting">
<Columns>

  <asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
                                                        <HeaderTemplate>
                                                            <asp:LinkButton ID="lnkECM_ID" runat="server" Text="ECM Number" CommandName="ECM_ID"></asp:LinkButton>
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="lblECM_id" Width="100px"></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="txtECMidET" Width="100px"></asp:Label>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
</Columns>
<asp:GridView>



Coding :

static int i;

Page load you can declare the static variable . This variable is used for ascending and descending purpose .
Default set to 0 .
i = 0;


 protected void gvNew_RowCommand(object sender, GridViewCommandEventArgs e)
    {


//This code is useful for Grid view sorting purpose

if (e.CommandName == "ECM_ID")
        {
            if (i == 0)
            {
                ViewState["SortString"] = "ecm_id ASC";
               //Below method is used to bind data to Grid view
                BindGridDataAll();
                i = 1;
            }
            else
            {
                ViewState["SortString"] = "ecm_id DESC";
               //Below method is used to bind data to Grid view
                BindGridDataAll();
                i = 0;
            }
        }
}

// Declare dataview

 public void BindGridDataAll()
    {   
            ds = ObjAtSite.GetECMdataUsingPId();
            dvData = ds.Tables[0].DefaultView;
            string SortString = "";
            if (ViewState["SortString"] != null)
            {
                SortString = ViewState["SortString"].ToString();
            }
            if (SortString != "")
            {
                dvData.Sort = SortString;
                gvNew.DataSource = dvData;
                gvNew.DataBind();
            }
            else
            {
                gvNew.DataSource = dvData;
                gvNew.DataBind();
            }
    }



How to write code for Grid view RowDataBound Event ?

In this article I will explain what GridView RowDataBound Even is and how to bind data to the Grid view Dropdown list and Delete conformation message in ASP.NET

Grid view RowDataBound Event fired in page load. This below code is useful for bind data to the Grid view Dropdown list and Delete conformation message.

Gridview control is most common control in all asp.net applications to display the data and it’s very powerful control and lot of built in features. In this article, I will explore some tips and tricks in rowdatabound event in gridvew control, Actually RowDatabound event will occur when data row is bound to the control in the gridview control in ASP.NET

Syntax:

<asp:GridView OnRowDataBound="GridViewRowEventHandler" /> 

 Example:
 Below is the code for Grid view designing 

DOT NET Page life cycle events


An event handler is basically a function or subroutine, bound to the event, using declarative attributes like Onclick or handle. At each stage of the page life cycle, the page raises some events, which could be coded. 
Following are the page life cycle events:
  • PreInit . PreInit is the first event in page life cycle. It checks the IsPostBack property and determines whether the page is a postback. It sets the themes and master pages, creates dynamic controls and gets and sets profile property values. 


  • Init . Init event initializes the control property and the control tree is built. This event can be handled by overloading the OnInit method or creating a Page_Init handler.

  • InitComplete . InitComplete event allows tracking of view state. All the controls turn on view-state tracking.


What is the difference between String and stringBuilder in ASP.NET ?

String

1.Its a class used to handle strings.
2.Here concatenation is used to combine two strings.
3.String object is used to concatenate two strings.
4.The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old 
string is deleted
5.That is why  "Strings are immutable".
6. Slower


Example  :

 string s = string.Empty;
for (i = 0; i < 1000; i++) {
  s += i.ToString() + " ";
}