Take parameter from listview - asp.net

i have this code:
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection sc = new System.Data.SqlClient.SqlConnection(GetConnectionString());
{
System.Data.SqlClient.SqlCommand cmd;
sc.Open();
cmd = new System.Data.SqlClient.SqlCommand("SET IDENTITY_INSERT Zapas OFF INSERT INTO Zapas (Zapas.Sezona,.....)SELECT Zapas.Sezona,... FROM Zapas WHERE ID_zapas=#ID;", sc);
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("#ID", System.Data.SqlDbType.Text)).Value = (TextBox)ListView1.FindControl("box1");
cmd.ExecuteNonQuery();
sc.Close();
Response.Redirect("~/Zapasy_seznam.aspx");
}
}
I need take value ID from listview, but with this code I have this error:
...expects the parameter '#ID', which was not supplied....
This part of my listview...
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID_zapas" DataSourceID="SqlDataSource1" style="text-align: center ">
<AlternatingItemTemplate>
<tr style="background-color: #e9ffe9;color: #284775;text-align:center">
<td>
<asp:TextBox ID="box1" runat="server" Text='<%# Eval("ID_zapas") %>' Visible="false" />
...
<td style="width:50px;background-color:white">
<asp:LinkButton ID="Button1" runat="server" OnClick="Button1_Click" Visible='<%# HttpContext.Current.User.IsInRole("admin") %>' CausesValidation="False" OnClientClick="javascript: return confirm('Opravdu chcete zápas zkopírovat?');">
<asp:Image ID="Image2" runat="server" ImageUrl="~/Icons/copy.png" Width="29px" Height="29px" ToolTip="Zkopírovat zápas" />
</asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
Have you some idea?

As per comments, please try this:
using System.Data.SqlClient;
protected void Button1_Click(object sender, EventArgs e)
{
var box1 = (TextBox)((LinkButton)sender).Parent.FindControl("box1");
using (var sc = new SqlConnection(GetConnectionString()))
{
using (var cmd = sc.CreateCommand())
{
sc.Open();
cmd.CommandText = "SET IDENTITY_INSERT Zapas OFF INSERT INTO Zapas (Zapas.Sezona,.....)SELECT Zapas.Sezona,... FROM Zapas WHERE ID_zapas=#ID;";
cmd.Parameters.AddWithValue("#ID", box1.Text);
cmd.ExecuteNonQuery();
sc.Close();
Response.Redirect("~/Zapasy_seznam.aspx");
}
}
}
When using data-aware controls such as a ListView here, the controls are created with automatic unique IDs per data item. This means that you can have more than 1 of the same control (such as "box1" in this case) within the page that should be referenced by the data item (((LinkButton)sender).Parent which is ListViewDataItem, representing the row).
ListViewDataItem.FindControl will find controls of a specific server ID within its own child scope, allowing you to get values for controls within the same row of the button that is being clicked.

Related

Create a dropdown that filters the output of my repeater

