【问题标题】:getting cell value of gridview获取gridview的单元格值
【发布时间】:2012-06-06 17:25:55
【问题描述】:

我想从gridview获取单元格值,但是返回的是空字符串。我实现了 单选按钮列表的 selectedindexchanged 事件中的所有代码。我遍历 gridview
并通过代码访问单元格。但问题仍然存在。我使用了三个项目模板,每个
有一个元素,因此每个元素都有自己的库 .aspx

        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" >

       <Columns>

        <asp:Label ID="Label2" runat="server" Text='<%# Eval("qno") %>'>

   </asp:Label>
       </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField>
  <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Eval("description") 

%>'>

  </ItemTemplate>
  </asp:TemplateField>

  <asp:RadioButtonList ID="RadioButtonList1" RepeatDirection="Horizontal" 
   runat="server" OnSelectedIndexChanged="changed"  AutoPostBack="true" >


     <asp:ListItem   Value="agree" Selected="True" >

     </asp:ListItem>
       <asp:ListItem 
        Value="disagree">

     </asp:ListItem>
       <asp:ListItem Value="strongagree">

     </asp:ListItem>
       <asp:ListItem Value="strondisagree">

     </asp:ListItem>
        </asp:RadioButtonList>

        </Columns>

   </asp:GridView>

 <asp:Label ID="Labe11" runat="server" ></asp:Label>

背后的代码: 公共无效更改(对象发送者,EventArgs e) {

      for(int i=0;i<GridView2.Rows.Count;i++)
      {
          string labtext;
            RadioButtonList list = 
    GridView2.Rows[i].Cells[2].FindControl("RadioButtonList1") as RadioButtonList;
           labtext= GridView2.Rows[i].Cells[0].Text;


           Label1.Text = labtext;


      }



            }

【问题讨论】:

    标签: .net gridview


    【解决方案1】:

    我认为您正在寻找网格视图的 RowUpdating 事件。下面的代码来自我几年前从事的一个项目,我必须从网格视图中的组合框中获取值。希望这能让您更接近解决方案。

        protected void gvReconciliation_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
             DropDownList dropDownListUser = gvReconciliation.Rows[e.RowIndex].FindControl("ddlSourceEdit") as DropDownList;
             e.NewValues["source"] = dropDownListUser.SelectedItem.Text;
        }
    

    我的控件绑定到一个对象数据源,这就是为什么您会在其中看到e.NewValues["source"]。 “Source”是我的 ObjectDataSource 中绑定的列名。

    【讨论】:

    • 但我想获取 label2 的文本
    • 您应该能够将 label2 及其类型插入到上面的代码中,或者非常接近它的东西并得到您需要的东西。如果您需要特定的代码,如果您需要那么多帮助,我可能会为您编写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    相关资源
    最近更新 更多