How to get cell value from a gridview?

倾然丶 夕夏残阳落幕 提交于 2020-01-06 12:50:06

问题


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.


回答1:


Try this:

int lastrow = grdPrevious.Rows.Count - 1;
Label lb = (Label)grdPrevious.Rows[lastrow].FindControl("Label5");
Response.Write(lb.Text);



回答2:


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 ?




回答3:


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();



回答4:


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.




回答5:


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.)




回答6:


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)




回答7:


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!



来源:https://stackoverflow.com/questions/8665176/how-to-get-cell-value-from-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!