Datagrid loses its javascript on Postback?

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>
 
L

\(Laurent Jordi\)

I think you don't understand the mechanism of PostBack... Try to view the
Client source code of an aspx page...
The web form generates a serial of "submitters". Each of them submits the
form to it self. The dot Net form analysis the difference between the
original _viewstate and the submitted one to determine witch event to fire.

When the page is submitted to the server the page is unloaded from the
navigator and reloaded with a new content... All variables are erased during
this operation...

If the page is light enough you can't see the reloading because it's too
fast...

If you want to keep your values client side... You can create a specific
frame that contain hidden fields to keep your values...
In the onLoad of the page you can access those values with..

var MyLocalVar =
window.parent.frames["frameName"].document.[hiddenName].value

This works very well

If you don't do that you have to use the postback Boolean serverside to do
not replace your values by the original form value.

Be careful... Even if visual studio and Internet explorer understand
document.control.value or form1.control.value. You always have to respect
the full syntax from window... window.document.form1.conrol.value to be
compatible with Mozilla, netscape or Macintosh Internet Explorer...

I hope this help you...

Regards

Laurent Jordi
www.eztree-msdn.com
www.ezlogic.mc




Bruce W.1 said:
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"> said:
<asp:BoundColumn DataField="first_name" HeaderText="First
Name"> said:
<asp:BoundColumn DataField="last_name" HeaderText="Last
Name"> said:
<asp:BoundColumn DataField="telephone"
HeaderText="Telephone"> said:
<asp:ButtonColumn Visible="False" Text="Select"
 
L

\(Laurent Jordi\)

I think you don't don't understand the mechanism of POstBack... Try to view
the Client source code of an aspx page...
The web form generate a seriel of "subitters". Each of them self-submit the
form. The form dot Net analysis the difference between the original
_viewstate and the submitted one...

When the page is submitted to the server the page is unloaded from the
navigator and reloaded with a new content... All variables are erased during
this operation...

If the page is light enought you can't see the reloading because it's too
fast...

If you wana keep your values client side... You can create a specific frame
that contain hidden fields to keep your values...
In the onLoad of the page you can access those values with..

var MyLocalVar =
window.parent.frames["frameName"].document.[hiddenName].value

This works very well

Be carefull... Even if visual studio and internet explorer understand
document.control.value or form1.control.value

You always have to respect the full syntax from window...
window.document.form1.conrol.value to be compatible with Mozilla, netscape
or internet explorer for mac...

I hope this help you...

Regards

Laurent Jordi

www.eztree-msdn.com





Bruce W.1 said:
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"> said:
<asp:BoundColumn DataField="first_name" HeaderText="First
Name"> said:
<asp:BoundColumn DataField="last_name" HeaderText="Last
Name"> said:
<asp:BoundColumn DataField="telephone"
HeaderText="Telephone"> said:
<asp:ButtonColumn Visible="False" Text="Select"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top