gridview的问题

时间:2011-01-18 16:29:29

标签: c# asp.net gridview datatable datasource

我用过滤器创建了一个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)
    {

    }
}

2 个答案:

答案 0 :(得分:0)

在DataBind()行的代码中设置断点。当它到达该行时,检查绑定的DataTable并确保它包含信息。您遇到的症状表明过滤器没有导致数据传递到网格。如果我是对的,你什么也看不出来并不令人惊讶。如果你在绑定之前有数据,我会感到惊讶。

下一步是确定您最初是否有数据。

我能想到的另一个选择是你正在进行初始绑定,但是在更改值后重新绑定(重新提交等)。如果是这种情况,您需要设置一个“缓存”来保存表,然后根据需要进行过滤。无论是那个还是回程数据库。

答案 1 :(得分:0)

我不确定哪个DataBind()会导致您出现问题。

执行时:

    protected void Page_Load(object sender, EventArgs e)     
{
         SetInitialRow(GridView1, SqlDataSource1);       
} 

您正在通过值传递GridView1,因此当您在SetInitialValue函数中绑定它时,绑定到副本,而不是WebForm上的gridview。您可以尝试通过引用传递GridView1。这同样适用于SqlDataSource1。

修改 有关c#中ref关键字的更多信息,请参阅ref (C# Reference)。如果通过引用传递,您的代码将类似于:

   protected void Page_Load(object sender, EventArgs e)     
{
         SetInitialRow(ref GridView1, ref SqlDataSource1);       
} 

private void SetInitialRow(ref GridView inputGridView,ref SqlDataSource inputSqlDataSource)     
{