根据CommandArgument在子网格上获取数据 [英] Getting Data on a child grid based on CommandArgument

查看:85
本文介绍了根据CommandArgument在子网格上获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,开发人员/程序员,在这里遇到了一些问题.单击图像按钮时,无法从子GridView中获取数据键.

使用我的代码,我收到一个错误提示
Object reference not set to an instance of an object.
指向该区域

Hello fellow developers/programmers, got some problems here. I cannot get the datakey from my child gridview when i click the imagebutton.

Using my code, im getting an error saying
Object reference not set to an instance of an object.
Pointing on this area

Dim row As GridViewRow = DirectCast(ImageDelete.NamingContainer, GridViewRow)


您能帮我调试这个问题吗?非常感谢


Could you help me debug this problem.? Many thanks

    <asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False"
        Width="900px" DataKeyNames="CourseCatID" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" CellPadding="4" ForeColor="#333333" GridLines="None">
    <columns>
    <asp:BoundField  HeaderText = "CourseCatID" DataField="CourseCatID" />

    <asp:TemplateField HeaderText="Course Category">
    <itemtemplate>
    <a href="java<!-- no -->script:toggleDiv(''mydiv<%# Eval(" coursecatid=")%>'')"><asp:Image ID="img" onclick="javascript:Toggle(this);" runat="server" ImageUrl="~/Images/plus.gif"
    ToolTip="Expand" Width="7px" Height="7px" ImageAlign="AbsMiddle"/></a>
    <asp:Label ID="lbllastname" Height="15px" runat="server" Text=''<%# Eval("CourseCatName")%>''>
    <div id="mydiv<%# Eval(" coursecatid=")%>" style="display:none">
    <br />      
    <asp:ImageButton ID="ImageAdd" Height="17px" ImageUrl="Images/addCourse.png" runat="server" CommandName="cmdAdd" CommandArgument=''<%# Eval("CourseCatID") %>'' OnClick="ImageAdd_click"
    /><br /><br />

            <asp:GridView ID="grdCourse" runat="server"  Width="800px" HorizontalAlign="Right" AutoGenerateColumns="False" align="center" DataKeyNames="CourseCode" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" BackColor="White"
            GridLines="Vertical">
            <columns>
            <asp:TemplateField HeaderText="CourseName" HeaderStyle-HorizontalAlign="Left">
             <itemtemplate>
                   <asp:Label ID="lblCoursename" runat="server" Text=''<%# Eval("CourseName")%>'' Width="300px" Height="10px">

             </itemtemplate>

           <asp:TemplateField HeaderText="Course Code" HeaderStyle-HorizontalAlign="Left">
             <itemtemplate>
                   <asp:Label ID="lbllastname" runat="server" Text=''<%# Eval("CourseCode")%>'' Height="10px">
            </itemtemplate>


         <asp:BoundField HeaderText = "Duration" DataField="Duration" HeaderStyle-HorizontalAlign="Left" />

         <asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Left" >
             <itemtemplate>
                  <asp:ImageButton ID="ImageLink" Height="15px" ImageUrl="Images/edit.png" runat="server" CommandName="cmdlink" PostBackUrl=''<%#"CourseUpdate.aspx?CourseName=" & Eval("CourseName")%>'' />
             </itemtemplate>
             <itemstyle horizontalalign="Left" />


         <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left"  >
             <itemtemplate>
                 <asp:ImageButton ID="ImageDeleteCourse"  Height="15px" ImageUrl="Images/Delete.png" runat="server" CommandName="cmdDelete" CommandArgument=''<%# Eval("CourseCode") %>'' onclick="ImageDeleteCourse_Click"
                    />
             </itemtemplate>
             <itemstyle horizontalalign="Left" />


        <asp:CommandField ShowDeleteButton="True"  />
        </columns>
        <rowstyle backcolor="White" forecolor="#333333" />
        <footerstyle backcolor="White" forecolor="#333333" />
        <pagerstyle backcolor="#336666" forecolor="White" horizontalalign="Center" />
        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

  </div>


 </itemtemplate>




 <asp:TemplateField HeaderText="Delete" >
  <itemtemplate>
  <asp:ImageButton ID="ImageDelete" Height="15px" ImageUrl="Images/Delete.png"
   runat="server" CommandName="cmdDelete"
   CommandArgument=''<%# Eval("CourseCatID") %>'' Width="16px" onclick="ImageDelete_Click"
  /><br />
  </itemtemplate>
  <itemstyle verticalalign="Top" />

  <asp:CommandField ShowDeleteButton="True" />
  <asp:TemplateField>
  <itemtemplate>

  </itemtemplate>


 </columns>



<rowstyle backcolor="#EFFDFF" forecolor="#333333" />
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
<pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<editrowstyle backcolor="#999999" forecolor="White" />
<alternatingrowstyle backcolor="White" forecolor="#333333" verticalalign="Middle" />

如您所见,它是一个嵌套的gridview

现在这是我在服务器端的代码

As you see it is a nested gridview

Now here is my code on my server side

Protected Sub ImageDeleteCourse_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
       Dim ImageDelete As ImageButton = CType(grdCategory.Rows(0).FindControl("ImageDeleteCourse"), ImageButton)
       Dim row As GridViewRow = DirectCast(ImageDelete.NamingContainer, GridViewRow)
       Dim courseCode As String = Convert.ToString(Me.grdCategory.DataKeys(row.RowIndex).Value)
       Dim coursecode2 As String = courseCode
   End Sub



我已经更新了代码,但仍然无法获取数据键,因为该数据键正在使用ParentGrid(使用断点)
我认为我在这里遇到错误



Ive Updated my codes but still cant get datakeys cause the datakey im getting is on the ParentGrid(Using breakpoint)
I think im getting error here

Dim courseCode As String = Convert.ToString(Me.grdCategory.DataKeys(row.RowIndex).Value)


先生,您有任何解决方案吗?


Any Solution sir?

For i As Integer = 0 To grdCategory.Rows.Count - 1
           Dim gvChild As GridView = DirectCast(grdCategory.Rows(i).Cells(0).FindControl("grdCourse"), GridView)
           Dim ImageDelete As ImageButton = CType(gvChild.Rows(0).FindControl("ImageDeleteCourse"), ImageButton)
           Dim row As GridViewRow = DirectCast(ImageDelete.NamingContainer, GridViewRow)
           Dim courseCode As String = Convert.ToString(Me.grdCategory.DataKeys(row.RowIndex).Value)
       Next

推荐答案

确保从您的VB代码中获取的Gridview数据中没有空值.在零件上放置一个断点
Make sure that from your VB codes, you are not getting a null value from retrieval of data from your Gridview. Put a break point on part
Dim ImageDelete As ImageButton = CType(grdCategory.Rows(0).FindControl("ImageDeleteCourse"), ImageButton)


然后开始调试!希望你能明白.

问候,
爱德华


then start debugging! Hope you get it.

Regards,
Eduard


这篇关于根据CommandArgument在子网格上获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