C# gridview的问题
我创建了一个带有过滤器的gridview,一切都很好,但是当我使用gridview.databind()时;没有发生任何事情,随着我更改页面索引,也没有发生任何事情 我使用了scriptmanger、updatepanel和一些javascript标记代码:C# gridview的问题,c#,asp.net,gridview,datatable,datasource,C#,Asp.net,Gridview,Datatable,Datasource,我创建了一个带有过滤器的gridview,一切都很好,但是当我使用gridview.databind()时;没有发生任何事情,随着我更改页面索引,也没有发生任何事情 我使用了scriptmanger、updatepanel和一些javascript标记代码: var gridViewCtlId=''; var gridViewCtl=null; var curSelRow=null; 功能更改(evt){ getControlData(evt.target.id,document.getE
var gridViewCtlId='';
var gridViewCtl=null;
var curSelRow=null;
功能更改(evt){
getControlData(evt.target.id,document.getElementById(evt.target.id).value);
}
函数getGridViewControl(){
如果(null==gridViewCtl){
gridViewCtl=document.getElementById(gridViewCtlId);
}
}
函数onGridViewRowSelected(rowIdx,isfiltered){
var selRow;
//过滤一
如果(rowIdx=='1',isfiltered=='True')
selRow=null;
其他的
selRow=getSelectedRow(rowIdx);
if(curSelRow!=null){
curSelRow.style.backgroundColor='#ffffff';
}
if(selRow!=null){
curSelRow=selRow;
curSelRow.style.backgroundColor='#8098B3';
}
}
函数getSelectedRow(rowIdx){
getGridViewControl();
getRowSelected(rowIdx-1);
如果(null!=gridViewCtl){
返回gridViewCtl.rows[rowIdx];
}
返回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)
{
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统数据;
公共部分类\u默认值:System.Web.UI.Page
{
静态字符串controlID、controlValue、selectedRowIndex;
#区域网络方法
[System.Web.Services.WebMethod]
公共静态无效getControlData(字符串controlIDP、字符串controlValueP)
{
controlID=controlIDP;
controlValue=controlValueP;
_默认d=新的_Default();
d、 textboxs_TextChanged();
}
[System.Web.Services.WebMethod]
公共静态无效getRowSelected(字符串rowIndex)
{
selectedRowIndex=rowIndex;
}
#端区
私有void SetInitialRow(GridView inputGridView,SqlDataSource inputSqlDataSource)
{
如果(inputGridView!=null)
{
DataView dv=(DataView)inputSqlDataSource.Select(DataSourceSelectArguments.Empty);
对象[]会话=新对象[2];
会话[0]=dv;
会话[1]=inputGridView;
会话[“GridView”]=会话;
TextBox[]tb=newtextbox[inputGridView.Columns.Count];
DataTable dt=dv.Table.Clone();
添加(dt.NewRow());
for(int row=0;row
在DataBind()行的代码中设置断点。当它到达行时,检查绑定的DataTable并确保它包含信息。您遇到的症状表明,筛选器未导致任何数据传送到网格。如果我是对的,你什么也看不到也不奇怪。如果您在绑定之前有数据,我会感到惊讶
下一步是确定最初是否有数据
我可以想到的另一个选择是,您正在进行初始绑定,但在值发生更改(重新提交等)后不会重新绑定。如果是这种情况,您需要设置一个“缓存”来保存表,然后根据需要进行过滤。或者返回数据库。我不确定是哪个DataBind()导致了问题 执行时:
protected void Page_Load(object sender, EventArgs e)
{
SetInitialRow(GridView1, SqlDataSource1);
}
您通过值传递GridView1,因此当您在SetInitialValue函数中绑定到它时,绑定到副本,而不是WebForm上的gridview也是如此。您可以尝试通过引用传递GridView1。这同样适用于SqlDataSource1
编辑
有关c#中ref关键字的更多信息,请参阅。如果通过引用传递,您的代码将如下所示:
protected void Page_Load(object sender, EventArgs e)
{
SetInitialRow(ref GridView1, ref SqlDataSource1);
}
及
我确实根据需要对datatable和filter进行了说明,并在筛选后检查了数据,发现
protected void Page_Load(object sender, EventArgs e)
{
SetInitialRow(ref GridView1, ref SqlDataSource1);
}
private void SetInitialRow(ref GridView inputGridView,ref SqlDataSource inputSqlDataSource)
{