Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Pass Eval in JavaScript function as parameter in Gridview?

How to pass Eval value to JavaScript function as parameter in Gridview?

I want to pass dynamic row value to JavaScript function to perform some action.

For example I want select checkbox in grid-view  if user is active only on click.

Client side grid view code:

<asp:GridView runat="server" ID="dgPublisData" >                 
  <Columns>
<asp:TemplateField HeaderText="Cancel" Visible="true">
  <ItemTemplate>
<input type="checkBox" ID="isActive" runat="server" class="cs-button" onclick='<%# string.Format("EnableData (\"{0}\");return false;",Convert.ToBoolean(Eval("User .IsActiveUser")) %>'  Value="Status"  '/>     
</ItemTemplate>
</asp:TemplateField>
 
</Columns>
                   
</asp:GridView>

 

JavaScript function

 

function EnableData (isActiveUser) {
    // do some script here
if (isActiveUser == "true") {
     //do some script here to select checkbox
 }
     else
         {
          // add some notification
 
         }
}

Asp.net page life cycle

When you run asp.net page goes through life cycle in which performs series of steps.

General page life cycle goes with following stages:

 

Page request:

When ASP.NET gets a page request, it decides whether to parse and compile the page, or there would be a cached version of the page; accordingly the response is sent.

Start:

In the start stage, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property.

Initialization

At this stage, the controls on the page are assigned unique ID by setting the UniqueID property and the themes are applied. For a new request, postback data is loaded and the control properties are restored to the view-state values.

Page load

At this stage if current request is post back, control properties are set using the view state and control state values.

Rendering

If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.)

Page rendering

At this stage, view state for the page and all controls are saved. The page calls the Render method for each control and the output of rendering is written to the OutputStream class of the Response property of page.

Unload

The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.

 

Page life cycle events:

For each stage of the life cycle of a page, the page raises events that you can handle to run your own code.

PreInit

1.  Check the IsPostBack property to determine whether this is the first time the page is being processed. 

2.  Create or re-create dynamic controls and gets and sets profile property values

3.  Set a master page dynamically and set the theme property dynamically.

4.  This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.

protected void Page_PreInit(object sender, EventArgs e)
       
{
            //page PreInit code here
 
}


Init

1.  This event fires after each control has been initialized.

2.  Each control's UniqueID is set and any skin settings have been applied.

3.  Use this event is used to read or initialize control properties and the control tree is built

4.  This event can be handled by overloading the OnInit method or creating a Page_Init handler.

protected void Page_Init(object sender, EventArgs e)
        {
            //page Init code here
 
        }

InitComplete

1.  This event is used to track view state, in this event view state values are not loaded.

2.  Use this event for processing tasks that require all initialization to be complete.

protected void Page_InitComplete(object sender, EventArgs e)
        {
            //page Init complete code here
 
        }


LoadViewState

LoadViewState event allows loading view state information into the controls.

LoadPostData

 During this phase, the contents of all the input fields are defined with the <form> tag are processed.

OnPreLoad

1.  Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.

2.  Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

protected void Page_OnPreLoad(object sender, EventArgs e)
        {
            //page OnPreLoad code here
 
        }

Load

1.  The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded.

2.  The Load event of individual controls occurs after the Load event of the page.

3.  This is the first place in the page lifecycle that all values are restored.

4.  Most code checks the value of IsPostBack to avoid unnecessarily resetting state.

5.  You may also call Validate and check the value of IsValid in this method.

6.  You can also create dynamic controls in this method.

7.  Use the OnLoad event method to set properties in controls and establish database connections.

protected void Page_Load(object sender, EventArgs e)
        {
            //page load code here
 
        }
 

LoadComplete

1. In this event loading process is completed, control event handlers are run, and page validation takes place.

2. Use this event for tasks that require that all other controls on the page be loaded.

protected void Page_LoadComplete(object sender, EventArgs e)

        {

            //page LoadComplete code here

 

        }

 

OnPreRender

1.  Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls

2.  Use the event to make final changes to the contents of the page or its controls before the rendering stage begins.

protected void Page_OnPreRender(object sender, EventArgs e)
        {
            //page OnPreRender code here
 
        }

OnSaveStateComplete

1.  Raised after view state and control state have been saved for the page and for all controls.

2.  Before this event occurs, ViewState has been saved for the page and for all controls.

3.  Any changes to the page or controls at this point will be ignored.

4.  Use this event perform tasks that require the view state to be saved, but that do not make any changes to controls.

protected void Page_OnSaveStateComplete(object sender, EventArgs e)
        {
            //page OnSaveStateComplete code here
 
        }

Render Method

1.  This is not an event; this is a method of the page object and its controls.

2.  All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.

3.  The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser.

UnLoad

1.  Last event called in the asp.net page life cycle

2.  Final cleanup is done and all resources and references, such as database connections, are freed.

3.  This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad handler

protected void Page_UnLoad(object sender, EventArgs e)
        {
            //page UnLoad code here
 
        }