473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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>
Nov 18 '05 #1
2 2384
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.f rames["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.contro l.value or form1.control.v alue. You always have to respect
the full syntax from window... window.document .form1.conrol.v alue 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@noDirectEma il.com> a écrit dans le message de news:
u$************* @TK2MSFTNGP09.p hx.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(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>

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.f rames["frameName"].document.[hiddenName].value

This works very well

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

You always have to respect the full syntax from window...
window.document .form1.conrol.v alue 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@noDirectEma il.com> a écrit dans le message de news:
u$************* @TK2MSFTNGP09.p hx.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(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>

Nov 18 '05 #3

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

Similar topics

1
1285
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, however, I want it to postback to the dataset after that field loses focus.
2
5656
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 itemtemplate. Datagrid is something like this- Category Week Amt Week% Button Revenue 1 100 Update Revenue 2 200 ...
2
5579
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 a few seconds. The js function simply sets the css style to display. <asp:TemplateColumn> <ItemTemplate> <asp:LinkButton Runat="server"...
6
5186
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 have also tried changing the second ListBox to an HtmlSelect control) using bog standard JavaScript code like so where "used" is the name of my...
0
1811
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 emulate so that these hyperlinks force the postback of the appropriate linkbuttons in the pager. Let's take a pager with pager: 1 2 3 4 5 It consists...
1
1873
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 DataGrid items is not maintained on postback for some reason. I basically re-created my DataGrid again just to make sure this was indeed the problem....
0
1088
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 controls (and standard controls too) do not lose theirs text. I checked ViewState value of the page with the ViewStateDecoder ( v1.1, by Fritz Onion) and...
1
1622
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 images no longer appear as the ItemDataBound event is no longer fired. The solution to this is to refresh the grid with the original data using the...
0
847
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, even if I load it with information before displaying it. To make this more frustrating, I only see this on the server. Testing locally I have...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.