如何在搜索后更新gridview中的记录? [英] How to update the record in gridview after search?
问题描述
嗨
我正在尝试在gridview中搜索记录然后使用asp.net更新它(vb)
我正在使用这篇文章
http://www.aspsnippets .com / Articles / Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx [ ^ ]
但是当我按下更新时它没有更新记录,并且gridview消失了!!!
这里是girview脚本
Hi
I'm trying to search for a record in gridview then update it using asp.net(vb)
I'm using this article
http://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx[^]
but when I press update it didn't update the record and the gridview is disappeared!!!
here is the girview script
<div id = "dvGrid" style ="padding:10px;width:550px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns = "False" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor="LightGray"
HeaderStyle-BackColor ="Purple" ShowFooter = "True"
OnPageIndexChanging = "OnPaging" onrowediting="EditStudent"
onrowupdating="UpdateStudent" onrowcancelingedit="CancelEdit" Width ="100%">
<AlternatingRowStyle BackColor="LightGray" />
<Columns>
<asp:TemplateField HeaderText = "ID" >
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lblid" runat="server"
Text='<%# Eval("id")%>'></asp:Label>
</div>
</ItemTemplate>
<%-- <FooterTemplate>
<asp:TextBox ID="txtid"
runat="server" Width="20px"></asp:TextBox>
</FooterTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Name">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lblstname" runat="server"
Text='<%# Eval("stname")%>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtstname" runat="server"
Text='<%# Eval("stname")%>'></asp:TextBox>
</EditItemTemplate>
<%--
<FooterTemplate>
<asp:TextBox ID="txtstname" runat="server" Width="200px"></asp:TextBox>
</FooterTemplate> --%>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Birth Date">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lbldob" runat="server"
Text='<%# Eval("dob")%>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdob" runat="server"
Text='<%# Eval("dob")%>'></asp:TextBox>
</EditItemTemplate>
<%-- <FooterTemplate>
<asp:TextBox ID="txtdob" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate> --%>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Nationality">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lblnationality" runat="server"
Text='<%# Eval("nationality")%>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtnationality" runat="server"
Text='<%# Eval("nationality")%>'></asp:TextBox>
</EditItemTemplate>
<%-- <FooterTemplate>
<asp:TextBox ID="txtnationality" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Gender">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lblgender" runat="server"
Text='<%# Eval("gender")%>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtgender" runat="server"
Text='<%# Eval("gender")%>'></asp:TextBox>
</EditItemTemplate>
<%-- <FooterTemplate>
<asp:TextBox ID="txtgender" runat="server" Width="60px"></asp:TextBox>
</FooterTemplate> --%>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Level">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lbllevel" runat="server"
Text='<%# Eval("level")%>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtlevel" runat="server"
Text='<%# Eval("level")%>'></asp:TextBox>
</EditItemTemplate>
<%--<FooterTemplate>
<asp:TextBox ID="txtlevel" runat="server" Width="40px"></asp:TextBox>
</FooterTemplate> --%>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Section">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lblsect" runat="server"
Text='<%# Eval("sect")%>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtsect" runat="server"
Text='<%# Eval("sect")%>'></asp:TextBox>
</EditItemTemplate>
<%-- <FooterTemplate>
<asp:TextBox ID="txtsect" runat="server" Width="40px"></asp:TextBox>
</FooterTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText = "New Student" FooterStyle-Width="20px">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:Label ID="lblnewst" runat="server"
Text='<%# Eval("newst")%>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtnewst" runat="server"
Text='<%# Eval("newst")%>'></asp:TextBox>
</EditItemTemplate>
<%-- <FooterTemplate>
<asp:TextBox ID="txtnewst" runat="server" Width="40px"></asp:TextBox>
</FooterTemplate> --%>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Delete">
<ItemTemplate>
<div style="margin-removed auto; margin-removed auto; text-align: center;">
<asp:LinkButton ID="lnkRemove" runat="server"
CommandArgument = '<%# Eval("id")%>'
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" OnClick = "DeleteStudent"></asp:LinkButton>
</div>
</ItemTemplate>
<%-- <FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add"
OnClick = "AddNewStudent" />
</FooterTemplate> --%>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" HeaderText="Edit" />
</Columns>
<HeaderStyle BackColor="Purple" />
</asp:GridView>
<br />
<div style="width: 100%; overflow: scroll;">
</div>
<br />
<br />
<br />
<br />
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
</div>
here is the code
here is the code
Private Function GetData(ByVal cmd As SqlCommand) As DataTable
Dim dt As New DataTable()
Dim con As New SqlConnection(strConnString)
Dim sda As New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
sda.SelectCommand = cmd
sda.Fill(dt)
Return dt
End Function
Private Sub BindData()
Dim strQuery As String = "select * from stinfo where id = '" & txtno.Text & "'"
Dim cmd As New SqlCommand(strQuery)
GridView1.DataSource = GetData(cmd)
GridView1.DataBind()
End Sub
Protected Sub btnsearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Dim query As String
Dim SqlCommand As New SqlCommand
Dim reader As SqlDataReader
Dim adapter As New SqlDataAdapter
If RadioButton1.Checked = True Then
'Open the connection to db
conn.Open()
'Generating the query to fetch the contact details
' query = "select * from stinfo where id = '" & txtno.Text & "'"
query = "SELECT * from stinfo where id = '" & txtno.Text & "'"
SqlCommand.Connection = conn
SqlCommand.CommandType = CommandType.Text
SqlCommand.CommandText = query
'SqlCommand = New MySqlCommand(query, conn)
adapter.SelectCommand = New SqlCommand(query, conn)
'execute the query
reader = SqlCommand.ExecuteReader()
'Assign the results
GridView1.DataSource = reader
'Bind the data
GridView1.DataBind()
'search by name
ElseIf RadioButton2.Checked = True Then
'TextBox1.Text = ""
conn.Open()
query = "select * from stinfo where stname like '%" & txtname.Text & "%'"
SqlCommand.Connection = conn
SqlCommand.CommandType = CommandType.Text
SqlCommand.CommandText = query
'SqlCommand = New MySqlCommand(query, conn)
adapter.SelectCommand = New SqlCommand(query, conn)
'execute the query
reader = SqlCommand.ExecuteReader()
'Assign the results
GridView1.DataSource = reader
'Bind the data
GridView1.DataBind()
End If
End Sub
Protected Sub EditStudent(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
BindData()
End Sub
Protected Sub CancelEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
BindData()
End Sub
Protected Sub UpdateStudent(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
MsgBox("hhh")
Dim id As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("lblid"), Label).Text
Dim stname As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtstname"), TextBox).Text
Dim dob As String = DirectCast(GridView1.Rows(e.RowIndex) _
.FindControl("txtdob"), TextBox).Text
Dim nationality As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtnationality"), TextBox).Text
Dim gender As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtgender"), TextBox).Text
Dim level As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtlevel"), TextBox).Text
Dim sect As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtsect"), TextBox).Text
Dim newst As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtnewst"), TextBox).Text
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
' cmd.CommandText = "update stinfo set id=@id,stname=@stname," _
' & "dob=@dob, nationality=@nationality, gender=@gender,level=@level,sect=@sect,newst=@newst where id=@id;" _
'& "select stname,dob,nationality,gender,level,sect,newst from stinfo"
cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = id
cmd.Parameters.Add("@stname", SqlDbType.NVarChar).Value = stname
cmd.Parameters.Add("@dob", SqlDbType.NChar).Value = dob
cmd.Parameters.Add("@nationality", SqlDbType.NChar).Value = nationality
cmd.Parameters.Add("@gender", SqlDbType.NVarChar).Value = gender
cmd.Parameters.Add("@level", SqlDbType.NVarChar).Value = level
cmd.Parameters.Add("@sect", SqlDbType.NChar).Value = sect
cmd.Parameters.Add("@newst", SqlDbType.NVarChar).Value = newst
GridView1.EditIndex = -1
GridView1.DataSource = GetData(cmd)
GridView1.DataBind()
End Sub
Protected Sub DeleteStudent(ByVal sender As Object, ByVal e As EventArgs)
Dim lnkRemove As LinkButton = DirectCast(sender, LinkButton)
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "delete from cstinfo where " & _
"id=@id;" & _
"select id,stname,dob,nationality,gender,level,sect,newst from stinfo"
cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value _
= lnkRemove.CommandArgument
GridView1.DataSource = GetData(cmd)
GridView1.DataBind()
End Sub
Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
BindData()
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
How can I update record and keep the gridview appeared after updating?
How can I update record and keep the gridview appeared after updating?
推荐答案
这篇关于如何在搜索后更新gridview中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!