刷新GridView的问题 [英] Problem with refreshing gridview
本文介绍了刷新GridView的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每次我更新视频时,它都不会显示该视频,但是如果我只是键入随机字母,则每次更新时它都会显示.
基尔是我的gridview
every time i update my video, it doesnot show the video but if i just type random letter it does display everytime i update.
Gere is my gridview
<asp:GridView ID="GridView1" runat="server" CellPadding="4" Width="100%"
AutoGenerateColumns="False" OnRowEditing="EditRow" OnRowCancelingEdit="CancelEditRow" OnRowUpdating="UpdateRow"
DataKeyNames="Video_Number" AllowPaging="True" OnPageIndexChanging="ChangePage" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Video Link Name">
<ItemTemplate>
<%# Eval("Video_Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Width="770px" Height="50px" runat="server" Text='<%# Eval("Video_Name") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="true" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="true" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
这是我后面的代码
Here is my code behind
public void PopulateData()
{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(ConnString))
{
string sql = "SELECT * FROM VIDEO";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(table);
}
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
var autoID = GridView1.DataKeys[e.RowIndex].Value;
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
TextBox tVideo= row.FindControl("txtName") as TextBox;
//TextBox tManifesto = row.FindControl("txtManifesto") as TextBox;
//TextBox tName = row.FindControl("txtName") as TextBox;
//TextBox tLogo = row.FindControl("txtLogo") as TextBox;
//DropDownList dropActive = row.FindControl("dropActive") as DropDownList;
using (SqlConnection conn = new SqlConnection(ConnString))
{
//string sql = "Update PARTY set Party_Manifesto = @Party_Manifesto, " +
// "Party_Name = @Party_Name, Party_Logo = @Party_Logo, Status = @Status " +
// "WHERE Party_Number = @Party_Number";
string sql = "Update VIDEO set Video_Name = @Video_Name WHERE Video_Number = @Video_Number";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Video_Name", tVideo.Text.Trim());
cmd.Parameters.AddWithValue("@Video_Number", autoID);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lblMessage.Text = "Record updated sussesfully!";
GridView1.EditIndex = -1;
this.PopulateData();
}
请帮助
Please help
推荐答案
尝试一下,
在链接按钮中添加事件on_click,请参见下文.
Try this,
Add event on_click in linkbutton, see below.
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update" onclick="lnkUpdate_click" />
<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
然后在后面的代码中添加新的空白
Then add new void in behind code
protected void lnkUpdate_Click(object sender, EventArgs e)
{
var autoID = GridView1.DataKeys[e.RowIndex].Value;
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
TextBox tVideo= row.FindControl("txtName") as TextBox;
using (SqlConnection conn = new SqlConnection(ConnString))
{
string sql = "Update VIDEO set Video_Name = @Video_Name WHERE Video_Number = @Video_Number";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Video_Name", tVideo.Text.Trim());
cmd.Parameters.AddWithValue("@Video_Number", autoID);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lblMessage.Text = "Record updated sussesfully!";
GridView1.EditIndex = -1;
this.PopulateData();
}
问候,
亚历山大·安德里(Alexander Andri)
Regards,
Alexander Andri
这篇关于刷新GridView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文