在使用Gridview的更新面板中没有获取单元格值

时间:2015-08-21 06:15:46

标签: c#

我的问题是我在页面上使用了更新面板。有一个GridView,在这个数据绑定文本框的时间。我的问题是,如果我点击选择命令字段,我应该选择行单元格值。但是现在我只从选定行的指定单元格中选择索引没有值。我只从选定的行获得行索引。我需要进一步编码的单元格值。如果有人知道请分享您的知识。我记得你在我的页面上使用了更新面板。我需要获得选定的行值。我不关心哪个事件正在使用..

我的代码如下:

<asp:GridView ID="GridView1" style="margin-left:130px;" runat="server"
     AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" 
     BorderStyle="None" BorderWidth="1px" 
     CellPadding="3" CellSpacing="2" DataKeyNames="MachID" 
     onselectedindexchanged="GridView1_SelectedIndexChanged" AllowPaging="True" 
     PageSize="2" onselectedindexchanging="GridView1_SelectedIndexChanging" 
     onrowdatabound="GridView1_RowDataBound" 
     onrowcommand="GridView1_RowCommand">
     <Columns>
     <asp:TemplateField HeaderText="Punch No">
      <ItemTemplate>
        Punch <%#Container.DataItemIndex+1 %>
          </ItemTemplate>
             <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
             <%--  <asp:TemplateField>
              <ItemTemplate>
              <asp:Label ID="Label2" runat="server" Text="Punch"></asp:Label>
               </ItemTemplate>
               </asp:TemplateField>--%>
              <asp:BoundField DataField="MachID" Visible="False" />
              <asp:BoundField DataField="PunchTime" HeaderText="Punch Time" ReadOnly="True" />
                <asp:TemplateField Visible="False">
                     <ItemTemplate>
                       <asp:Label ID="lblhidden" runat="server" Text='<%# Eval ("Punchtime") %>'></asp:Label>
                 </ItemTemplate>
                   </asp:TemplateField>
                   <asp:CommandField SelectText="Edit" ShowSelectButton="True" />
                   <asp:TemplateField>
                    <ItemTemplate>
                          <asp:Button ID="btnEdit" runat="server" CommandName="edit" 
                             onclick="btnEdit_Click" Text="Edit" />
                     </ItemTemplate>
                       </asp:TemplateField>
                     </Columns>
                      <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                       <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                         <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                       <SortedAscendingCellStyle BackColor="#FFF1D4" />
                        <SortedAscendingHeaderStyle BackColor="#B95C30" />
                        <SortedDescendingCellStyle BackColor="#F1E5CE" />
                        <SortedDescendingHeaderStyle BackColor="#93451F" />
                       </asp:GridView>
           </td>

我的代码:

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

        //e.NewSelectedIndex
        int i = e.NewSelectedIndex;
        string data = GridView1.Rows[i].Cells[0].Text;       
        if (data != "")
        {

            txtpunchin.Text = data.ToString();
        }
    }

1 个答案:

答案 0 :(得分:0)

检查这个......

buttonclick()// this must be ur edit button
{
    Button btn= (Button)sender;
    GridViewRow grv = (GridViewRow)btn.NamingContainer;
    string name= grv.Cells[0].Text;

// once update your updated panel if it wont trigger any value, just like
    UpdatePanel1.Update();
}