Я создал gridview с фильтром и все хорошо, но когда я делаю gridview.databind ();ничего не происходит, и с изменением индекса страницы тоже ничего не происходит.
Я использовал scriptmanger, updatepanel и немного кода разметки JavaScript:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" ClientIDMode="Static" runat="server" AllowPaging="True" AutoGenerateColumns="False"
onload="GridView1_Load" OnRowCreated="OnRowCreated" Width="614px" CellPadding="4" ForeColor="#333333"
GridLines="Both" EmptyDataText="No Data To Show">
<Columns>
<asp:BoundField DataField="areacode" HeaderText="areacode"
SortExpression="areacode" />
<asp:BoundField DataField="areaename" HeaderText="areaename"
SortExpression="areaename" />
<asp:BoundField DataField="areaaname" HeaderText="areaaname"
SortExpression="areaaname" />
<asp:BoundField DataField="areazipcode" HeaderText="areazipcode"
SortExpression="areazipcode" />
<asp:BoundField DataField="districtcode" HeaderText="districtcode"
SortExpression="districtcode" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#ffffff" ForeColor="#333333" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NetCareConnectionString %>"
SelectCommand="SELECT * FROM [areadata]"></asp:SqlDataSource>
<script language="javascript" type="text/javascript">
var gridViewCtlId = '<%=GridView1.ClientID%>';
var gridViewCtl = null;
var curSelRow = null;
function onchange(evt) {
PageMethods.getControlData(evt.target.id, document.getElementById(evt.target.id).value);
}
function getGridViewControl() {
if (null == gridViewCtl) {
gridViewCtl = document.getElementById(gridViewCtlId);
}
}
function onGridViewRowSelected(rowIdx, isfiltered) {
var selRow;
//filter one
if (rowIdx == '1' & isfiltered == 'True')
selRow = null;
else
selRow = getSelectedRow(rowIdx);
if (curSelRow != null) {
curSelRow.style.backgroundColor = '#ffffff';
}
if (selRow != null) {
curSelRow = selRow;
curSelRow.style.backgroundColor = '#8098B3';
}
}
function getSelectedRow(rowIdx) {
getGridViewControl();
PageMethods.getRowSelected(rowIdx - 1);
if (null != gridViewCtl) {
return gridViewCtl.rows[rowIdx];
}
return null;
}
код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
static string controlID, controlValue, selectedRowIndex;
#region WebMethods
[System.Web.Services.WebMethod]
public static void getControlData(string controlIDP, string controlValueP)
{
controlID = controlIDP;
controlValue = controlValueP;
_Default d = new _Default();
d.textboxes_TextChanged();
}
[System.Web.Services.WebMethod]
public static void getRowSelected(string rowIndex)
{
selectedRowIndex = rowIndex;
}
#endregion
private void SetInitialRow(GridView inputGridView,SqlDataSource inputSqlDataSource)
{
if (inputGridView != null)
{
DataView dv = (DataView)inputSqlDataSource.Select(DataSourceSelectArguments.Empty);
object [] session=new object[2];
session[0]=dv;
session[1] = inputGridView;
Session["GridView"] = session;
TextBox[] tb = new TextBox[inputGridView.Columns.Count];
DataTable dt = dv.Table.Clone();
dt.Rows.Add(dt.NewRow());
for (int row = 0; row < dv.Table.Rows.Count; row++)
{
DataRow newRow = dt.NewRow();
newRow = dv.Table.Rows[row];
dt.ImportRow(newRow);
}
inputGridView.DataSource = dt;
inputGridView.DataBind();
for (int i = 0; i < inputGridView.Columns.Count; i++)
{
tb[i] = new TextBox();
tb[i].Attributes.Add("id", i.ToString());
if (!inputGridView.Rows[0].Cells[i].HasControls())
inputGridView.Rows[0].Cells[i].Controls.Add(tb[i]);
else
inputGridView.Rows[0].Cells[i].Controls.Clear();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
SetInitialRow(GridView1, SqlDataSource1);
}
public void textboxes_TextChanged()
{
if (!string.IsNullOrWhiteSpace(controlValue))
{
object[] session = (object[])Session["GridView"];
DataView dv = (DataView)session[0];
GridView1 = (GridView)session[1];
String strexpr = string.Format("{0}={1}", dv.Table.Columns[int.Parse(controlID)], controlValue);
DataRow[] rows = dv.Table.Select(strexpr);
DataTable dt = dv.Table.Clone();
foreach (DataRow row in rows)
dt.ImportRow(row);
System.Windows.Forms.MessageBox.Show(dt.Rows.Count.ToString());
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + (e.Row.RowIndex + 1).ToString() + "','" + true + "')");
}
}
protected void GridView1_Load(object sender, EventArgs e)
{
}
}