I have a repeater that lists all the products in my Products table and I need a dropdown box to filter the repeater content by category_id. I have absolutely no idea what I'm doing. Here is my repeater:
<asp:Repeater ID="RShopItems" runat="server" ItemType="WebDevAssessment.Models.Product">
<ItemTemplate>
<div class="col-md-4 productOuterContainer">
<div class="col-md-10 col-md-offset-1 productInnerContainer border">
<a href='<%#"ProductDetails.aspx?item=" + Item.product_id %>' runat="server" target="_parent">
<img runat="server" alt='<%# Item.product_name %>' src='<%# Item.product_image1 %>'
style="width: 100%" class="img-thumbnail" />
</a>
<h4 class="text-center"><%# Item.product_name %></h4>
<p class="text-center">Size: <%#Item.product_size%> - $<%#Item.product_price%></p>
<p class="features_text text-center"><%#Item.product_feat_short%></p>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
And the behind code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebDevAssessment.Models;
namespace WebDevAssessment
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (WebDatabaseEntities wde = new WebDatabaseEntities())
{
var products = (from si in wde.Products
orderby si.product_price descending
select si).ToList();
// assign the data to the repeater
RShopItems.DataSource = products;
// trigger the repeater to incorporate to display the data
RShopItems.DataBind();
}
}
}
}
Ok, you can use this apporach.
In this example, we have a repeater - showing hotel names.
We have a drop down list to filter by city (no choice = all cities).
So, the mark up looks like this:
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="City" DataValueField="ID" AutoPostBack="True">
</asp:DropDownList>
<br />
<br />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div style="border-style:solid;color:black;width:250px;float:left">
<div style="padding:5px;text-align:right">
Hotel Name: <asp:TextBox ID="txtHotelName" runat="server" Text ='<%# Eval("HotelName") %>' Width="130px" />
<br />
First Name: <asp:TextBox ID="txtFirst" runat="server" Text ='<%# Eval("FirstName") %>' Width="130px" />
<br />
Last Name: <asp:TextBox ID="txtLast" runat="server" Text ='<%# Eval("LastName") %>' Width="130px" />
<br />
City: <asp:TextBox ID="txtCity" runat="server" Text ='<%# Eval("City") %>' Width="130px" />
<br />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
And the code to load up drop, load up Repeater, and filter looks like this:
protected void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack == false)
{
LoadDropDown();
LoadGrid();
}
}
public void LoadDropDown()
{
// load drop down list
using (SqlCommand cmdSQL = new SqlCommand("SELECT ID, City from tblCity ORDER BY City",
new SqlConnection(My.Settings.TEST3)))
{
cmdSQL.Connection.Open();
DropDownList1.DataSource = cmdSQL.ExecuteReader;
DropDownList1.DataBind();
// add blank row choice to drop down list
DropDownList1.Items.Insert(0, new ListItem(string.Empty, string.Empty));
}
}
public void LoadGrid(string sFilter = "")
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * FROM tblHotels",
new SqlConnection(My.Settings.TEST3)))
{
if (sFilter != "")
{
cmdSQL.CommandText += " WHERE City = #City";
cmdSQL.Parameters.Add("#City", SqlDbType.NVarChar).Value = DropDownList1.SelectedItem.Text;
}
cmdSQL.CommandText += " ORDER BY HotelName";
cmdSQL.Connection.Open();
Repeater1.DataSource = cmdSQL.ExecuteReader;
Repeater1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
LoadGrid(DropDownList1.SelectedItem.Text);
}
So it now looks like this:
So, on first load - you load up the dropdown, and the repeater.
Dropdown list has auto post back = true, and it simply filters the Repeater by applying a parameters (optional) to the data source for the repeater.
It not a lot of code, and I don't see any advantages to using linq as you are.

When i click button in gridview it does't work

I am a beginner of Asp.net, making l small project but having big problems, :) ...
Right now I have a situation, I used gridview to retrieve data from Access database and I done that, I added buttons in grid view. When I click on button the following error comes
Server Error in '/E Shop' Application. Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page
EnableEventValidation="true" %> in a page. For security purposes,
this feature verifies that arguments to postback or callback events
originate from the server control that originally rendered them. If
the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width= "100%" CellPadding="3">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table class="style17" width="100%" border=" 0">
<tr>
<td height="100%" width="25%">
<asp:Image ID="Image6" runat="server" Height="144px"
ImageUrl='<%# "data:image/jpg;base64, " + Convert.ToBase64String((byte[]) Eval("Picture")) %>'
Width="158px" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px" />
</td>
<td align="center" height="100%" width="75%">
<table align="right" class="style17">
<tr>
<td align="left">
<asp:Label ID="Label7" runat="server" style="font-size: 15pt; color: #0000FF"
Text='<%# Bind("Title") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Brand") %>'
CssClass="style18" Font-Size="15pt"></asp:Label>
</td>
<td align="right">
<asp:ImageButton ID="ImageButton1" runat="server" Height="51px"
ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px"
onclick="ImageButton1_Click" />
</td>
</tr>
<tr>
<td align="left">
<strong>Rs:</strong><asp:Label ID="Label2" runat="server"
Text='<%# Bind("Price") %>' CssClass="style18" Font-Size="15pt"></asp:Label>
</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Color") %>'
CssClass="style18" Font-Size="15pt"></asp:Label>
</td>
<td align="right">
<asp:ImageButton ID="ImageButton2" runat="server" Height="51px"
ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px"
onclick="ImageButton2_Click" />
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Condition") %>'
CssClass="style18" Font-Size="15pt"></asp:Label>
</td>
<td>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Material") %>'
CssClass="style18" Font-Size="15pt"></asp:Label>
</td>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<table class="style17">
<tr>
<td align="left" height="100%" width="30%">
<asp:Image ID="Image5" runat="server" Height="144px"
ImageUrl='<%# "data:image/jpg;base64, " + Convert.ToBase64String((byte[]) Eval("Picture")) %>'
Width="193px" />
</td>
<td>
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
c#
public partial class Shirts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("~\\App_Data\\Products.mdb"));
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from Shirts";
cmd.Connection = con;
OleDbDataAdapter Adaptor = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
Adaptor.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
// Session["Title"] = "Label7.Text";
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
// Response.Redirect("Cart.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
// Response.Redirect("Cart.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Cart.aspx");
}
}
Please Help me to make the buttons work
Thanks in Advance
You can't create an event handler for controls present in Gridview control like this, because they are not static and they repeat for each item coming from the datasource for the Gridview.
When a button is clicked in Gridview, RowCommand event is raised, so you need to write your logic in this event by identifying your control, something like this:-
To identify each control individually in Gridview RowCommand event, you need to add CommandName & CommandArgument properties to your control:-
<asp:ImageButton ID="ImageButton1" runat="server" Height="51px" ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px" OnCommand="Foo" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
Here, CommandName is used to identify the control which raised the event and CommandArgument is used to identify the current row.
Finally, you can read them in code behind like this:-
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Foo")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
// Add you logic here
}
}
I follow the Example you provide, i set commandname and commandargument properties and in Rowcommand event i try to same steps like example but again i got that Error
Server Error in '/E Shop' Application.
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Source:
<asp:ImageButton ID="ImageButton1" runat="server" Height="51px"
commandname="Foo" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
ImageUrl="~/Images/orange_addtocart-trans.png" Width="159px"
onclick="ImageButton1_Click2"/>
C#
public partial class raff : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("~\\App_Data\\Products.mdb"));
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select * from Shirts";
cmd.Connection = con;
OleDbDataAdapter Adaptor = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
Adaptor.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
Response.Redirect("Cart.aspx");
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
// Session["Title"] = "Label7.Text";
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
// Response.Redirect("Cart.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
// Response.Redirect("Cart.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
// Response.Redirect("Cart.aspx");
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Redirect("Cart.aspx");
}

