Hello All, i have some challenges.
1. i want the date textbox to display calendar.I added the script on my tr2.aspx page but it's not working
2. i want the regno textbox to display auto complete.I added the script on my tr2.aspxpage it's not working as well
how do i make the calendar display in the date text box and autocomplete display in the regno text box
please find my code below
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="CustomerName" HeaderText="CustomerName" />
<asp:BoundField DataField="CustomerEmail" HeaderText="CustomerEmail" />
<asp:BoundField DataField="CustomerCity" HeaderText="CustomerCity" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" Text="Update" runat="server" OnClick="OnUpdate" />
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnUpdate(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4]
{
new DataColumn("id"),
new DataColumn("CustomerName"),
new DataColumn("CustomerEmail"),
new DataColumn("CustomerCity")
});
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string id = row.Cells[0].Text;
string CustomerName = row.Cells[1].Text;
string CustomerEmail = row.Cells[2].Text;
string CustomerCity = row.Cells[3].Text;
dt.Rows.Add(id, CustomerName, CustomerEmail, CustomerCity);
}
}
Session["Data"] = dt;
Response.Redirect("TR2.aspx");
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT id,CustomerName,CustomerEmail,CustomerCity FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script>
$(function () {
$("#txtdate").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'yy/mm/dd'
});
});
</script>
<link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "jQueryAutoCompleteTextbox.aspx/GetAutoCompleteData",
data: "{'regno':'" + document.getElementById('txtSearch').value + "'}",
dataType: "json",
success: function (data) {
if (data.d.length > 0) {
response($.map(data.d, function (item) {
return {
label: item.split('/')[0],
val: item.split('/')[1]
}
}));
}
else {
response([{ label: 'No Records Found', val: -1 }]);
}
},
error: function (result) {
alert("Error");
}
});
},
select: function (event, ui) {
if (ui.item.val == -1) {
return false;
}
$('#lblUserId').text(ui.item.val);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="CustomerName" HeaderText="CustomerName" />
<asp:BoundField DataField="CustomerEmail" HeaderText="CustomerEmail" />
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox ID="txtdate" runat="server" Width="180"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtquantity" runat="server" Width="180"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Baseno">
<ItemTemplate>
<asp:TextBox ID="txtSearch" class="autosuggest" runat="server" Width="180"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" Text="Update" runat="server" OnClick="OnUpdate" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["Data"] != null)
{
DataTable dt = Session["Data"] as DataTable;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
public static List<string> GetAutoCompleteData(string regno)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=External\\PRESS01;Integrated Security=true;Initial Catalog=Customer;"))
{
using (SqlCommand cmd = new SqlCommand("select id,regno from Account where regno LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", regno);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(string.Format("{0}/{1}", dr["regno"], dr["id"]));
}
return result;
}
}
}
protected void OnUpdate(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string id = row.Cells[0].Text;
string Datea = (row.FindControl("txtdate") as TextBox).Text.Trim();
string quantity = (row.FindControl("txtquantity") as TextBox).Text.Trim();
string regno = (row.FindControl("txtsearch") as TextBox).Text.Trim();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "UPDATE Customers SET Datea = @Datea, quantity = @quantity, regno = @regno WHERE id = @id";
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@Datea", Datea);
cmd.Parameters.AddWithValue("@Quantity", quantity);
cmd.Parameters.AddWithValue("@Regno", regno);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}