•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 391,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,576 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser:
Views: 14444 | Replies: 12
![]() |
•
•
Join Date: Jul 2006
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
I am not new to ASP and c# but fairly new to Javascript. I have a Datagrid with a button column and a text column. The button is linked to a Javascript which when pressed prompts the user to enter a number. Upon entering a number and clicking 'OK', I want the number to be added to whatever number is present in the Text column.
I have been able to get the number correctly. The part that I have not been able to figure out is 'How to access the current row/column from the Javascript so I can take the number and add it to the TextBox cell'. I tried passing the current cell object to the Javascript but it gives me an error.
Here's my C# code
Here's my Javascript
I have scoured several Javascript websites but couldn't find much help with Datagrids. Any help would be greatly appreciated.
Thanks
ktum
I am not new to ASP and c# but fairly new to Javascript. I have a Datagrid with a button column and a text column. The button is linked to a Javascript which when pressed prompts the user to enter a number. Upon entering a number and clicking 'OK', I want the number to be added to whatever number is present in the Text column.
I have been able to get the number correctly. The part that I have not been able to figure out is 'How to access the current row/column from the Javascript so I can take the number and add it to the TextBox cell'. I tried passing the current cell object to the Javascript but it gives me an error.
Here's my C# code
private void dgTest_ItemCreated(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemIndex >= 0)
{
Button btn = (Button)e.Item.Cells[3].Controls[1];
//Does not work; Browser does not like the parameter
btn.Attributes.Add("onclick","disp_prompt(<%= e.Item.Cells[4] %>)");
}
}Here's my Javascript
function disp_prompt(tablecell)
{
var number=prompt("Enter number","")
if (number!=null && number!="")
{
tablecell.value += number;
}
}I have scoured several Javascript websites but couldn't find much help with Datagrids. Any help would be greatly appreciated.
Thanks
ktum
Last edited by ktum : Jul 20th, 2006 at 9:59 am.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation:
Rep Power: 7
Solved Threads: 58
A datagrid id rendered to the client as an HTML table
<table><tr><td>... blah de blah
So thats what javascript is seeing, javascript will never know diddley squat about what the hell a datagrid is
Also you are using the ItemCreated event this happens before the ItemDataBound event so at that point
I would use the 'this' property as the argument to your client function disp_prompt();
That will pass a reference to the button itself, or use the W3C DOM 'event' in your javascript which will be a reference to the element that raised the last event (eg the clicked button in the DOM), you can use knowledge of the DOM to find cell number four because cell number four and the clicked button share the same parent element (a table row)
so in your javascript function you can have something like:
You might need to check my javascript I havn't time to check the exact syntax but it shouldn;t be too disimilar to what I have posted.
<table><tr><td>... blah de blah
So thats what javascript is seeing, javascript will never know diddley squat about what the hell a datagrid is
Also you are using the ItemCreated event this happens before the ItemDataBound event so at that point
e.Item.Cells[4] is empty anyway and is a reference to the cell value not the cell itself.I would use the 'this' property as the argument to your client function disp_prompt();
btn.Attributes.Add("onclick","disp_prompt(this)");That will pass a reference to the button itself, or use the W3C DOM 'event' in your javascript which will be a reference to the element that raised the last event (eg the clicked button in the DOM), you can use knowledge of the DOM to find cell number four because cell number four and the clicked button share the same parent element (a table row)
so in your javascript function you can have something like:
function disp_prompt(button)
{
var number=prompt("Enter number","")
if (number!=null && number!="")
{
var row = button.parentElement;
var cell = row.cells[3];
var num = cell.innerHTML;
cell.innerHTML = (num+number);
}
}You might need to check my javascript I havn't time to check the exact syntax but it shouldn;t be too disimilar to what I have posted.
•
•
Join Date: Jul 2006
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks for your response. I tried several variations of the code that you suggested but none of them gave me access to the 'current row'. I even tried
var row = button.parentElement;
row.rowIndex;
Also, it just doesn't seem to like row.cells[4] at all. Is there another way to get the row and column index of an item?
Any help would be very much appreciated. I've spent way too much time on this.
Thanks!
var row = button.parentElement;
row.rowIndex;
Also, it just doesn't seem to like row.cells[4] at all. Is there another way to get the row and column index of an item?
Any help would be very much appreciated. I've spent way too much time on this.
Thanks!
Last edited by ktum : Jul 24th, 2006 at 1:10 pm.
Hi,
Even if you manage to alter the data w/ JScript on the client side grid you see in the browser, this will not affect the datagrid object and the bound dataset on the server. If you don't like the postbacks for each button click and want to use AJAX style code you should try ATLAS.Net from MS but it is still tricky to alter databound grid style webform controls this way.
Loren Soth
Even if you manage to alter the data w/ JScript on the client side grid you see in the browser, this will not affect the datagrid object and the bound dataset on the server. If you don't like the postbacks for each button click and want to use AJAX style code you should try ATLAS.Net from MS but it is still tricky to alter databound grid style webform controls this way.
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation:
Rep Power: 7
Solved Threads: 58
•
•
•
•
Thanks for your response. I tried several variations of the code that you suggested but none of them gave me access to the 'current row'. I even tried
Ah yes actually the parentElement of the button is the cell, so you may need to do:
var row = button.parentElement.parentElement;
Last edited by hollystyles : Jul 25th, 2006 at 4:10 am.
•
•
Join Date: Jul 2006
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Thank you Lord Soth and hollystyles. I was able to update the cell but the change lasted only for a second before it disappeared. May be changing a Datagrid control after it is bound to a DataSet is not so straightforward as it seems.
I am curious to know why. After the ASP is sent over to the client's browser isn't it mere HTML? I should be able to change it, right?
Thanks for your help.
I am curious to know why. After the ASP is sent over to the client's browser isn't it mere HTML? I should be able to change it, right?
Thanks for your help.
Last edited by ktum : Jul 25th, 2006 at 8:55 am.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation:
Rep Power: 7
Solved Threads: 58
The button is causing a post back, so the page is rebuilt and the datagrid refilled. have you got paging set-up on the datagrid or something?
What are you trying to achieve here? I think you maybe going about this the wrong way.
Once the number is added to the existing number I presume you are going to want to save that back to the database ?
If so you are up the wrong tree I feel.
consider this:
user clicks button,
types a number at the prompt
javascript to put the number in a hidden text control not the datagrid cell
postback occurs
server side identify which button clicked
add the number for that row index + the number in the hidden control
bind the datagrid
back to the client
What are you trying to achieve here? I think you maybe going about this the wrong way.
Once the number is added to the existing number I presume you are going to want to save that back to the database ?
If so you are up the wrong tree I feel.
consider this:
user clicks button,
types a number at the prompt
javascript to put the number in a hidden text control not the datagrid cell
postback occurs
server side identify which button clicked
add the number for that row index + the number in the hidden control
bind the datagrid
back to the client
•
•
Join Date: Jul 2006
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
I changed my DataGrid design completely. Instead of prompting the user for a number, I have another column in the Datagrid where the user could enter the number and I have a '+' button when clicked will add this number to the Total. This application is for field Biologists to enter species and count.
My datagrid has 4 columns.
Column 1 - Template Dropdown column which is databound.
Column 2 - total
Column 3 - count
Column 4 - + button
My datagrid works now except that as hollystyles mentioned, when the button is clicked, a PostBack happens and all the Dropdown columns get refreshed and it takes a few seconds for the page to reload before the user can proceed.
How can I overcome this problem?
Thanks again for all the help.
My datagrid has 4 columns.
Column 1 - Template Dropdown column which is databound.
Column 2 - total
Column 3 - count
Column 4 - + button
My datagrid works now except that as hollystyles mentioned, when the button is clicked, a PostBack happens and all the Dropdown columns get refreshed and it takes a few seconds for the page to reload before the user can proceed.
How can I overcome this problem?
Thanks again for all the help.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,164
Reputation:
Rep Power: 7
Solved Threads: 58
Did you start this project with a thorough analysis of the problem that produced a apecification ?
You have coded yourself into a bit of a cul-de-sac my friend. I presume the drop down lists have a list of species ? how long is the list and how many drop downs in the datagrid ? and of course the web is a stateless world so you have to re-build them every time, and of course they then lose there selected value. I feel you need to read up on the ASP page life cycle it's fairly obvious this is a gap in your knowledge.
This is a typical "schlemiel the painter" scenario Joel Spolsky is right, these .NET abstractions are great but they don't scale if you don't know what you are doing.
Have ONE drop down list on the page, and ONE textbox and ONE button, below all of those ONE datagrid (no buttons and shit in the datagrid just the results)
Use Case - species count form
(you heard of these? if not read up on software analysis and design)
User counts some species, selects that species from drop down box and types the amount they counted into the text box, and clicks the submit button. The form is POSTED to the server where the value in the textbox is converted to an integer and added to any existing count in the database, the data grid is re-bound and the page sent back to the user ready for the next species count.
You have coded yourself into a bit of a cul-de-sac my friend. I presume the drop down lists have a list of species ? how long is the list and how many drop downs in the datagrid ? and of course the web is a stateless world so you have to re-build them every time, and of course they then lose there selected value. I feel you need to read up on the ASP page life cycle it's fairly obvious this is a gap in your knowledge.
This is a typical "schlemiel the painter" scenario Joel Spolsky is right, these .NET abstractions are great but they don't scale if you don't know what you are doing.
Have ONE drop down list on the page, and ONE textbox and ONE button, below all of those ONE datagrid (no buttons and shit in the datagrid just the results)
Use Case - species count form
(you heard of these? if not read up on software analysis and design)
User counts some species, selects that species from drop down box and types the amount they counted into the text box, and clicks the submit button. The form is POSTED to the server where the value in the textbox is converted to an integer and added to any existing count in the database, the data grid is re-bound and the page sent back to the user ready for the next species count.
Last edited by hollystyles : Jul 26th, 2006 at 9:49 am.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
- Previous Thread: I need to write code for the save and close botton and close button on my form
- Next Thread: Listview: How do I get the data from a column in a selected row



Linear Mode