ASPxGridView1.Selection总是= 0并且ASPxGridView1.GetSelectedFieldValues不起作用

时间:2012-12-13 03:08:17

标签: asp.net gridview devexpress aspxgridview

我只是想从devexpress gridview中获取选定的行,但它对我来说无效,我尝试了客户端和自定义回调,并尝试了服务器端,但它没有工作,花了10个小时搜索并尝试了每一个我得到的解决方案,但没有对我有用,有人可以给我一个提示,我缺少什么?,这是代码:

的Default.aspx:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>

        <dx:ASPxGridView ID="ASPxGridView1" ClientInstanceName="ASPxGridView1"  KeyFieldName="ID" AutoGenerateColumns="False" runat="server">
            <Columns>
                <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" 
                    Caption="Select" Name="Select">
                </dx:GridViewCommandColumn>
                <dx:GridViewDataTextColumn Caption="ID" FieldName="pag_id_pk" Name="ID" 
                    VisibleIndex="1">
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn Caption="Name" FieldName="pag_name" Name="Name" 
                    VisibleIndex="2">
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn Caption="Title" FieldName="pag_title" Name="Title" 
                    VisibleIndex="3">
                </dx:GridViewDataTextColumn>
            </Columns>
        </dx:ASPxGridView>
        <dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="Get Values" UseSubmitBehavior="False" onclick="ASPxButton1_Click" />
    </div>
</asp:Content>

default.cs:

public partial class _Default : System.Web.UI.Page
{
    DataTable table = null;
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack && !IsCallback)
        {
            string connectionString = ...
            con.Open();
            SqlDataReader rd;
            SqlCommand cmd = new SqlCommand("SELECT * FROM [Pages]", con);
            rd = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(rd);

            ASPxGridView1.DataSource = dt;

            Session["dt"] = (DataTable)dt;
            ASPxGridView1.DataBind();
            rd.Close();
            con.Close();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        List<object> indexes = new List<object>();
        for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
        {
            if (ASPxGridView1.Selection.IsRowSelected(i))
            {
                indexes.Add(ASPxGridView1.GetRowValues(i, ASPxGridView1.KeyFieldName));
            }
        }

        foreach (var selectedKey in ASPxGridView1.GetSelectedFieldValues(ASPxGridView1.KeyFieldName))
        {
            if (!indexes.Contains(selectedKey))
                ASPxGridView1.Selection.UnselectRowByKey(selectedKey);
        }
    }

提前致谢...

更新: 当我使用“asp:SqlDataSource”时它工作正常。

1 个答案:

答案 0 :(得分:0)

你有没有尝试在回发/回调期间做一个数据绑定?我认为你的网格不知道你的按钮点击事件中有哪些行。