Thursday, March 21, 2019

Calling Server side method from Client Script ASP.Net

A way to call service from client browser without a post back. This is only a structure, this will receive call back request from client page (aspx) by implementing ICallbackEventHandler interface


Step 1: [new Class]
public partial class ClientCallbackPage : System.Web.UI.Page, ICallbackEventHandler

{


}

Step 2: [Interface implementation]
The ICallbackEventHandler has two methods method 1: RaiseCallBackEvent() that receives request from the client browser, accept param as string argrument, method 2: GetCallbackResult() return result back to client browser.

public void RaiseCallbackEvent(string callbackEventArgument)

{
//m_callbackEventArgument is a string variable declare in te class
m_callbackEventArgument = callbackEventArgument;
}

Step 3:[Interface implementation]

//method to execute server method and return result back
public string GetCallbackResult()
{
//type ur code here

return (something); //return value will send to the client script method
}

Step 4:

//Client script
function ClientCallbackScript(result, context)
{
//result will return the data from server.(see return (something); above)

//type ur code here
}

Step 5:

// in page load we are deciding for which event
//the server side method to invoke
if (! Page.IsPostBack)
{
string callbackEventReference =
Page.ClientScript.GetCallbackEventReference(this,
"parameter pass from client", "ClientCallbackScript",
"null");

txtName.Attributes["onclick"] = callbackEventReference;//event fire for each click in the text box
}

Tuesday, March 19, 2019

Display <div> content in column wise

Output






Here explain a way to display multiple <div> in column wise. To achieve this, we required one Parent <div>, and parent div contains multiple child <div>, we list child <div> in column wise.

To achieve this we just need a style like below.

<style>

 

* {

           box-sizing : border-box ;
    }

 

/* Create equal columns that floats next to each other */
.column{

                        float:left ;
          width:50%;

                        padding : 10px;

}

 

/* Clear floats after the columns */

.row:after{

                 content : "";

                        display : table;

                        clear : both;

}

</style>


hope style is straight forward, width in style-class .column and display in style-class .row:after is crucial here. Width property defining number of column to be displayed.

 

 

HTML
you should required one parent and multiple child <div> tags. Below is parent should look like below. Parent should refer style-class "row" 

 

<divname="parent"class="row"style="width:100%;">


Child should refer style-class "column" and put your content under the <div> tag 

 

<divname="child"class="column"style= "background: cornflowerblue" >

 

Once complete above, your child <div> will list in two column. If you decrease the width property of style-class .column to 30%, then <div> will list in 3 column, like that you could reduce the width to list desired level of columns.


HTML should be like below.

 

<divname="parent"style="width: 100%;"class="row">

           <divname="child"class="column"style= "background: cornflowerblue" >

                     <divname="childcontent">

                               My Content 1

                     </div>

           </div>

            

           <divname="child"class="column"style= "background: cornflowerblue" >

                     <divname="childcontent">

                               My Content 2

                     </div>

           </div>

 

           <divname="child"class="column"style= "background: cornflowerblue" >

                     <divname="childcontent">

                               My Content 3

                     </div>

           </div>

 

           <divname="child"class="column"style= "background: cornflowerblue" >

                     <divname="childcontent">

                               My Content 4

                     </div>

           </div>

</div>

 

Output

Friday, February 22, 2019

Vb.net change config file at runtime - Windows application

-->
Below help you to switch application configuration at runtime.
Suppose you are maintain different configuration file for different task.
Suppose I have a project name Vehicle, and below are my project config structure



 Main configuration is vehicle.config, and Byke and car has their own configuration setting. Now how my code fetch configuration data from Byke and Car when my application is running? How all code in Byke/car reads from its own configuration?
Solution
1. First create a class which is responsible for config change, below class will do it
Public MustInherit Class AppConfig
    Implements IDisposable

    Public Shared Function ChangeConfigPath(ByVal path As String) As AppConfig
        Return New ChangeAppConfig(path)
    End Function

    Private _OldConfig As String = String.Empty
    Public Overridable Property OldConfig() As String
        Get
            Return _OldConfig
        End Get
        Set(ByVal value As String)
            _OldConfig = value
        End Set
    End Property

    Private _DisposedValue As Boolean = False
    Public Overridable Property DisposedValue() As Boolean
        Get
            Return _DisposedValue
        End Get
        Set(ByVal value As Boolean)
            _DisposedValue = value
        End Set
    End Property

Public MustOverride Sub Dispose()


  Private Class ChangeAppConfig
        Inherits AppConfig

        Public Sub New(ByVal path As String)

            If OldConfig = String.Empty Then
                OldConfig = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString
            End If

            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path)

            GetType(System.Configuration.ConfigurationManager).GetField("s_initState", System.Reflection.BindingFlags.NonPublic OrSystem.Reflection.BindingFlags.[Static]).SetValue(Nothing, 0)

            Dim clConfigSystem As Object = GetType(System.Configuration.ConfigurationManager).GetField("s_configSystem", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Static]).GetValue(Nothing)
            Dim clConfigHost As Object = clConfigSystem.GetType.GetField("_configHost", System.Reflection.BindingFlags.NonPublic OrSystem.Reflection.BindingFlags.[Instance]).GetValue(clConfigSystem)
            Dim clConfigPaths As Object = clConfigHost.GetType.GetField("_configPaths", System.Reflection.BindingFlags.NonPublic OrSystem.Reflection.BindingFlags.[Instance]).GetValue(clConfigHost)
            If Not clConfigPaths Is Nothing Then
                clConfigPaths.GetType.GetField("_applicationConfigUri", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Instance]).SetValue(clConfigPaths, path)
            End If

        End Sub

        Public Overrides Sub Dispose()
        End Sub
    End Class

    Public Sub Dispose1() Implements System.IDisposable.Dispose
        If Not Me.DisposedValue Then
            Dim configPath As String = OldConfig
            If String.IsNullOrWhiteSpace(configPath) Then
                configPath = Common.Application_Config_Path
            End If
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configPath)
            GetType(System.Configuration.ConfigurationManager).GetField("s_initState", System.Reflection.BindingFlags.NonPublic OrSystem.Reflection.BindingFlags.[Static]).SetValue(Nothing, 0)

            Dim clConfigSystem As Object = GetType(System.Configuration.ConfigurationManager).GetField("s_configSystem", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Static]).GetValue(Nothing)
            Dim clConfigHost As Object = clConfigSystem.GetType.GetField("_configHost", System.Reflection.BindingFlags.NonPublic OrSystem.Reflection.BindingFlags.[Instance]).GetValue(clConfigSystem)
            Dim clConfigPaths As Object = clConfigHost.GetType.GetField("_configPaths", System.Reflection.BindingFlags.NonPublic OrSystem.Reflection.BindingFlags.[Instance]).GetValue(clConfigHost)
            If Not clConfigPaths Is Nothing Then
                clConfigPaths.GetType.GetField("_applicationConfigUri", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Instance]).SetValue(clConfigPaths, Common.Application_Config_Path)
            End If

            Me.DisposedValue = True
        End If
        GC.SuppressFinalize(Me)
    End Sub
End Class




2. To call above class for changing config file, you needs to put your code inside using block

            Ex: code to read appSetting from Bype app.config, you have to write something like below
           
            Using AppConfig.ChangeConfigPath(“..\Vehicle\Byke\app.config”) ‘write the full app.config path here
           Dim BykemaxSpeed As string = ConfigurationManager.AppSetting[“BykeMaxSpeed”]
End Using
Note: app.config path should be a full path
Whenever you needs to read something for your config file, then write those code inside using block.