本文介绍了如何从gridview获取单元格值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我在我的网页上有一个gridview,它的设计方式如下: - $ / $>
$ b $

 < div id =divProductsstyle =height:200px; overflow:auto> 
< asp:GridView ID =grdPreviousrunat =serverwidth =100%
AutoGenerateColumns =falseShowHeader =falseGridLines =None
ShowFooter = trueAllowPaging =false>
<列>
< asp:TemplateField>
< ItemTemplate>
< tr>
< td style =width:20%; visibility:hidden>
< label class =labelHead>活动ID< / label>
< / td>< td>< / td>
< td style =width:80%; visibility:hidden>
< asp:Label ID =Label5runat =server
Text ='<%#Eval(USER_ACTIVITIES_ID)%>'
CssClass =labelCss> ;< / ASP:标签>
< / td>
< / tr>
< tr>
< td style =width:20%>
< label class =labelHead>日期< / label>
< / td>< td>:< / td>
< td style =width:80%>
< asp:Label ID =Label1runat =server
Text ='<%#Eval(ACTIVITY_DATE1)%>'
CssClass =labelCss> ;< / ASP:标签>
< / td>
< / tr>
< tr>
< td>
< label class =labelHead>互动类型< / label>
< / td>
< td>:< / td>
< td>
< asp:Label ID =Label2runat =server
Text ='<%#Eval(INTERFACE_DESCRIPTION)%>'
CssClass =labelCss> ;< / ASP:标签>
< / td>
< / tr>
< tr>
< td>
< label class =labelHead> Prospect< / label>
< / td>
< td>:< / td>
< td>
< asp:Label ID =Label3runat =server
Text ='<%#Eval(PROSPECT_DESCRIPTION)%>'
CssClass =labelCss> ;< / ASP:标签>
< / td>
< / tr>
< tr>
< td valign =top>
< label class =labelHead>注意< / label>
< / td>
< td valign =top>:< / td>
< td valign =top>
< asp:Label ID =Label4runat =server
Text ='<%#Eval(NOTES)%>'CssClass =labelCss>
< / asp:标签>
< / td>
< / tr>
< tr>
< td colspan =3>< hr size =1pxcolor =#D5DEA1
style =margin:10px 0px; />
< / td>
< / tr>
< / ItemTemplate>
< ItemStyle Font-Names =Trebuchet MS; />
< / asp:TemplateField>
< /列>
< EmptyDataTemplate>
< tr>
< td align =centerclass =gridViewNoRecords>
没有找到记录
< / td>
< / tr>
< / table>
< / EmptyDataTemplate>
< / asp:GridView>
< / div>

我将这个网格绑定到一个数据源。现在,我想从gridview的最后一个填充行中获取USER_ACTIVITIES_ID的值。我正在使用hiddenfield来保存此id并将其用于项目中使用javascript的其他目的。但是,我的问题是我没有获得ID的价值(无论是后端编码还是JavaScript)。任何人都可以告诉我在哪个事件或代码中,我可以得到这个值吗?请,我非常需要它。

解决方案

试试这个:

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


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.

解决方案

Try this:

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

这篇关于如何从gridview获取单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:19