【问题标题】:Explicit iteration in Gridview and using findControl returns NothingGridview 中的显式迭代并使用 findControl 返回 Nothing
【发布时间】:2020-12-30 19:08:10
【问题描述】:

我正在尝试遍历 GridView 控件。它包含单选按钮,我想根据特定条件设置它的选中属性。但是,findControl 方法不返回任何内容。

这是aspx代码:

<asp:GridView ID="GridViewInfo"
                runat="server"
                AutoGenerateColumns="False"
                DataKeyNames="Result_ID">
    <Columns>
        <asp:TemplateField HeaderStyle-Width="3%" HeaderText="SELECT">
            <ItemTemplate>
                <input name="RadioButtonResultID" 
                       id="RadioButtonResultID" type="radio" 
                       value='<%# Eval("Result_ID") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Characteristics" 
                        DataField="Characteristics" />
    </Columns>
</asp:GridView>

后面的代码:

    Private Sub HighlightSelectedRow(ByVal id As String)
        Dim rowCount As Int32 = 0

        For Each row As GridViewRow In GridViewLabInfo.Rows
            If (GridViewLabInfo.DataKeys(rowCount).Value.ToString() = id) Then
                row.CssClass = "SelectedRowStyle"

                'Both of the below lines are failing
                TryCast(row.FindControl("RadioButtonResultID"), RadioButton).Checked = True
                CType(row.FindControl("RadioButtonResultID"), RadioButton).Checked = True

            End If
            rowCount = rowCount + 1
        Next
    End Sub

【问题讨论】:

  • 为什么我们不能在这里设置检查值?请提供任何参考。
  • 在该控件的标签中放置一个 runat=server。

标签: asp.net vb.net


【解决方案1】:

你投错了。那不是asp.net单选按钮(RadioButton),而是HTML单选按钮,

所以在这种情况下你的演员应该是:

TryCast(row.FindControl("RadioButtonResultID"), HtmlInputRadioButton ).Checked = True

而且你甚至不需要演员表,但这应该可以:

Dim MyRadioBut As HtmlInputRadioButton = row.FindControl("RadioButtonResultID")
MyRadioBut.Checked = True

但是,在上述两种情况下?除非您声明该控件是由服务器呈现的,否则您无法在后面的代码中获取控件。

您需要添加 runat="server"。

例如:

 <input name="RadioButtonResultID" 
                   id="RadioButtonResultID" type="radio" 
                   value='<%# Eval("Result_ID") %>' runat="server" />

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 2013-03-16
    • 2012-10-31
    • 2010-10-05
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多