Using Datagrid with EnableViewState="false" gets wrong DataKeys value

Discussion in 'ASP .Net Datagrid Control' started by Earl Teigrob, Jul 17, 2003.

  1. Earl Teigrob

    Earl Teigrob Guest

    I have created a datagrid with search text boxes on the page that allow the
    user to filter the list. If I use view state, as in the Example 1,
    everything works perfectly. However, I would like to disable view state and
    regenerate the datagrid on every page load/post back. However, when I set
    EnableViewState="false" and move the BindDataGrid call to the main body of
    the Page Load method (Example 2), everything works fine visually but
    clicking on an event that uses

    DataGrid1.DataKeys[e.Item.ItemIndex]

    produces the DataKey for the previous datagrid indexItem, not the current
    one.

    I realize that I must regenerate all controls on a postback and that I must
    not be doing this correctly but what am I doing wrong???



    (Complete codebehind listing at bottom of the post)

    Thanks for your help

    Earl





    //EXAMPLE 1: DATAGRID AND SEARCH FILTER WITH ENABLEVIEWSTATE=TRUE

    private void DataBindGrid()

    {

    string s = Convert.ToString(Application["JeWebSite_CS"]);

    JumpDbDataClass r = new
    JumpDbDataClass(Convert.ToString(Application["JeWebSite_CS"]));

    DataSet ds = r.Select(SortOrder, SortAsc, 0, RedirectKey1.Text,
    DisplayName1.Text, State1.Text, Chain1.Text);

    DataGrid1.CurrentPageIndex=CurrentPageIndex;

    DataGrid1.DataSource=ds;

    DataGrid1.DataBind();

    }



    private void Page_Load(object sender, System.EventArgs e)

    {

    if (!(Page.IsPostBack))

    {

    DataBindGrid();

    }

    private void Search1_Click(object sender, System.EventArgs e)

    {

    DataBindGrid();

    }



    //EXAMPLE 2: DATAGRID AND SEARCH FILTER WITH ENABLEVIEWSTATE=FALSE

    (same as above except)

    private void Page_Load(object sender, System.EventArgs e)

    {

    DataBindGrid();

    }







    //EXAMPLE 3: ENTIRE PAGE CODEBEHIND

    using System;

    using System.Collections;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Web;

    using System.Web.SessionState;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.HtmlControls;

    using csCommonLibrary;

    using csCommonLibrary.Security;

    using csJumpDb.JumpDbDataObjects;

    using System.Data.SqlClient;

    using System.Web.Security;

    namespace JeIntranet

    {



    /// <summary>

    /// Summary description for main.

    /// </summary>

    public abstract class JumbDbList :
    JeIntranet.UserControls.WebUserControlDerived

    //public abstract class main : System.Web.UI.UserControl

    {


    public JumbDbList():base(2, "jumpdb.section")

    {

    }

    enum mode {none, edit, add};

    protected System.Web.UI.WebControls.TextBox StateName1;

    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    protected System.Web.UI.WebControls.Literal StatusMessage;

    protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;

    protected System.Web.UI.WebControls.Button Button2;

    protected System.Web.UI.WebControls.TextBox StateDesc2;

    protected System.Web.UI.WebControls.TextBox StateId1;

    protected System.Web.UI.WebControls.TextBox pkState5;

    protected System.Web.UI.WebControls.TextBox RedirectKey1;

    protected System.Web.UI.WebControls.TextBox DisplayName1;

    protected System.Web.UI.WebControls.TextBox State1;

    protected System.Web.UI.WebControls.TextBox Chain1;

    protected System.Web.UI.WebControls.Button Search1;

    protected System.Web.UI.WebControls.Panel pnlAddButton1;

    protected System.Web.UI.HtmlControls.HtmlTableRow AddButtonRow;

    protected System.Web.UI.HtmlControls.HtmlInputButton Button1;

    protected System.Web.UI.HtmlControls.HtmlTableRow ShowMessage;

    protected System.Web.UI.HtmlControls.HtmlInputButton Add1;

    protected System.Web.UI.WebControls.CustomValidator CustomValidator1;


    private void Page_Load(object sender, System.EventArgs e)

    {

    if (Common.RightsTo("jumpdb.admin"))

    AddButtonRow.Visible=true;

    DisplayName1.Attributes.Add("style","width:486");

    if (!(Page.IsPostBack))

    {

    RedirectKey1.Text=(Request.Params["RedirectKey"]==null)?"":Request.Params["R
    edirectKey"];

    DisplayName1.Text=(Request.Params["DisplayName"]==null)?"":Request.Params["D
    isplayName"];

    State1.Text=(Request.Params["State"]==null)?"":Request.Params["State"];

    Chain1.Text=(Request.Params["Chain"]==null)?"":Request.Params["Chain"];

    CurrentPageIndex=(Request.Params["CurrentPageIndex"]==null)?0:Convert.ToInt3
    2(Request.Params["CurrentPageIndex"]);

    SortOrder=(Request.Params["SortOrder"]==null)?1:Convert.ToInt32(Request.Para
    ms["SortOrder"]);

    SortAsc=(Request.Params["SortAsc"]==null)?true:Convert.ToBoolean(Request.Par
    ams["SortAsc"]);


    }

    DataBindGrid();

    }

    private void DataBindGrid()

    {

    string s = Convert.ToString(Application["JeWebSite_CS"]);

    JumpDbDataClass r = new
    JumpDbDataClass(Convert.ToString(Application["JeWebSite_CS"]));

    DataSet ds = r.Select(SortOrder, SortAsc, 0, RedirectKey1.Text,
    DisplayName1.Text, State1.Text, Chain1.Text);

    DataGrid1.CurrentPageIndex=CurrentPageIndex;

    DataGrid1.DataSource=ds;

    DataGrid1.DataBind();

    }

    private void DataGrid1_DeleteCommand(object sender,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)

    {

    string s = sender.ToString();

    string x = e.Item.Width.ToString();

    int pkState = (int) DataGrid1.DataKeys[e.Item.ItemIndex];



    if (StatMode==mode.none)

    {

    JumpDbDataClass r = new
    JumpDbDataClass(Convert.ToString(Application["JeWebSite_CS"]));

    r.Delete(Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]));

    DataBindGrid();

    }

    else

    {

    StatusMessage.Text="Can not delete a State while status in " +
    StatMode.ToString() + " mode";

    }

    }

    private void DataGrid1_SortCommand(object sender,
    System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

    {


    if (StatMode==mode.none)

    {

    int SortExpression=Convert.ToInt32(e.SortExpression);

    SortAsc = newSortType(SortExpression);

    SortOrder = SortExpression;

    DataBindGrid();

    }

    else

    StatusMessage.Text="Can not sort while status in " + StatMode.ToString() + "
    mode";

    }

    private bool newSortType(int SortExp)

    {

    if (SortOrder==SortExp)

    {

    return SortAsc==true?false:true;

    }

    else

    return true;

    }

    private void DataGrid1_ItemCreated (Object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)

    {

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)

    {

    WebControl button = (WebControl) e.Item.Cells[6].Controls[0];

    button.Attributes.Add ("onclick", "return confirm (\"Are you sure?\");");

    }

    }

    private void Datagrid1_PreRender(Object s, EventArgs e)

    {

    }

    private mode StatMode

    {

    get

    {

    object x = ViewState["StatMode"];

    return x==null?mode.none:(mode)x;

    }

    set

    {

    ViewState["StatMode"] = value;

    }

    }

    private int SortOrder

    {

    get

    {

    object x = ViewState["SortOrder"];

    return x==null?1:(int)x;

    }

    set

    {

    ViewState["SortOrder"] = value;

    }

    }

    private bool SortAsc

    {

    get

    {

    object x = ViewState["SortAsc"];

    return x==null?true:(bool)x;

    }

    set

    {

    ViewState["SortAsc"] = value;

    }

    }

    private int CurrentPageIndex

    {

    get

    {

    object x = ViewState["CurrentPageIndex"];

    return x==null?0:(int)x;

    }

    set

    {

    ViewState["CurrentPageIndex"] = value;

    }

    }

    private string RedirectKey

    {

    get

    {

    object x = RedirectKey1.Text;

    return Convert.ToString(x).Length==0?null:(string)x;

    }

    }





    private string DisplayName

    {

    get

    {

    object x = DisplayName1.Text;

    return Convert.ToString(x).Length==0?null:(string)x;

    }

    }

    private string State

    {

    get

    {

    object x = State1.Text;

    return Convert.ToString(x).Length==0?null:(string)x;

    }

    }

    private string Chain

    {

    get

    {

    object x = Chain1.Text;

    return Convert.ToString(x).Length==0?null:(string)x;

    }

    }



    private void DataGrid1_PageChanged(object sender,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

    {

    CurrentPageIndex=e.NewPageIndex;

    DataBindGrid();

    }

    private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs
    e)

    {

    CurrentPageIndex=0;

    DataBindGrid();

    }

    private void TextBox4_TextChanged(object sender, System.EventArgs e)

    {


    }

    private void Search1_Click(object sender, System.EventArgs e)

    {

    CurrentPageIndex=0;

    DataBindGrid();

    }

    private void DataGrid1_ItemCommand(object sender,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)

    {

    int i, k;

    if (e.CommandName=="Edit")

    {

    if (StatMode==mode.none)

    {

    i = e.Item.ItemIndex;

    k = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);


    Response.Redirect("default.aspx?page=Pages/JumpDb/JumpDbDetail.ascx"

    + "&pkRedirectTable=" + DataGrid1.DataKeys[e.Item.ItemIndex]

    + "&viewmode=Edit"

    + "&RedirectKey=" + RedirectKey

    + "&DisplayName=" + DisplayName

    + "&State=" + State

    + "&Chain=" + Chain

    + "&SortOrder=" + SortOrder

    + "&SortAsc=" + SortAsc

    );

    }

    else

    StatusMessage.Text="Can not manage role permissions while status in " +
    StatMode.ToString() + " mode";

    }

    else

    {

    if (e.CommandName=="View")

    {

    if (StatMode==mode.none)


    Response.Redirect("default.aspx?page=Pages/JumpDb/JumpDbDetail.ascx"

    + "&pkRedirectTable=" + DataGrid1.DataKeys[e.Item.ItemIndex]

    + "&viewmode=View"

    + "&RedirectKey=" + RedirectKey

    + "&DisplayName=" + DisplayName

    + "&State=" + State

    + "&Chain=" + Chain

    + "&SortOrder=" + SortOrder

    + "&SortAsc=" + SortAsc

    );

    else

    StatusMessage.Text="Can not manage role permissions while status in " +
    StatMode.ToString() + " mode";

    }



    }







    }



    private void Button1_ServerClick(object sender, System.EventArgs e)

    {

    Response.Redirect("index.aspx");

    }

    private void Add1_ServerClick(object sender, System.EventArgs e)

    {

    if (StatMode==mode.none)

    Response.Redirect("default.aspx?page=Pages/JumpDb/JumpDbDetail.ascx"

    + "&viewmode=Add"

    + "&RedirectKey=" + RedirectKey

    + "&DisplayName=" + DisplayName

    + "&State=" + State

    + "&Chain=" + Chain

    + "&SortOrder=" + SortOrder

    + "&SortAsc=" + SortAsc

    );

    else

    StatusMessage.Text="Can not manage role permissions while status in " +
    StatMode.ToString() + " mode";


    }

    #region Web Form Designer generated code

    override protected void OnInit(EventArgs e)

    {

    //

    // CODEGEN: This call is required by the ASP.NET Web Form Designer.

    //

    InitializeComponent();

    base.OnInit(e);

    }


    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    this.Chain1.TextChanged += new
    System.EventHandler(this.TextBox4_TextChanged);

    this.Search1.Click += new System.EventHandler(this.Search1_Click);

    this.DataGrid1.ItemCreated += new
    System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreate
    d);

    this.DataGrid1.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCom
    mand);

    this.DataGrid1.PageIndexChanged += new
    System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_Pag
    eChanged);

    this.DataGrid1.SortCommand += new
    System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_Sor
    tCommand);

    this.DataGrid1.DeleteCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteC
    ommand);

    this.DataGrid1.SelectedIndexChanged += new
    System.EventHandler(this.DataGrid1_SelectedIndexChanged);

    this.Add1.ServerClick += new System.EventHandler(this.Add1_ServerClick);

    this.Load += new System.EventHandler(this.Page_Load);

    }

    #endregion

    }


    }
    Earl Teigrob, Jul 17, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Verwer

    EnableViewState = false not working

    Dave Verwer, Jul 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    671
    Dave Verwer
    Jul 10, 2003
  2. JollyK
    Replies:
    5
    Views:
    21,812
    Peter Blum
    Jun 2, 2004
  3. michele
    Replies:
    2
    Views:
    503
    michele
    Aug 26, 2004
  4. DC
    Replies:
    2
    Views:
    379
  5. michele
    Replies:
    0
    Views:
    119
    michele
    Aug 26, 2004
Loading...

Share This Page