This DataList shows on page so that the user can edit the records, but now want the user to only see and edit the records when the user selects the location dropdown and enters Item ID, so if the dropdown location selected is Store A and the item ID inserted is 1 then the record of that ID and location will show.
Example (Invisible on page)
Datalist
ID Store Item
------------------
1 StoreA Cup
---------------------
2 StoreB Belt
When the user selects the location and inserts the Item ID then the Datalist will show the record
HTML
<div class="form-group-inner">
<div class="row">
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
<div class="input-group mg-b-pro-edt">
<asp:DropDownList runat="server" ID="Department" CssClass="btn btn-custon-four btn-default dropdown-toggle" AppendDataBoundItems="true">
<asp:ListItem>StoreA</asp:ListItem>
<asp:ListItem>StoreB</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblrecipt" runat="server"></asp:Label>
</div>
</div>
</div>
</div>
<asp:TextBox ID="txtid" runat="server" CssClass="form-control" placeholder="Enter Item ID"></asp:TextBox>
<asp:LinkButton ID="btnsearch" runat="server" CssClass="btn btn-default">Search</asp:LinkButton>
<asp:DataList ID="dlCustomers" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th>Item</th>
<th>Qty</th>
<th>Qty_Remaning</th>
<th>Damages</th>
<th></th>
<th></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblId" Text='<%#Eval("ID") %>' runat="server" /></td>
<td><asp:Label ID="lblitem" Text='<%#Eval("Item") %>' runat="server" /></td>
<td><asp:Label ID="lblqty" Text='<%#Eval("Qty") %>' runat="server" /></td>
<td><asp:Label ID="lblqtyremaning" runat="server" Text='<%# Bind("Qty_Remaining") %>'></asp:Label></td>
<td><asp:Label ID="lbldamages" Text='<%#Eval("Damages") %>' runat="server" /></td>
<td><asp:LinkButton ID="lnkEdit" runat="server" OnClick="OnEdit" CssClass="btn btn-primary">
<asp:Label ID="Label4" runat="server" Text="" CssClass="fa fa-edit"></asp:Label>
Edit
</asp:LinkButton>
</td>
<td><asp:LinkButton ID="lnkDelete" runat="server" OnClick="OnDelete" CssClass="btn btn-danger"
OnClientClick="return confirm('Are you sure?')">
<asp:Label ID="Label5" runat="server" Text="" CssClass=" glyphicon glyphicon-trash"></asp:Label>
Delete
</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td><asp:Label ID="lblId" Text='<%#Eval("ID") %>' runat="server" /></td>
<td><asp:TextBox ID="txtitem" runat="server" Width="120" Text='<%# Eval("Item") %>' CssClass="form-control" /></td>
<td><asp:TextBox ID="txtqty" runat="server" Width="120" Text='<%# Eval("Qty") %>' CssClass="form-control" /></td>
<td><asp:TextBox ID="txtqtyremaning" runat="server" Width="120" Text='<%# Eval("Qty_Remaining") %>' CssClass="form-control" /></td>
<td><asp:TextBox ID="txtdamages" runat="server" Width="120" Text='<%# Eval("Damages") %>' CssClass="form-control" /></td>
<td><asp:LinkButton ID="lnkUpdate" runat="server" OnClick="OnUpdate" CssClass="btn btn-info">
<asp:Label ID="Label6" runat="server" Text="" CssClass="glyphicon glyphicon-check"></asp:Label>
Update
</asp:LinkButton>
</td>
<td><asp:LinkButton ID="lnkCancel" runat="server" OnClick="OnCancel" CssClass="btn btn-warning">
<asp:Label ID="Label7" runat="server" Text="" CssClass=" glyphicon glyphicon-ban-circle"></asp:Label>
Cancel
</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDatalist();
}
}
protected void OnDelete(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
int id = Convert.ToInt32((item.FindControl("lblId") as Label).Text.Trim());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "DELETE FROM Store_Table WHERE ID = @ID";
cmd.Parameters.AddWithValue("@ID", id);
InsertUpdateDelete(cmd);
this.BindDatalist();
}
protected void OnEdit(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
dlCustomers.EditItemIndex = item.ItemIndex;
this.BindDatalist();
}
protected void OnUpdate(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
int id = Convert.ToInt32((item.FindControl("lblId") as Label).Text.Trim());
// string receipt = (item.FindControl("txtreceipt") as TextBox).Text;
string items = (item.FindControl("txtitem") as TextBox).Text;
string qty = (item.FindControl("txtqty") as TextBox).Text;
string qtyremaing = (item.FindControl("txtqtyremaning") as TextBox).Text;
// string dsc = (item.FindControl("txtdsc") as TextBox).Text;
string damages = (item.FindControl("txtdamages") as TextBox).Text;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE Store_Table SET Item=@Item,Qty=@Qty,Qty_Remaining=@Qty_Remaining,Damages=@Damages WHERE ID = @ID";
cmd.Parameters.AddWithValue("@ID", id);
cmd.Parameters.AddWithValue("@Qty_Remaining", qtyremaing);
cmd.Parameters.AddWithValue("@Qty", qty);
cmd.Parameters.AddWithValue("@Item", items);
// cmd.Parameters.AddWithValue("@Store", to);
cmd.Parameters.AddWithValue("@Damages", damages);
InsertUpdateDelete(cmd);
dlCustomers.EditItemIndex = -1;
this.BindDatalist();
}
protected void OnCancel(object sender, EventArgs e)
{
dlCustomers.EditItemIndex = -1;
this.BindDatalist();
}
private void BindDatalist()
{
string conString = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
string query = "SELECT ID,Item,Qty,Qty_Remaining,Damages FROM Store_Table ORDER WHERE Store=@Store BY ID DESC";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@Store", Department.SelectedItem.Text);
cmd.Parameters.AddWithValue("@ID", txtid.Text);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dlCustomers.DataSource = dt;
dlCustomers.DataBind();
//GridView1.DataSource = dt;
// GridView1.DataBind();
}
}
}
}
private void InsertUpdateDelete(SqlCommand cmd)
{
string conString = ConfigurationManager.ConnectionStrings[""].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}