select index of listView is not working in ASP.NET

I am new to ASP.NET, I am making a search box in my application.
For example: if a user enters "abc" in the textbox, then the textbox will fetch data from the database which starts with "abc". I am passing this data to DataTable.
It works properly,
Here is my code snippet:
DataTable result = new DataTable();
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);
SqlCommand cmd = new SqlCommand(query, conn);
result.Load(cmd.ExecuteReader());
conn.Close();
lvwItems.DataSource = result;
lvwItems.DataBind();
}
Now I want to retrieve all this data in my <div> tag. so i tried using asp:ListView,
here is my code snippet,
it works properly, but now i want to navigate to new page when user select any row of listView, but i am unable to select any row..
<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
</div>
</ItemTemplate>
Thanks In Advance !!
Any help will be appreciated.
EDIT:(SearchBox.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class SearchBox : System.Web.UI.Page
{
string connString;
DataTable result = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{ }
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);
SqlCommand cmd = new SqlCommand(query, conn);
result.Load(cmd.ExecuteReader());
conn.Close();
lvwItems.DataSource = result;
lvwItems.DataBind();
}
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("lablId");
if (String.IsNullOrEmpty(lablId.Text))
{
Response.Redirect("NextPage.aspx?id=" + lablId.Text, false);
}
}
(SearchBox.aspx)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="SearchBox.aspx.cs" Inherits="SearchBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TextBox ID="TextBox1" runat="server" Height="30px" Width="179px"
OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Go"
Width="62px" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
<asp:Label ID="lablId" visible="false" runat="server" Text='<%#Eval("Scrip") %>'/>
</div>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:krunal_DBConnectionString2 %>"
SelectCommand="SELECT * FROM [SearchBoxData]"></asp:SqlDataSource>
</form>
</body>
</html>
You have to add a SELECT button in the ItemTemplate, see the complete working code.
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<%# Eval("Scrip")%>
<asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" />
</ItemTemplate>
</asp:ListView>
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("CONTROL_ID");
}
Thanks
Deepu
Probably you will have to add the event for Selected index change:
<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging">
In code behind you can get the selected row item like below.
Also you can place a label or hidden field so that you can get some data from those control and pass it in to the next page.. (might be id or something).
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
<asp:Label ID="lablId" visible="flase" runat="server" Text='<%#Eval("Id") %>' />
</div>
</ItemTemplate>
</asp:ListView>
void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("lablId");
if (String.IsNullOrEmpty(lablId.Text))
{
Response.Redirect("page.aspx?id="+lablId.Text,false);
}
}
Thanks
Deepu
Here you go,
Also remove the OnClick="callMethod" from the button. Since you have SelectedIndex method no need to have the onClick method.
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Button btn = (Button)item.FindControl("btn1");
if(btn != null)
{
var buttonText = btn.Text;
}
}
Hope this helps
Thanks
Deepu

