Friday, May 9, 2014

Grid View with Text Box

Design the form as shown below

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:TextBox ID="txtFilterby" runat="server" OnKeyUp="f1()" onfocus="SetFocusEnd(this)" Width="325px"></asp:TextBox>
<asp:UpdateProgress ID="UpdateProgress2" AssociatedUpdatePanelID="updPanelAllDocs" runat="server">
<ProgressTemplate>
<img src="images/loading.gif" alt="Loading..." height="14px" width="14" style="border: 1px solid white!important; margin-top: 4px;" />
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="updPanelAllDocs" UpdateMode="Always" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="BranchName" DataField="BranchName" ShowHeader="true" />
<asp:BoundField HeaderText="AmountRequested" DataField="AmountRequested" ShowHeader="true" />
<asp:BoundField HeaderText="FundingDate" DataField="FundingDate" ShowHeader="true" />
<asp:BoundField HeaderText="ProductCode" DataField="ProductCode" ShowHeader="true" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>

</form>

Default.aspx





Write the following JavaScript code in the Page.

<script type="text/javascript">
function f1() {

__doPostBack('<%= this.updPanelAllDocs.ClientID %>', '');
}
function SetFocusEnd(TB) {
if (TB.createTextRange) {
var FieldRange = TB.createTextRange();
FieldRange.moveStart('character', TB.value.length);
FieldRange.collapse(); FieldRange.select();
}
}
</script>


Write the following code in Default.aspx.cs






Code in Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionStrings"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from sampledata";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
ViewState["AvailableProducts"] = ds;
con.Close();
}

else
{
DataView dvData=new DataView();
DataSet ds = new DataSet();
if (ViewState["AvailableProducts"] != null)
{
if (txtFilterby.Text.Length > 0)
{
DataSet dsTemp = (DataSet)ViewState["AvailableProducts"];
if (dsTemp != null && dsTemp.Tables.Count > 0)
{
dvData = new DataView(dsTemp.Tables[0]);
dvData.RowFilter = "BranchName like '%" + txtFilterby.Text + "%'" + "or ProductCode like '%" + txtFilterby.Text + "%'";
DataTable  dtAllDocs = dvData.ToTable();
ds.Tables.Add(dtAllDocs);
ViewState["dvTableFilter"] = ds;
}
if (dvData != null && dvData.Count > 0)
{
dvData.Sort = "AmountApproved ASC";
GridView1.DataSource = dvData;
GridView1.DataBind();
}
else
{
DataTable dtTable = new DataTable();
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Documents Found";
}
}
}
}
}



No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...