Thursday, September 15, 2011

Getting Previous Page's control that caused postback in current page

First checking normal postback or crosspage postback:
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
       // Do Something}

Previous Page Aspx:
  <asp:LinkButton ID="LnkID" runat="server" PostBackUrl="" Text="Link to New Page" />

We must use "PostBackUrl"  property here

Current Page CS:
page_load
{
    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
    {
         Control LnkCtrl = GetPostBackControl(this.Page);
         string ctrlID = LnkCtr.ID;
    }
}

private Control GetPostBackControl(Page page)
        {
            Control control = null;

            string ctrlname = page.Request.Params.Get("__EVENTTARGET");
            if (ctrlname != null && ctrlname != string.Empty)
            {
                control = page.PreviousPage.FindControl(ctrlname);
            }
            else
            {
                foreach (string ctl in page.Request.Form)
                {
                    Control c = page.FindControl(ctl);
                    if (c is System.Web.UI.WebControls.Button)
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control;
        }

GridView Validation on edit update action

Use below JavaScript function in .aspx

function ValidateGrid(e)
        {               
            var cnt = 0;
            var grid = document.getElementById('<%= gvActionItems.ClientID %>');
            for(i=1;i<grid.rows.length;i++)
                {               
                    if(grid.rows[i].cells[4].children[0].id == e.id)
                    {                       
                        if(grid.rows[i].cells[0].children[0].value == "")
                        {
                            alert("Please add Action Item");
                            cnt = 1;
                            return false;
                        }
                        if(grid.rows[i].cells[3].children[0].value == "")
                        {
                            alert("Please enter a Target Date");
                            cnt = 1;
                            return false;
                        }                     
                    }                 
                }
            if(cnt > 0)
                return false;
            else
                return true;
     }

In .cs file under GridView's RowDataBound Event put this code
protected void gvActionItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[4].HasControls())
                {
                    LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[4].Controls[0]);
                    lnkbtnDelete.Attributes.Add("id", "LnEdtUpdt");
                    lnkbtnDelete.Attributes.Add("onclick", "return ValidateGrid(this);");
                }
            }
        }