我必须使用更新链接按钮更新我的gridview行,但单击按钮后gridview不会更新。
这是我在asp中的代码
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" GridLines="None" EmptyDatatext ="There are no records to display" OnRowEditing="EditViolation"
OnRowCancelingEdit="CancelEdit">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="Registry Number">
<ItemTemplate>
<asp:Label ID="lblRegNo" runat="server" Text='<%#Eval("registry_no")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vessel Name">
<ItemTemplate>
<asp:Label ID="lblVname" runat="server" Text='<%#Eval("ship_name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Violation">
<ItemTemplate>
<asp:Label ID="lblViolation" runat="server" Text='<%#Eval("violation")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox ID="txtViolation" runat="server" Text='<%#Eval("violation")%>'></asp:Textbox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Violated">
<ItemTemplate>
<asp:Label ID="lblDateViolated" runat="server" Text='<%#Eval("date_violated")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox ID="txtDateViolated" runat="server" Text='<%#Eval("date_violated")%>'></asp:Textbox>
<ajaxtoolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDateViolated">
</ajaxtoolkit:CalendarExtender>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Case Status">
<ItemTemplate>
<asp:Label ID="lblCaseStatus" runat="server" Text='<%#Eval("case_status")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="dropCase" runat="server" Text='<%#Eval("case_status")%>' >
<asp:ListItem Value = "0">N.A.</asp:ListItem>
<asp:ListItem Value = "1">Pending</asp:ListItem>
<asp:ListItem Value = "2">Disposed</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="false">
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server" CausesValidation="true"
Text="Update" onclick="btnupdate_Click" ></asp:LinkButton>
<asp:LinkButton ID="CancelEdit" runat="server" CausesValidation="false"
Text="Cancel" CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditViolation" runat="server" CausesValidation="false"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle CssClass="gridviewfooter"/>
<PagerStyle CssClass="gridviewfooter" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<RowStyle CssClass="gridviewrow"/>
<EmptyDataTemplate>There are no records to display.</EmptyDataTemplate>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:PhilfishPhilcatchConnectionString %>">
<InsertParameters>
<asp:Parameter Name="v_id" />
<asp:Parameter Name="violation" />
<asp:Parameter Name="date_violated" />
<asp:Parameter Name="case_status" />
</InsertParameters>
</asp:SqlDataSource>
这是我的代码
Public Sub bindData()
SqlDataSource2.InsertParameters.Item(0).DefaultValue = GridView1.SelectedDataKey.Value
SqlDataSource2.SelectCommand = "SELECT vessel_details.ship_name, vessel_details.registry_no, violation.violation, violation.date_violated, violation.case_status FROM vessel_details LEFT OUTER JOIN violation ON vessel_details.v_id = violation.v_id WHERE vessel_details.v_id ='" & GridView1.SelectedDataKey.Value & "'"
SqlDataSource2.DataBind()
GridView2.DataBind()
End Sub
Protected Sub EditViolation(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView2.EditIndex = e.NewEditIndex
bindData()
End Sub
Protected Sub CancelEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView2.EditIndex = -1
bindData()
End Sub
Protected Sub btnupdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txtViolation As TextBox = DirectCast(GridView2.FindControl("txtViolation"), TextBox)
Dim txtDateViolated As TextBox = DirectCast(GridView2.FindControl("txtDateViolated"), TextBox)
Dim dropCase As DropDownList = DirectCast(GridView2.FindControl("dropCase"), DropDownList)
Dim cmd As New SqlCommand()
cmd.CommandText = "Update violation set violation=@txtViolation, date_violated=@txtDateViolated, case_status=@dropCase where v_id = @v_id"
cmd.Parameters.AddWithValue("@v_id", GridView1.SelectedDataKey.Value)
cmd.Parameters.AddWithValue("@txtViolation", txtViolation.Text)
cmd.Parameters.AddWithValue("@txtDateViolated", txtDateViolated.Text)
cmd.Parameters.AddWithValue("@dropCase", dropCase.SelectedValue)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
bindData()
End Sub
我对gridview行的更新无效。 有什么建议?感谢。
答案 0 :(得分:0)
我发现您没有将命令与任何连接对象相关联。 你需要关联连接对象,需要在cmd.ExecuteNonQuery()之前打开。如下
Dim cmd As New SqlCommand()
Dim con As New SqlConnection(SqlDataSource2.ConnectionString)
cmd.Connection = con
cmd.CommandText = "Update violation set violation=@txtViolation, date_violated=@txtDateViolated, case_status=@dropCase where v_id = @v_id"
cmd.Parameters.AddWithValue("@v_id", GridView2.SelectedDataKey.Value)
cmd.Parameters.AddWithValue("@txtViolation", txtViolation.Text)
cmd.Parameters.AddWithValue("@txtDateViolated", txtDateViolated.Text)
cmd.Parameters.AddWithValue("@dropCase", dropCase.SelectedValue)
Try
con.Open()
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Catch ex As Exception
Finally
con.Close()
con.Dispose()
End Try