我想在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>
</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>
</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>
</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>
</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中只有一个模板字段。这是否意味着我只有一行和一个单元格?
非常感谢你。答案 0 :(得分:0)
您需要在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");