[英]UpdatePanel Gridview not updating
出於某種原因,在我做出更改后,我無法讓Updatepanel中的Gridview刷新。 有人可以幫忙嗎?
我正在使用ToolkitScriptManager控件和UpdatePanel。
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView blah...
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DeleteButton" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="IBUpUp" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="IBDownDown" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="IBUp" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="IBDown" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="EditProfile" EventName="Click" />
</Triggers>
Cs
protected void Unnamed3_Click(object sender, ImageClickEventArgs e)
{
int rowIndex = GridView1.SelectedIndex;
GridViewRow gvr = GridView1.SelectedRow;
if (rowIndex >= 0)
{
//delete
String GridViewOne = GridView1.DataKeys[rowIndex].Value.ToString();
//delete image
string imagename = gvr.Cells[2].Text;
string pathToImage = @"C:\Images\";
pathToImage = pathToImage + imagename;
if (System.IO.File.Exists(pathToImage))
{
// Use a try block to catch IOExceptions, to
// handle the case of the file already being
// opened by another process.
try
{
System.IO.File.Delete(pathToImage);
}
catch (System.IO.IOException m)
{
Console.WriteLine(m.Message);
return;
}
}
int bannerid = Convert.ToInt32(GridViewOne);
SqlDataReader sdr = null;
SqlConnection conn = GetConnection();
SqlCommand cmd = new SqlCommand("Tool_DeleteBannerAds", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@BannerID";
param1.Value = bannerid;
cmd.Parameters.Add(param1);
conn.Open();
sdr = cmd.ExecuteReader();
sdr.Close();
UpdatePanel1.Update();
GridView1.DataBind();
}
else
{
//don't do anything
//keep
//Response.Redirect("Default.aspx");
}
}
改變順序:
GridView1.DataBind();
UpdatePanel1.Update();
這是我的問題代碼
ASPX文件
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Name List" DataField="EmpName" />
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
</form>
</body>
文件背后的代碼
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var Employee = new { EmpID = 1, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" };
var customerList = (new[] { Employee }).ToList();
customerList.Add(new { EmpID = 2, EmpName = "Sheetal Jain", Department = "IT", Age = 33, Address = "Hello" });
GridView1.DataSource = customerList;
GridView1.DataBind();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
try
{
var Employee = new { EmpID = 1, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" };
var customerList = (new[] { Employee }).ToList();
customerList.Add(new { EmpID = 2, EmpName = "Sheetal Jain", Department = "IT", Age = 33, Address = "Hello" });
customerList.Add(new { EmpID = 2, EmpName = "Minakshi Jain", Department = "IT", Age = 33, Address = "Hello" });
GridView1.DataSource = customerList;
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
本代碼Dosent為我創建了回發希望它也能為你工作....
作為替代方案,您可以將UpdateMode參數設置為“Always”(這是默認值),以允許任何觸發回發的Web控件自動更新GridView。
這為您節省了幾行代碼(配置觸發器),但如果您動態地向GridView添加控件,這也會有助於您觸發頁面上的回發事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.