Thursday, December 15, 2011

Refresh UpdatePanel on postback, using JavaScript in Asp.net


in .aspx page

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<div id="Container" onclick="__doPostBack('UpdatePanel1', '');">
<asp:UpdatePanel runat="server" ID="UpdatePanel1" OnLoad="UpdatePanel1_Load"> <ContentTemplate> <asp:Label runat="server" ID="Label1" /> </ContentTemplate> </asp:UpdatePanel>
    <p>
        <asp:Button ID="
Button1
" runat="server" Text="Button" /> </p>
        <asp:Label runat="server" ID="Label2" />

in .cs page

protected void UpdatePanel1_Load(object sender, EventArgs e)
{
  Label1.Text = DateTime.Now.ToString();
}

protected void Button1_Click(object sender,EventArgs e)
{
   Label2.Text = DateTime.Now.ToString();
}


output

On Button click updatepanel get postback first
(display Label1.Text first) on second time Click (Label2.Text value get display)


How to optimize code using .dll (data link library) file in asp.net

Here is the method how you can reduce code using .dll file
This also use for code re-usability.

Phase 1:

Step 1: Open project in Visual Studio
            (File->New Project-> Project)

Step2: Select window tab -> Class library














Step3: In Class1.vb
           Create function or method, i am creating method in which i will insert record in table.


Imports System.Data.SqlClient
Public Class Class1


    Public Sub insert(ByVal sqlstr As String,ByVal constring As String)

''''sqlstr contain sqlquery while constring contain connectionstring


        Dim con As New SqlConnection(constring)
        Dim cmd As New SqlCommand(sqlstr, con)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Sub
End Class


Step4: Now Run the program
           it will not run but show dialogue box, just ignore it.

Step5: Open the destination folder where this application is save.
  
Step6: Copy the ClassLibrary1.dll file from (ClassLibrary1\bin\Debug) and paste anywhere in your pc.


Phase 2: 


Step1: Open Website in Visual Studio
            (File->New Website->Asp.net Website)

Step2: Now add ClassLibrary1.dll file, which you have created in phase1 in your application,
           by following way
         .
           
        

    

           














Press ok.
ClassLibrary1.dll get added in bin folder within solution explorer.

Step3: Now import the ClassLibrary1.dll file

Import ClassLibrary1

write the code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       Dim sqlstr,constring as String
       sqlstr="Insert into test values('ani')"
constring="write database connectionstring"
      Dim c As New Class1()
                          '' it will display method used in Class1"
       c1.insert(sqlstr,constring)
    End Sub


Step4: Now run the program
           it will insert record in test table by calling insert method from ClassLibrary1.dll


You can use this .dll file anywhere such as in different application,application  in different pc






Wednesday, December 14, 2011

Display the values of particular Cell of GridView in asp.net with vb

Example:

in .aspx page


<div>
    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    <p>
        <asp:Button ID="Btndisplayvalue" runat="server" Text="Button" />
    </p>


in .vb page


 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim a As String
        a = "anish"
        GridView1.DataSource = a
        GridView1.DataBind()


    End Sub


    


    Protected Sub Btndisplayvalue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btndisplayvalue.Click
        Dim dr As GridViewRow
        For Each dr In GridView1.Rows
            Response.Write(dr.Cells(0).Text)
        Next
    End Sub




it will display values of cell(0) present in GridView1

Mail the data by dynamically reading from text file in Asp.net with vb


IN .vb

Imports System.IO

Imports System.Net
Imports System.Net.Mail


 Dim line As String = ""


        Using reader As StreamReader = New StreamReader("c:\read.txt")
         
                      line = reader.ReadToEnd


        End Using


'''Using smtp.gmail.com '''



    '-------------GMAIL-------------'
                  
                    Dim mailMsg1 As New Net.Mail.MailMessage
                    Dim fromAdd As Net.Mail.MailAddress
                    fromAdd = New Net.Mail.MailAddress(strloginEmailID)

                    mailMsg1.To.Add(dSet1.Tables(0).Rows(i).ItemArray(0).ToString())

                    chk = dSet1.Tables(0).Rows(i).ItemArray(0).ToString()
                    mailMsg1.IsBodyHtml = True
                    mailMsg1.Priority = MailPriority.High
                    mailMsg1.Subject = "BarodaWeb" ' "New Task Assign!"

                    mailMsg1.Body = line
                    Dim smtp As New Net.Mail.SmtpClient()
                    smtp.Host = strMailhost

                    smtp.Port = strMailport
                    smtp.EnableSsl = True
                    smtp.UseDefaultCredentials = False
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network
                    smtp.Credentials = New NetworkCredential(strMailid, strMailpass)

                    smtp.Send(mailMsg1)

                    '--------------------------------------------------


How to set colour of particular gridview cell in asp.net


in .aspx page

  <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>

in .cs page  

protected void Page_Load(object sender,EventArgs e)
{
       String a;
        a = "test"
        GridView1.DataSource = a;
        GridView1.DataBind();
       GridView1.Rows(0).Cells(0).BackColor = Drawing.Color.Cyan;
}


you  can  adjust color in any cells of gridview by adjusting Rows(2).Cells(1)

Saturday, November 5, 2011

How many character remaining code in asp.net

Character count get increment and decrement when it get typed or deleted from textbox


 <script language="javascript">
    function count()
    {
        var keyid=event.KeyCode;

        var c=document.getElementById('TextBox1').value.length;
        if(c<=10)
        {
        document.getElementById('Text1').value=(10-c);
        }
        else
        {
           alert("Character exeed");
        }
  switch (keyid)
 {
    case 8:
    document.getElementById('Text1').value=( document.getElementById('Text1').value - c);
    case 46:
    document.getElementById('Text1').value=(document.getElementById('Text1').value - c);
 }
      
    }
  </script>


 <div>
  
        <asp:TextBox ID="TextBox1" runat="server" onKeyPress="count()" onKeyDown="count()" MaxLength="10"></asp:TextBox>
        <br />
      
    <label>10-></label> <input id="Text1" type="text" disabled="disabled" />
    </div>

Wednesday, November 2, 2011

CultureInfo Class - populate the dropdownlist with countries using culture in asp.net with vb

in Default.aspx.vb

Imports System.Globalization
Imports System.Collections.Generic



 Public Function getcountry() As List(Of String)
        Dim list As New List(Of String)()
        Dim culture As CultureInfo() = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures Or CultureTypes.SpecificCultures)

        For Each info As CultureInfo In Culture
            Dim info2 As New RegionInfo(info.LCID)
            If Not List.Contains(info2.EnglishName) Then
                List.Add(info2.EnglishName)
                List.Add(info2.DisplayName)

            End If

        Next
        Return List
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      DropDownList1.DataSource = getcountry()

        DropDownList1.DataBind()
         DropDownList1.Items.Insert(0, "select")



    End Sub