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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24291543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:43:13  来源:igfitidea点击:

Accessing TemplateField controls value of gridview inside button click handler in asp.net

asp.netvb.netgridview

提问by saz

After clicking the edit linkbutton of my gridview, I show the data on different text boxes which are not inside the gridview. I have a "Reset" button which i want to use to get back to the original values. But I am having problem to access those gridview data inside the button click handler and reset it.I tried using DirectCast() but its showing System.NullReferenceException.

单击我的 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>

The backend vb.net code is-

后端 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

I have copied the portion of the code cause the gridview has lot more rows. I would appreciate if anyone please show me a solution.Thanks in advance.

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

回答by lucidgold

First remove the following:

首先删除以下内容:

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

And add:

并添加:

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

That markup will allow your GridView to have an EDITbutton which will switch the current selected row index correctly. System.NullReferenceExceptionerror you are recieving could be because the GridView3.SelectedRow is NULL/EMPTY, which also means GridView3 currently has NO selected index.

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

To make sure that GrieView3 indeed selected a ROW, you can add: the following right AFTERthe

为了确保GrieView3确实选择一排,你可以添加:下面的右

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

right after

紧随其后

</Columns>

So your final GridView3 markup should look like:

所以你最终的 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>

Then you can also use TryCast too, like so:

然后您也可以使用 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