Friday, 6 June 2014

How to add Expand And Collapse Feature In REPEATER Control

There are many times when a we all needs to Expand and Collapse text on a website. This feature is especially useful when it comes to setting up FAQs or creating quiz features.In this article, i am going to give complete overview on how to add Expand and Collapse feature in ASP.NET using JavaScript function.


aspx.cs page


aspx page

<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function ExpandableDisplay(id) {
            // 'd' was concatenated to get id of description text
            var descelem = document.getElementById('d' + id);
            if (descelem) {
                if (descelem.style.display != 'block') {
                    descelem.style.display = 'block';
                    descelem.style.visibility = 'visible';
                }
                else {
                    descelem.style.display = 'none';
                    descelem.style.visibility = 'hidden';
                }
            }
        }
    </script>

    <style type="text/css">
        .header { font-size: larger; margin: 10px; font-weight: bold; cursor: pointer; background-color: #cccccc; font-family: Verdana; }
        .description { display: none; margin: 15px; visibility: hidden; font-family: Verdana; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="repeaterItems" runat="server">
            <ItemTemplate>
                <%--'h' used for making it header text--%>
                <div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header"
                    onclick='ExpandableDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
                    <b>Price: </b><%# DataBinder.Eval(Container.DataItem, "price") %><b> Rs.</b>
                </div>
                <%--'d' used for making it description text--%>
                <div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="description">
                    <b>Quantity Available:</b> <%# DataBinder.Eval(Container.DataItem, "quantity") %><br />
                </div>
            </ItemTemplate>
        </asp:Repeater>

    </form>
</body>
</html>

Expand and Collapse Output




That’s it!!…..Happy Programming...

Thursday, 5 June 2014

Implement Paging in DataList using ASP.NET

When building ASP.NET Web applications, one of the most common tasks is displaying data. ASP.NET offers a powerful data Web control - the DataList but imposes some limitations on its paging capabilities. Therefore, i am going to explain how to implement paging in DataList using ASP.NET.

aspx page

<form id="form1" runat="server">
        <table>
            <asp:DataList ID="dltItems" runat="server">
                <HeaderTemplate>
                    <h2>Item Details </h2>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <h4>Price</h4>
                        </td>
                        <td>
                            <h4>Quantity</h4>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <h5><%#Eval("price") %></h5>
                        </td>
                        <td>
                            <h5><%#Eval("quantity") %></h5>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:DataList>
        </table>
        <table>
            <tr>
                <td>
                    <asp:Button ID="btnfirst" runat="server" Font-Bold="true" Text="<<" OnClick="btnfirst_Click" /></td>
                <td>
                    <asp:Button ID="btnprevious" runat="server" Font-Bold="true" Text="<" OnClick="btnprevious_Click" /></td>
                <td>
                    <asp:Button ID="btnnext" runat="server" Font-Bold="true" Text=">" OnClick="btnnext_Click" /></td>
                <td>
                    <asp:Button ID="btnlast" runat="server" Font-Bold="true" Text=">>" OnClick="btnlast_Click" /></td>
            </tr>
        </table>
    </form>

aspx.cs page

    PagedDataSource pdsPaging = new PagedDataSource();
    int startPosition;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["vspaging"] = 0;
        }

        startPosition = Convert.ToInt32(ViewState["vspaging"].ToString());
        bindData();
    }

    void bindData()
    {
        string connectionString = "Server=localhost;Database=shopping;Trusted_Connection=true";
        SqlConnection myConnection = new SqlConnection(connectionString);
        SqlDataAdapter ad = new SqlDataAdapter("SELECT price, quantity FROM demo", myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        pdsPaging.DataSource = ds.Tables[0].DefaultView; //returns dataview
        pdsPaging.PageSize = 4; // specify no. of rows bind at startup
        pdsPaging.AllowPaging = true; // make it true to allow paging
        pdsPaging.CurrentPageIndex = startPosition;
        dltItems.DataSource = pdsPaging;
        dltItems.DataBind(); // bind data in datalist
    }

    protected void btnfirst_Click(object sender, EventArgs e)
    {
        startPosition = 0; // set it zero to go to first page
        bindData();
    }

    protected void btnprevious_Click(object sender, EventArgs e)
    {
        startPosition = Convert.ToInt32(ViewState["vspaging"].ToString());
        startPosition -= 1; // subtract one from actual postion
        ViewState["vspaging"] = startPosition; // set new value in viewstate
        bindData();
    }

    protected void btnnext_Click(object sender, EventArgs e)
    {
        startPosition = Convert.ToInt32(ViewState["vspaging"].ToString());
        startPosition += 1; // add one in actual postion
        ViewState["vspaging"] = startPosition; // set new value in viewstate
        bindData();
    }

    protected void btnlast_Click(object sender, EventArgs e)
    {
        // subtract one from no. of total pages because index starts from zero
        startPosition = pdsPaging.PageCount - 1;
        bindData();
    }

Paging Output



That’s it!!…..Happy Programming...

Saturday, 31 May 2014

How to add Global.asax in Visual Studio 2012

This question and this post talks about Global.asax template missing in Visual Studio 2012. I have a similar problem in Visual Studio 2012. What you can do is download the default template of Global.asax file and add it to the project. Simply download and copy it to the project. Then choose right click “Add Existing”, select the two files and you are done.

Global.asax download

Make sure you change the namespace in this file to what you are using in your project.





That’s it!!…..Happy Programming...

Friday, 30 May 2014

Password and Confirm Password validation in ASP.NET using JAVASCRIPT

If you have a form with a ‘confirm password’ field and you want to match that with a simple Javascript code rather than to use a heavy validation than this solution will helpful for you. In this, 'onkeyup' event was used to match both fields while user entering input. if you want to match it after textbox loses their focus, you have to call it on 'onblur' event, nothing more than this. You have to do something Like this:-

aspx code

<form id="form1" runat="server">
        <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
        <asp:TextBox ID="txtPassword" ClientIDMode="Static" runat="server"></asp:TextBox>
        <asp:Label ID="lblConfirmPassword" runat="server" Text="Confirm Password"></asp:Label>
        <asp:TextBox ID="txtConfirmPassword" ClientIDMode="Static" runat="server" onkeyup="matchPassword();"></asp:TextBox>
    </form>

Javascript function

    <script type="text/javascript">
        function matchPassword() {
          //store both values into variables.
        var password1 = document.getElementById('txtPassword');
        var password2 = document.getElementById('txtConfirmPassword');

          //set color to differentiate both conditions.
        var matchColor = "#66cc66";
        var unmatchColor = "#ff6666";

        if (password1.value.trim() == password2.value.trim()) {
              //if passwords match. 

            password2.style.backgroundColor = matchColor;
        } else {
              //if passwords do not match.
            password2.style.backgroundColor = unmatchColor;
        }
      }
    </script>
matched password output

odd password output

That’s it!!…..Happy Programming...

Tuesday, 27 May 2014

Difference Between Eval() and Bind() in Asp.Net

In this article, i am going to explain difference between Eval() and Bind(). The main difference between the two is Bind must be assigned to a property of server side control (runat="server") while you can assign Eval to server side or client side control. See implementation below:-

Example 1 :- We can do this
      <asp:TemplateField HeaderText="Price">
                  <ItemTemplate>
                      <%# Eval("price") %>
                  </ItemTemplate>
      </asp:TemplateField>

Example 2 :- We can't do this
      <asp:TemplateField HeaderText="Price">
                  <ItemTemplate>
                      <%# Bind("price") %>
                  </ItemTemplate>
      </asp:TemplateField>
Output of Example 2
Example 3 :- We can do this
  <asp:TemplateField HeaderText="Price">
       <ItemTemplate>
            <asp:Label ID="lblPrice" runat="server" Text='<%# Bind("price") %>'></asp:Label>
       </ItemTemplate>
  </asp:TemplateField>
The difference between Example 2 and Example 3 was nothing expect one server side control. As i already told you, Bind must be assigned to a property of server side controls, that's why Example 3 is perfect but Example 2 not.


That’s it!!…..Happy Programming...

Saturday, 24 May 2014

How to align text under each image in CSS

In this article, i am going to give some overview on how to align text under image in CSS.

HTML Code

    <div class="images">
                <ul>
                    <li><span><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a></span>
                        <a href="">See More...</a>
                    </li>
                    <li><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a>
                        <a href="">See More...</a>
                    </li>
                    <li><span><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a></span>
                        <a href="">See More...</a>
                    </li>
                    <li><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a>
                        <a href="">See More...</a>
                    </li>
                    <li><span><a href="#">
                        <img src="http://i1.asp.net/images/ui/asplogo-square.png?cdn_id=2014-05-21-001" /></a></span>
                        <a href="">See More...</a>
                    </li>
                </ul>
    </div>


CSS Code

.images {
display:block;
}

.images ul li {
display:inline;
list-style:none;
float:left;
text-align:center;
color:#000;
margin:0 10px 10px 0;
}

.images ul li a img {
border:6px solid #e1d9ec;
border-radius:50%;
width:150px;
height:150px;
}

.images ul li a {
display:block;
margin-bottom:5px;
opacity:0.8;
font-family:Verdana;
font-size:12px;
text-decoration:none;
letter-spacing:1px;
}

.images ul li a:hover {
opacity:1.0;
}

Output



That’s it!!…..Happy Programming...