2013-11-28 76 views
0

我要訪問我的O1控制在我的GridView 這裏是使用此代碼訪問它在GridView如何找到在GridView控件

<asp:GridView ID="SelectedPollGridView" runat="server" Width="100%" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="PollID" DataSourceID="SelectedPollSqlDataSource" ForeColor="Black" GridLines="Horizontal"> 
        <Columns> 
         <asp:TemplateField> 
          <HeaderTemplate> 
           <p class="text-center"><small><%#Eval("Header") %></small></p> 
          </HeaderTemplate> 
          <ItemTemplate> 
           <p class="text-right"><%#Eval("Body") %></p> 
           <div class="text-right"> 
            <div runat="server" id="O1Div" visible='<%#Eval("O1Vis") %>' class="radio "> 
             <label> 
              <input class="pull-right" type="radio" name="optionsRadios" id="O1" value="option1"> 
              <%#Eval("O1") %> 
             </label> 
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </div> 
            <div runat="server" id="O2Div" visible='<%#Eval("O2Vis") %>' class="radio"> 
             <label> 
              <input class="pull-right" type="radio" name="optionsRadios" id="O2" value="option2"> 
              <%#Eval("O2") %> 
             </label> 
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </div> 
            <div runat="server" id="O3Div" visible='<%#Eval("O3Vis") %>' class="radio"> 
             <label> 
              <input class="pull-right" type="radio" name="optionsRadios" id="O3" value="option3"> 
              <%#Eval("O3") %> 
             </label> 
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </div> 
            <div runat="server" id="O4Div" visible='<%#Eval("O4Vis") %>' class="radio"> 
             <label> 
              <input class="pull-right" type="radio" name="optionsRadios" id="O4" value="option4"> 
              <%#Eval("O4") %> 
             </label> 
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </div> 
            <asp:Button CssClass="btn btn-info" ID="SubmitPollButton" runat="server" Text="ثبت نظر" OnClick="SubmitPollButton_Click" /> 
           </div> 
          </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 

       </asp:GridView> 

和IM:

protected void SubmitPollButton_Click(object sender, EventArgs e) 
    { 
     System.Web.UI.HtmlControls.HtmlGenericControl O1Radio = (System.Web.UI.HtmlControls.HtmlGenericControl)SelectedPollGridView.Rows.FindControl("O1"); 
     if (O1Radio.Attributes["checked"] == "checked") 
     { 
      Response.Redirect("somewhere"); 
     }    

    } 

但不起作用。 可以幫助我嗎? 在我的GridView中只有一個模板字段。這是否意味着我只有一行一格?

非常感謝。

+1

在你試圖得到控制什麼方法? – Nacho

+0

System.Web.UI.HtmlControls.HtmlGenericControl O1Radio =(System.Web.UI.HtmlControls.HtmlGenericControl)SelectedPollGridView.Rows [0] .FindControl(「O1」); –

+0

添加'runat =「server」'標籤:' afzalulh

回答

0

由於按鈕單擊事件冒泡到gridview控件,您需要處理gridview上的RowCommand事件。

<asp:GridView id="GridView1" AutoGenerateColumns="false" 
    OnRowCommand="OnGridRowCommand" runat="server"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Button ID="btn" 
        CommandArgument="<%# Eval("ID") %>" 
        Text="Click" CommandName="foo" runat="server"/> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

後面的代碼:

protected void OnGridRowCommand(object sender, GridViewEventArgs e) 
{ 
    //e.CommandSource - reference to the button 
    //e.CommandName - the command name, in this case foo 
    //e.CommandArgument - use it to find which row the button click originated from 


} 

現在,如果你想找到你的其他控件做這樣的事情:

System.Web.UI.HtmlControls.HtmlGenericControl O1Radio = 
    (System.Web.UI.HtmlControls.HtmlGenericControl) 
     ((Button)e.CommandSource).Parent.FindControl("O1"); 
+0

我是新來的asp.net,非常感謝你的回答,我想我得試試這幾個小時才能得到答案。 –

+0

那麼如果我想使用自動回發單選按鈕並刪除按鈕? 非常感謝你 –

+0

@afzalulh我的不好解決它 – deostroll