B
Bruce W.1
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(object sender, System.EventArgs e)
{
_db = new dbClass();
DataGrid1.ItemCreated += new DataGridItemEventHandler(this.Item_Created);
DataGrid1.SelectedIndexChanged += new EventHandler(this.DataGrid1_SelectedIndexChanged);
if (!IsPostBack) BindGrid();
}
public void DataGrid1_SelectedIndexChanged (Object sender, EventArgs e)
{
DataGrid1.SelectedItem.Attributes["onmouseover"] = "this.style.cursor='hand'";
DataGrid1.SelectedItem.Attributes.Remove("onmouseout");
}
protected void BindGrid()
{
DataSet ds = _db.GetList();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
void Item_Created(Object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='beige';this.style.cursor='hand'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" + "DataGrid1:" + "_ctl" + (e.Item.ItemIndex + 2) + ":_ctl0','')");
}
And in the .aspx file:
======================
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="400px">
<SelectedItemStyle BackColor="PaleTurquoise"></SelectedItemStyle>
<ItemStyle BackColor="White"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="present" HeaderText="Present"></asp:BoundColumn>
<asp:BoundColumn DataField="first_name" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="last_name" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="telephone" HeaderText="Telephone"></asp:BoundColumn>
<asp:ButtonColumn Visible="False" Text="Select" CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="id" HeaderText="id"></asp:BoundColumn>
</Columns>
</asp:datagrid>
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(object sender, System.EventArgs e)
{
_db = new dbClass();
DataGrid1.ItemCreated += new DataGridItemEventHandler(this.Item_Created);
DataGrid1.SelectedIndexChanged += new EventHandler(this.DataGrid1_SelectedIndexChanged);
if (!IsPostBack) BindGrid();
}
public void DataGrid1_SelectedIndexChanged (Object sender, EventArgs e)
{
DataGrid1.SelectedItem.Attributes["onmouseover"] = "this.style.cursor='hand'";
DataGrid1.SelectedItem.Attributes.Remove("onmouseout");
}
protected void BindGrid()
{
DataSet ds = _db.GetList();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
void Item_Created(Object sender, DataGridItemEventArgs e)
{
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='beige';this.style.cursor='hand'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" + "DataGrid1:" + "_ctl" + (e.Item.ItemIndex + 2) + ":_ctl0','')");
}
And in the .aspx file:
======================
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="400px">
<SelectedItemStyle BackColor="PaleTurquoise"></SelectedItemStyle>
<ItemStyle BackColor="White"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="present" HeaderText="Present"></asp:BoundColumn>
<asp:BoundColumn DataField="first_name" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="last_name" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="telephone" HeaderText="Telephone"></asp:BoundColumn>
<asp:ButtonColumn Visible="False" Text="Select" CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="id" HeaderText="id"></asp:BoundColumn>
</Columns>
</asp:datagrid>