473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Datagrid loses its javascript on Postback?

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("onmouseo ut");
}

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.cur sor='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>
Nov 18 '05 #1
2 2373
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" <bw@noDirectEmail.com> a écrit dans le message de news:
u$*************@TK2MSFTNGP09.phx.gbl...
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("onmouseo ut");
}

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.cur sor='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>

Nov 18 '05 #2
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" <bw@noDirectEmail.com> a écrit dans le message de news:
u$*************@TK2MSFTNGP09.phx.gbl...
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("onmouseo ut");
}

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.cur sor='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>

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brian Conway | last post by:
How do I code for a datagrid to postback an editable field? I have field that come up automatically when the datagrid gets populated. You can make some changed right there to multiple fields,...
2
by: Manish | last post by:
Hey folks I am having a weird problem in ASP .Net. My page is in C#. I have a datagrid, which populates based on selection in drop down box on ASP page. This datagrid has template textbox colum in...
2
by: Dave | last post by:
Hi, I have a datagrid with a Templated column below. I want to execute some javascript before the postback to show a hidden "div" tag with static message of "Please Wait..." since the query takes...
6
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
0
by: Ray | last post by:
I am trying to add a "Next >" and "<Prev" button to the pager of my datagrid. I have been able to add the hyperlinks to the pager. I am now trying to determine the correct postback script to...
1
by: Ken Varn | last post by:
I have a problem where my DataGrid would not maintain the ViewState of my databound rows. I finally narrowed down the problem. If my first column is a template column, the view state for the...
0
by: Tumurbaatar S. | last post by:
My web user control (ASCX) consists of one DataGrid and several web user controls (ASCX). On page postback, the DataGrid loses its content: row/item information, i.e. cells text. Other user...
1
by: John | last post by:
I am using the ItemDataBound event on the datagrid to place relevant images in the grid. However, when I press any button on the form which fires a postback the state of the grid is kept but the...
0
by: parker.scott | last post by:
I have a web page with two text boxes and a DataGrid with controls. During postback, the textboxes will have whatever value the user put in, but the DataGrid has nothing at all. grid.Items.Count ==...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.