繁体   English   中英

如何在GridView中找到控件

[英]how to find control in gridview

我想在我的gridview中访问我的O1控件,这是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>

和即时通讯使用此代码来访问它:

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中,只有一个模板字段。 这是否意味着我只有一行和一个单元格?

非常感谢你。

您需要在gridview上处理RowCommand事件,因为按钮单击事件会上升到gridview控件。

<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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM