2012-06-07 151 views
0

我想从GridView的单元格的值,但空字符串返回。我是在执行的RadioButtonList的SelectedIndexChanged事件
代码通过gridview的 和接入小区的代码。我重复。但问题是stll remaining.I使用了三个ItemTemplate中,每个 有一个elemnt使每个元素获得自己的coulmn的.aspx获取标签文本

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



       <Columns> 
<asp:TemplateField> 
    <itemtemplate> 
       <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:TemplateField> 
    <itemtemplate> 
      <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> 
    </itemtemplate> 
    </templatefield> 
       </Columns> 

      </asp:GridView> 

     <asp:Label ID="Labe11" runat="server" ></asp:Label> 
     Code behind: public void changed(object sender, 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; 


       } 



        } 

回答

0

你已经囊括了所有从上面的.aspx页面的HTML?你有什么没有工作。 ItemTemplates不形成列,它们被包括在的TemplateField列中

示例(改编自你的代码,
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.itemtemplate.aspx
http://msdn.microsoft.com/en-us/library/aa479353.aspx):

<asp:GridView ID="GridView1" Runat="server" 
    <Columns> 

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

     <asp:TemplateField HeaderText="Choice"> 
      <ItemTemplate> 
       <asp:RadioButtonList ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server" OnSelectedIndexChanged="changed" AutoPostBack="true" > 
        <asp:ListItem Value="agree" Selected="True" /> 
        <asp:ListItem Value="disagree" /> 
        <asp:ListItem Value="strongagree" /> 
        <asp:ListItem Value="strondisagree" /> 
       </asp:RadioButtonList> 
      </ItemTemplate> 
     </asp:TemplateField> 

    </Columns> 
</asp:GridView> 

你需要为每列一个单独的TemplateField的定义。

+0

我已经使用单独的但问题依旧 – user1405508

0

请改为尝试这个办法:

GridView2.Rows[i].FindControl("RadioButtonList1") as RadioButtonList; 

问候, 西瓦库玛