访问TemplateField控制asp.net中按钮单击处理程序中gridview的值

时间:2014-06-18 17:19:27

标签: asp.net vb.net gridview

单击gridview的编辑链接按钮后,我会在不在gridview内的不同文本框中显示数据。我有一个“重置”按钮,我想用它来恢复原始值。但我有问题访问按钮单击处理程序中的那些gridview数据并重置它。我尝试使用DirectCast(),但它显示System.NullReferenceException。

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
     <asp:TemplateField>
         <ItemTemplate>
             <asp:LinkButton ID="lblEdit" runat="server" CausesValidation="false" CommandName="editRecord" Text="EDIT" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
         </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
        <ItemTemplate>
             <asp:Label ID="lblRecordID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
         </ItemTemplate>
     </asp:TemplateField>
    <asp:TemplateField HeaderText="HANGER">
        <ItemTemplate>
            <asp:Label ID="lblHANGER" runat="server" Text='<%# Bind("HANGER") %>'></asp:Label>
        </ItemTemplate>
     </asp:TemplateField>
</Columns></asp:GridView>

后端vb.net代码是 -

Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        Dim vID As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
        Dim vHanger As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
        txtID.Text.Text = vID.Text()
        ddlHanger.SelectedValue = vHanger.Text 'dropdown list that's why selectedValue used

End Sub

我复制了部分代码,因为gridview有更多的行。如果有人请给我一个解决方案,我将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

首先删除以下内容:

<ItemTemplate>
    <asp:LinkButton ID="lblEdit" runat="server" CausesValidation="false"
         CommandName="editRecord" Text="EDIT" 
         CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</ItemTemplate>

并添加:

<asp:CommandField ButtonType="Button" ShowSelectButton="True" SelectText="EDIT" />

该标记将允许您的GridView具有 EDIT 按钮,该按钮将正确切换当前选定的行索引。您收到的 System.NullReferenceException 错误可能是因为GridView3.SelectedRow为NULL / EMPTY,这也意味着GridView3当前没有选择索引。

为确保GrieView3确实选择了ROW,您可以添加:以下右侧 AFTER

<SelectedRowStyle BackColor="Black" BorderColor="White" BorderStyle="Dotted" 
                  BorderWidth="3px" ForeColor="White" />

之后

</Columns>

所以你的最终GridView3标记应如下所示:

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
    <Columns>
        <asp:CommandField ButtonType="Button" ShowSelectButton="True" 
                          SelectText="EDIT"  />
        <asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
            <ItemTemplate>
                <asp:Label ID="lblRecordID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="HANGER">
            <ItemTemplate>
                <asp:Label ID="lblHANGER" runat="server" Text='<%# Bind("HANGER") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <SelectedRowStyle BackColor="Black" BorderColor="White" BorderStyle="Dotted" 
                      BorderWidth="3px" ForeColor="White" />
</asp:GridView>

然后你也可以使用TryCast,如下所示:

Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnReset.Click
    Dim vID As String = TryCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label).Text
    Dim vHanger As String = TryCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label).Text

    txtID.Text.Text = vID
    ddlHanger.SelectedValue = vHanger
End Sub