Thursday, March 10, 2011

Master-detail scenario: dropdown and griedview using xmldatasource

Hi,
I creatde an example for how to use Xpath property of the XmlDatSource to filter the records based on the dropdown selected value.
Here in the below example on page load all the records in the grid are displayed, once the user selected the employee name from the dropdown then records will be filterd based on the dropdown's selected item.
Here is the compelete Aspx page with code:

<%@ Page Language="C#" %> 
 
<%@ Import Namespace="System.Data" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 
 
    protected void ddlEmployee_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        if (ddlEmployee.SelectedItem.Text != "Select....") 
            grvDataSource.XPath = "/Employees/Employee[@Name='" + ddlEmployee.SelectedItem.Text + "']"; 
        else 
            grvDataSource.XPath = "/Employees/Employee"; 
    } 
 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title>Read XML Example</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <table> 
            <tr> 
                <td> 
                    Select Employee : 
                </td> 
                <td> 
                    <asp:DropDownList ID="ddlEmployee" runat="server" DataSourceID="ddlDataSource" DataTextField="Name" 
                        DataValueField="Id" AppendDataBoundItems="true" AutoPostBack="True" OnSelectedIndexChanged="ddlEmployee_SelectedIndexChanged"> 
                        <asp:ListItem Selected="True">Select....</asp:ListItem> 
                    </asp:DropDownList> 
                </td> 
            </tr> 
            <tr> 
                <td colspan="2"> 
                    <asp:GridView ID="grvEmployee" runat="server" DataSourceID="grvDataSource"> 
                    </asp:GridView> 
                </td> 
            </tr> 
        </table> 
    </div> 
    <asp:XmlDataSource ID="ddlDataSource" runat="server" DataFile="~/App_Data/empData.xml"> 
    </asp:XmlDataSource> 
     <asp:XmlDataSource ID="grvDataSource" runat="server" DataFile="~/App_Data/empData.xml"> 
    </asp:XmlDataSource> 
    </form> 
</body> 
</html>

empData.xml:


<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee Id="1" Name="ABC" Department="Computers" Salary="10000">
  </Employee>
  <Employee Id="2" Name="CDE" Department="Civil" Salary="20000">
  </Employee>
  <Employee Id="3" Name="FDS" Department="Computers" Salary="15000">
  </Employee>
</Employees>

Tuesday, March 8, 2011

GridView paging, retaining the old page afetr navigating

Hi,
This can be achived by storing the page index in the session variable and reassign the page index to the grid while populating data back.
Here I have an example how we can achive this .

AspxPage:(RememberPageGrid.aspx)

<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            if (Session["PageIndex"] != null && !string.IsNullOrEmpty(Session["PageIndex"].ToString())) 
                GridView1.PageIndex = (int)Session["PageIndex"]; 
        } 
    } 
 
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
        Session["PageIndex"] = e.NewPageIndex; 
    } 
 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        Response.Redirect("OtherPage.aspx"); 
    } 
     
</script> 
<html> 
<head id="Head1" runat="server"> 
    <title>GridView example to remember page number</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:GridView ID="GridView1" runat="server" DataKeyNames="ProductID" AutoGenerateColumns="False" 
            AllowPaging="True" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging" 
            DataSourceID="sqlDataSource"> 
            <Columns> 
                <asp:BoundField ReadOnly="True" HeaderText="ProductID" DataField="ProductID" SortExpression="ProductID"> 
                </asp:BoundField> 
                <asp:BoundField HeaderText="ProductName" DataField="ProductName" SortExpression="ProductName"> 
                </asp:BoundField> 
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" /> 
            </Columns> 
        </asp:GridView> 
        <asp:SqlDataSource ID="sqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Alphabetical list of products]"> 
        </asp:SqlDataSource> 
    </div> 
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go to some other page" /> 
    </form> 
</body> 
</html>

RedirectedPage(OtherPage.aspx):

<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 
 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        Response.Redirect("RememberPageGrid.aspx"); 
    } 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:Button ID="Button1" runat="server" Text="Go back" onclick="Button1_Click"  
            style="height: 26px" /> 
    </div> 
    </form> 
</body> 
</html>

Thursday, February 24, 2011

Adding keypress event to all textboxes in asp.net using JQuery

Hi All,

Here is the quick solution to add keypress event to all textboxes in asp.net using Jquery.

How to use IsNumeric function in C#.Net

Hi all,

If we want to check a given stringg is numeric ot not we have built-in function in Vb.Net but what about in C#.Net?
I tried to build my own function in C#, here is the list of ways to achive the above functionality.
We can use any of the below method to check the given string is numeric or not.

  • Using parse method of Int:














  • Using tryparse method of Int:
  • By looping through the string arrary:
  • 
    
  • By using regular expression:








  • By using the in built IsNumeric function of visual basic:
  • 





How to Add Asp.Net Controls in XSLT ?

It is very good to know how we can add asp.net controls in XSLT.

We know by using XSLT we can transform XML document to any format like HTML, DHTML... so on.

I think most of the users are working with html controls inside the XSLT , but How to use Asp.Net Controls ?

Step 1:  Add xmlns for Asp.net

I think you kow XSLT is having stylesheet element , here we need to specify the namespace for the XSL trnsformation.
For our requirement we need to use xmlns:asp="remove"

Step 2:  Parse the asp.net Controls

You need to parse the controls in Pgae_InIt Event

Why we need to Do parsing ?

The transformed XML need to  be converted  to asp.net controls if the transformed xml is having any asp.net controls .

Note:
 Once you have transformed xml you need to replace "xmlns:asp="remove" with empty string .

Pleasee see the attached image  for your reference.











Thursday, February 17, 2011

How to find the previous page in Asp.net ?

Do you want to know what is my previous page in asp.net ?

Yes It is possible to know where your request from.

You can use Request.UrlReferrer for finding your previous page.


Now you know how to find the previous page  . happy coding :)

How to find RowIndex for gridview when textbox text is changed ?

Hi All,


If you have a textbox inside the gridview , Is it possible to find the RowIndex  with the help of ontextchanged event for textbox ?


Yes it is possible :-)


Simple steps you need to follow .


Step 1:

You need to set AutoPostBack as True for TextBox


Step 2:

You need to add ontextchnaged event for TextBox


For Example:






Step 3: 

Please add below code for you "ontextchanged" event