0

Friends, I've a gridview in my web-page designed in the following manner:-

    <div id="divProducts" style="height:200px; overflow:auto">
       <asp:GridView ID="grdPrevious" runat="server" Width="100%" 
           AutoGenerateColumns="false" ShowHeader="false" GridLines="None" 
           ShowFooter="true" AllowPaging="false">
           <Columns>
               <asp:TemplateField>
                  <ItemTemplate>
                      <tr>
                          <td style="width:20%; visibility:hidden">
                              <label class="labelHead">Activity Id </label>
                          </td><td></td>
                          <td style="width:80%; visibility:hidden">
                              <asp:Label ID="Label5" runat="server" 
                                 Text='<%# Eval("USER_ACTIVITIES_ID") %>'
                                 CssClass="labelCss"></asp:Label>
                          </td>
                      </tr>
                      <tr>
                          <td style="width:20%" >
                             <label class="labelHead">Date </label>
                          </td><td>:</td>
                          <td style="width:80%">
                              <asp:Label ID="Label1" runat="server" 
                                  Text='<%# Eval("ACTIVITY_DATE1") %>'
                                  CssClass="labelCss"></asp:Label>
                          </td>
                      </tr>
                      <tr>
                          <td>
                             <label class="labelHead">Interaction Type </label>
                          </td>
                          <td>:</td>
                          <td>
                              <asp:Label ID="Label2" runat="server" 
                                  Text='<%# Eval("INTERFACE_DESCRIPTION") %>'
                                  CssClass="labelCss" ></asp:Label>
                          </td>
                      </tr>
                      <tr>
                          <td>
                              <label class="labelHead">Prospect</label>
                          </td>
                          <td>:</td>
                          <td>
                              <asp:Label ID="Label3" runat="server" 
                                  Text='<%# Eval("PROSPECT_DESCRIPTION") %>' 
                                  CssClass="labelCss"></asp:Label>
                          </td>
                      </tr>
                      <tr>
                          <td  valign="top" >
                              <label class="labelHead">Note </label>
                          </td>
                          <td valign="top">:</td>
                          <td  valign="top">
                              <asp:Label ID="Label4" runat="server" 
                                  Text='<%# Eval("NOTES") %>' CssClass="labelCss">
                              </asp:Label>
                          </td>                                                                
                      </tr>
                      <tr>
                          <td colspan="3"><hr size="1px" color="#D5DEA1" 
                             style="margin:10px 0px;" /> 
                          </td>
                       </tr>
                    </ItemTemplate>
                    <ItemStyle Font-Names="Trebuchet MS;" />
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                <table width="100%" cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td align="center" class="gridViewNoRecords">
                            No Records found
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
        </asp:GridView>
    </div>

I'm binding this grid with a datasource. Now,I want to get the value of "USER_ACTIVITIES_ID" from the last populated row of the gridview. I'm using hiddenfield to hold this id and use it for other purpose in the project using javascript. But,my problem is I'm not getting the ID's value(neither by back-end coding nor by javascript). Can anyone tell me in which event or by code,I can get this value?Please,I need it badly. Thanks in advance.

Sukanya
  • 991
  • 7
  • 19
  • 40
  • How are you setting the value, not clear you are not able to get value from hidden field or grid, in short what is the actual issue – V4Vendetta Dec 29 '11 at 07:45
  • I've not been able to get the value of "USER_ACTIVITIES_ID" from the last row of the grid to any variable(hiddenfield or normal variable).I'm fetching data from sqlserver 2005 database and putting it in a dataset and binding the grid with this dataset.But I can't 'GET' the value.. – Sukanya Dec 29 '11 at 07:53
  • @Alison why do you people edit the question?Is it not readable or understandable? – Sukanya Jan 04 '12 at 06:43

7 Answers7

1

Try this:

int lastrow = grdPrevious.Rows.Count - 1;
Label lb = (Label)grdPrevious.Rows[lastrow].FindControl("Label5");
Response.Write(lb.Text);
Paul Turner
  • 35,361
  • 15
  • 90
  • 155
Vinod
  • 3,872
  • 4
  • 17
  • 26
0

What is the purpose? For eg. If you want to delete or edit the particular item, on click of a button in the table row ?

Acn
  • 872
  • 2
  • 9
  • 22
  • No,I've to get the last ID displayed in the grid and then fetch the next 10 data from the table of sqlserver 2005 database. – Sukanya Dec 29 '11 at 07:55
0

I would use JQuery to access to last instance of Label5 within the divProducts. Firstly I would add an additional class to Label5 so that it is marked different from all the other spans in the table (Note:asp:Label will render a SPAN)

 <asp:Label ID="Label5" runat="server" 
                             Text='<%# Eval("USER_ACTIVITIES_ID") %>'
                             CssClass="labelCss useractivity"></asp:Label>

Then something like this to find it on the client.

var id=$("#divProducts").children("span[class=useractivity]:last").text();
fielde
  • 1
  • 1
0

Ok. What I Guess you want to do is pagination. If I'm right, there are many many many tutorials to do table pagination (paging of bulk rows, in a numbered sequence). In ASP.Net I think there is a property called 'AlloWPaging' for the standard 'GridView' control.

Acn
  • 872
  • 2
  • 9
  • 22
  • Thanks.I'm doing pagination in scroll down event.That part of the code comes at a later stage.But first I need to have the last 'ID' from the grid. – Sukanya Dec 29 '11 at 08:07
0

This is the best idea from my side then. -

Use DOM to iterate through the <tr> elements. First take the <table> element. Then get the firstChild. While there are more <tr> elements inside the <table> element keep on iterating. When you get the last '' element, you are there.

You can look for the TD in that last TR, where your last USER_ACTIVITY_ID is stored. Then you can get the inner text value. A simple example will be -

getTextFromTD = function() {
var gridView = document.getElementById("myGridView");
var lastTR = gridView.childNodes.item(5);
var ourRequiredTD = lastTR.childNodes(2);
requiredText = ourRequiredTD.innerHTML;
}

(Sometimes it becomes difficult to help when the actual code is not infront of me.)

Acn
  • 872
  • 2
  • 9
  • 22
  • I've no idea about how to iterate through and where to write the code.Can you provide something? – Sukanya Dec 29 '11 at 08:53
0

you need to provide an HTML id to the GridView (In your case it's grdPrevious).

you can open the web page in design view and write in <HEAD> -

<script language='javascript' runat='server'>

</script>

and you can javascript there using the code such as one listed by John Hartsock in

this link.

Hope it helps you. :) (Actually I'm not a asp.net developer)

Community
  • 1
  • 1
Acn
  • 872
  • 2
  • 9
  • 22
0

Try this one!

int lastRecord = grdPrevious.Rows.Count - 1;
Label lbl;
lbl = (Label)grdPrevious.Rows[lastRecord].Cells[0].FindControl("Label5");

int ID = Convert.ToInt32(lbl.Text);

Hope this one helps!

Eduard Lu
  • 1
  • 2