A datagrid id rendered to the client as an HTML table
... 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.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
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;
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
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
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
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.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
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:
Here was the discreet approach, but you totally missed that so I went for the jugular. I see you didn't like that either. Oh well.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Thanks for your indiscreet comments and your relentless display of your superior brain. You certainly need a How-not-to-be-a-jerk101 class. I'll take my posting elsewhere.
I'd have to weigh in on this one.
Normally, I'm the first one to call someone out for being a "jerk", but I don't see anything wrong with hollystyles's post. Did you read the information in the post, and get past the manner the post was written in? If so I have a feeling you'd be rather enlightened...
alc6379
Cookie... That's it
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
Thanx for the support Alex
Perhaps I was a little harsh, but like you point out my arguements were valid and pinpoint the areas that need attention. I just get so annoyed when my hard learned craft is devalued by victims of the Microsoft marketing machine trying to make out software is easy and anyone can do it.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68