There's something I can't figure out. I added some javascript
behavior to my datagrid, just like in this article, except in
C# instead of VB:
http://www.dotnetbips.com/displayarticle.aspx?id=205
If I bind the datagrid to the dataset at all times, whether
Postback or not, then everything works fine.
However if I only bind it when Page is not Postback (just like
in the article) then after it does post back my datagrid
loses its javascript.
The code in the article works fine and does not lose its
javascript on postback. I can't figure out the difference.
Thanks for your help.
Here's my code:
===============
dbClass _db;
private void Page_Load(objec t sender, System.EventArg s e)
{
_db = new dbClass();
DataGrid1.ItemC reated += new DataGridItemEve ntHandler(this. Item_Created);
DataGrid1.Selec tedIndexChanged += new EventHandler(th is.DataGrid1_Se lectedIndexChan ged);
if (!IsPostBack) BindGrid();
}
public void DataGrid1_Selec tedIndexChanged (Object sender, EventArgs e)
{
DataGrid1.Selec tedItem.Attribu tes["onmouseove r"] = "this.style.cur sor='hand'";
DataGrid1.Selec tedItem.Attribu tes.Remove("onm ouseout");
}
protected void BindGrid()
{
DataSet ds = _db.GetList();
DataGrid1.DataS ource = ds;
DataGrid1.DataB ind();
}
void Item_Created(Ob ject sender, DataGridItemEve ntArgs e)
{
e.Item.Attribut es.Add("onmouse over", "this.style.bac kgroundColor='b eige';this.styl e.cursor='hand' ");
e.Item.Attribut es.Add("onmouse out", "this.style.bac kgroundColor='w hite';");
e.Item.Attribut es.Add("onclick ", "javascript:__d oPostBack('" + "DataGrid1: " + "_ctl" + (e.Item.ItemInd ex + 2) + ":_ctl0','' )");
}
And in the .aspx file:
=============== =======
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateCol umns="False" Width="400px">
<SelectedItemSt yle BackColor="Pale Turquoise"></SelectedItemSty le>
<ItemStyle BackColor="Whit e"></ItemStyle>
<Columns>
<asp:BoundColum n DataField="pres ent" HeaderText="Pre sent"></asp:BoundColumn >
<asp:BoundColum n DataField="firs t_name" HeaderText="Fir st Name"></asp:BoundColumn >
<asp:BoundColum n DataField="last _name" HeaderText="Las t Name"></asp:BoundColumn >
<asp:BoundColum n DataField="tele phone" HeaderText="Tel ephone"></asp:BoundColumn >
<asp:ButtonColu mn Visible="False" Text="Select" CommandName="Se lect"></asp:ButtonColum n>
<asp:BoundColum n Visible="False" DataField="id" HeaderText="id" ></asp:BoundColumn >
</Columns>
</asp:datagrid>