在gridview行更新后获取值

时间:2010-06-29 10:16:31

标签: .net asp.net gridview

我有一个显示问题列表的gridview。只有一个字段是可编辑的,如果信息已被编辑,则会将电子邮件发送到特定的“命运”。一切正常,但当我尝试在我的SendEmail函数中获取问题ID(使用Findcontrol)时,我得到了 “你调用的对象是空的。”我如何获得问题ID?

我的sqldatasource:

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>" SelectCommand="SELECT Questions.QuestionID, Questions.Name, Questions.Question, Questions.Destiny, Questions.DateQuestion, Questions.Answer" UpdateCommand="UPDATE Questions SET Destiny = @Destiny WHERE (QuestionID = @QuestionID)">
    <SelectParameters>
   </SelectParameters>
     <UpdateParameters>
        <asp:Parameter Name="Destiny" />
        <asp:Parameter Name="QuestionID" />
    </UpdateParameters>
</asp:SqlDataSource>

我的网格视图:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="QuestionID" OnRowUpdated="GridView1_RowUpdated"
    DataSourceID="SqlDataSource1">
    <Columns>

        <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="QuestionID">
          <ItemTemplate>
                <asp:Label ID="LabelQuestionID" runat="server" Text='<%# Bind("QuestionID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>    

   <asp:TemplateField HeaderText="Question" InsertVisible="False" SortExpression="Question">
          <ItemTemplate>
                <asp:Label ID="LabelQuestion" runat="server" Text='<%# Bind("Question") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField> 

     <asp:TemplateField HeaderText="Date" SortExpression="DateQuestion">
                         <ItemTemplate>
                <asp:Label ID="LabelDateQuestion" runat="server" Text='<%# Bind("DateQuestion", "{0:g}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>



        <asp:TemplateField HeaderText="Destiny" SortExpression="Destiny">
                           <ItemTemplate>
                <asp:Label ID="LabelDestiny" runat="server" Text='<%# Bind("Destiny") %>'></asp:Label>

            </ItemTemplate>

            <EditItemTemplate>
            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Destiny") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>



        <asp:TemplateField ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update"></asp:LinkButton>
                <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>




      </Columns>
</asp:GridView>

我的代码背后:

    Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) 

    If Trim(e.OldValues("Destiny").ToString <> Trim(e.NewValues("Destiny").ToString)) Then
    QuestionID  = GridView1.FindControl("LabelQuestionID").ToString
    SendEmailtoAdviser(Trim(e.NewValues("Destiny")), QuestionID)
    End If
    End Sub

2 个答案:

答案 0 :(得分:0)

您的第一步应该是分解代码以分隔行,以找出哪个语句返回空引用:

QuestionControl = GridView1.FindControl("LabelQuestionID")
QuestionID = QuestionControl.ToString

Recipient = e.NewValues("Destiny")
SendEmailtoAdviser(Trim(Recipient), QuestionID)

这将告诉您是否未找到问题控件或收件人不在事件参数中。

然后你可以找出未正确设置的内容。

答案 1 :(得分:0)

如果使用以下代码完成更新,我可以在gridview中获取特定单元格的值。在我的情况下,我想检索ID,这是我的gridview中的第一列。

 Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs)
    If Trim(e.OldValues("Destiny").ToString <> Trim(e.NewValues("Destiny").ToString)) Then
        Dim index As Integer = GridView1.EditIndex
        Dim row As GridViewRow = GridView1.Rows(index)
        SendEmailtoAdviser(Trim(e.NewValues("Destiny").ToString), row.Cells(0).Text)
    End If
 End Sub