create a filter row with textboxes on each column in asp.net gridview

I have a asp.net gridview, which i am binding at runtime with a custom List object. I want to add a filter row below the header row on each column and on click of filter button grid data should get filtered based on values written in the filter textboxes. requirement seems weird but this is what client wants. please help with some clue.
aspx code :
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />
<HeaderTemplate>
<table>
<tr>
<td align="center">
<asp:ImageButton runat="server" ID="imgFilter1" ImageUrl="../Images/filter.png" Style="height: 20px;
width: 20px;" OnClick="imgFilter1_click" />
</td>
<td align="center">
<asp:TextBox runat="server" ID="gridTextboxFilter1" AutoPostBack="true" onTextChanged="gridTextboxFilter1_text_changed">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="center" colspan="2">
//your column header
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("your_dataFeild") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
cs code :
private void BindGrid(string strFilter)
{
try
{
// Simple created a table to bind with Grid view and
// populated it with data.
DataTable dt = new DataTable("sample");
dt.Columns.Add("ID");
dt.Columns.Add("Name");
DataRow dr ;
for(int counter=1;counter<11;counter++)
{
dr = dt.NewRow();
dr["ID"]=counter.ToString();
dr["Name"]= "Cat" + counter.ToString();
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
if(strFilter != "")
dv.RowFilter="Name like '%" + strFilter + "%'";
if (CategoryFilter == "")
gvCategory.DataSource = dv;
else
gvCategory.DataSource = dv;
gvCategory.DataBind();
}
catch (Exception ex)
{
}
finally
{
}
}
protected void gridTextboxFilter1_text_changed(object sender, EventArgs e)
{
string text = ((TextBox)sender).Text;
BindGrid(text);
}
Add a textbox and button on the header template.
Write a query on button press and get the value.
The query something like select * from tbl where col like '%val%'
Bind the value to gridView.
I think this will solves for you

Working with Datalist in asp.net?

good morning to all.
i place a datalist in my project in that i place a link button when i click on that link button a panel will open in that row with a textbox and a button. it is working fine but my problem is if i click on one link button of a row panel will open, when i click on second row link button the sencond row panel is opening but first row panel is not closing. I think you get my point owhter wise i will explain again this is my code check out
<form id="form1" runat="server">
<div>
<asp:DataList ID="Mydatalist" runat ="Server"
OnItemCommand="Mydatalist_ItemCommand" >
<ItemTemplate >
<table >
<tr>
<td>
<asp:Label ID="lblcouname" runat ="server"
Text ='<%# Eval("country_name") %>'></asp:Label>
</td>
<td>
<asp:LinkButton ID="lnkrepl" Text="reply"
CommandName ="reply" runat ="server"
CommandArgument ='<%# Eval("country_id") %>'>
</asp:LinkButton>
</td>
</tr>
</table>
<div>
<asp:Panel ID="mypane" runat ="Server" Visible ="false" >
<asp:TextBox ID="txtpane" runat ="Server" ></asp:TextBox><br />
<asp:Button ID="btnInsert" runat="Server" Text ="Insert" />
</asp:Panel>
</div>
</ItemTemplate>
</asp:DataList>
</div>
</form>
Code behind:
public partial class Datlist : System.Web.UI.Page
{
SqlConnection con; SqlDataAdapter da; DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(
#"server=msmsm;database=pop;user id=sa;password=abc");
con.Open();
if (!IsPostBack)
{
getCountry();
}
}
public void getCountry()
{
string sqr="select * from country";
da=new SqlDataAdapter (sqr,con);
ds = new DataSet();
da.Fill(ds,"country");
Mydatalist.DataSource = ds.Tables[0];
Mydatalist.DataBind();
}
protected void Mydatalist_ItemCommand(object source, DataListCommandEventArgs e)
{
Panel pn = (Panel)e.Item.FindControl("mypane");
pn.Visible = false;
if (e.CommandName == "reply")
{
pn.Visible = true;
}
}
}
You have to programmably hide it. THe DataList should have an items property, and as such you can loop through all items, find the panel control using FIndControl, and set its visibility to false.
EDIT: So you need to do:
private void HideItems()
{
foreach (var item in this.dl.Items)
{
var panel = item.FindControl("mypane") as Panel;
if (panel != null)
panel.Visible = false;
}
}
In ItemCommand, call this method to hide all the existing control's panels.

Resources