Friday, 4 July 2014

OnClick event upon changing the item in a DropDown

This posts says about triggering an event upon selecting a item in a DropDown.

First add the DropDownList into the page, update the asp snippet of the dropdown control in .aspx file ash shown as below.

<asp:DropDownList ID="DropDownList1" runat="server"  OnDataBound = "DropDown1_OnDataBound" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="100px" AutoPostBack="True" BackColor="#666666">      </asp:DropDownList>



Below functions are to be used for the dropdown

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
             //Write the code which you want to trigger upon selecting an item in the dropdown list
        }


protected void DropDown1_OnDataBound(object sender, EventArgs e)
        {
            if (this.DropDownList1.Items.Count > 0)
            {               
                this.DropDownList1.Items.Insert(0, new ListItem("--Select--", "-1"));                
            }                        
        }


Thursday, 3 July 2014

Reading Data from the SQL

This post says about how to read data from SQL

string oCommand = "Select * from Table";
SqlConnection connection = new SqlConnection(ConnectionString);

using (connection)
    {
            SqlCommand command = new SqlCommand(oCommand,connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
                {
                     while (reader.Read())
                     {
                          // use reader.GetString(0) to read the data
                      }
                }
            else
                {
                       Console.WriteLine("No rows found.");
                 }
            reader.Close();
   }


OnClick event upon selecting Row on the GridView

This post says about how to select a row upon clicking GridView and accessing its cell values

Firstly add the GridView into the page from tool box.

Check the .aspx code and update if necessary to

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BorderStyle="Double" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "GridView1_OnSelectedIndexChanged"> </asp:GridView>

For binding the data to gridview while the page gets loaded, follow the below link
DataBinding to GridView

Add the following functions into the .aspx.cs file

protected void GridView1_OnSelectedIndexChanged(object sender, EventArgs e)

        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowIndex == GridView1.SelectedIndex)
                {
                    row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
                    row.ToolTip = string.Empty;
                }
                else
                {
                    row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
                }
            }
        }


protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (Page.IsPostBack)
            {

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
                    //e.Row.ToolTip = "Click to select this row.";
                }
            }
        }



For accessing the selected row data you can access it the following way

GridView1.SelectedRow.Cells[3].Text


Binding Data to the GridView


This post says about how to bind the data into the GridView

Firstly add the GridView into the page from tool box.

Check the .aspx code and update if necessary to

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" BorderStyle="Double" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "GridView1_OnSelectedIndexChanged"> </asp:GridView>

In the .aspx.cs file, update the Page_Load function

if (!Page.IsPostBack)
{
       string oCommand = "SELECT * from table";
       SqlConnection oConnection = new SqlConnection(ConnectionString);
       SqlCommand oCommand = new SqlCommand(oCommand );
       oCommand.Connection = oConnection;
       SqlDataAdapter adpt = new SqlDataAdapter(oCommand);
       DataSet ds = new DataSet();
       adpt.Fill(ds);
       grid.DataSource = ds.Tables[0];
       grid.DataBind();
}