2012-06-07 159 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; 

問候, 西瓦庫瑪