如何在不使用数据源的情况下使用updatepanel中的gridview进行排序? [英] How to do sorting using gridview within updatepanel without using datasource?
问题描述
您好,
我这里有问题。我可以在没有插入SQL数据源的情况下对gridview进行排序。但是,当我将排序代码插入updatepanel时,它给了我dopostback的问题。这是我的代码:
aspx
Hello,
I have a problem here. I am able to do sorting for gridview without insert SQL datasource. However, when i insert the sorting codes into within updatepanel, it give me the problem of dopostback. Here are my codes:
aspx
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView7" runat="server" AutoGenerateColumns="False"
DataKeyNames="DetailsId" GridLines="Horizontal"
OnRowCommand="GridView7Data_RowCommand" EnableSortingAndPagingCallbacks ="false"
onpageindexchanging="GridView7_PageIndexChanging" AllowPaging="True"
AllowSorting="True" onsorting="GridView7_Sorting">
<Columns>
<asp:ButtonField CommandName="editRecord" ControlStyle-CssClass="btn"
ButtonType="Button" Text="Edit" HeaderText="Edit Record">
<ControlStyle CssClass="btn"></ControlStyle>
</asp:ButtonField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="AdminNo" HeaderText="Admin No"
SortExpression="AdminNo" />
<asp:BoundField DataField="FName" HeaderText="File Name" />
<asp:TemplateField HeaderText="Download Link"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload7" runat="server"
CommandArgument='<%# Eval("DetailsId") %>' OnClick="DownloadFile7"
Text="Download"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image1" ItemStyle-Height="150"
ItemStyle-Width="170">
<EditItemTemplate>
<asp:TextBox ID="TextBox24" runat="server" Text='<%# Eval("image") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image19" runat="server"
ImageUrl='<%# Eval("image", GetUrl("{0}")) %>' />
</ItemTemplate>
<ControlStyle Height="170px" Width="150px" />
<ItemStyle Height="170px" Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image2">
<EditItemTemplate>
<asp:TextBox ID="TextBox25" runat="server" Text='<%# Eval("image2") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image20" runat="server"
ImageUrl='<%# Eval("image2", GetUrl("{0}")) %>' />
</ItemTemplate>
<ControlStyle Height="170px" Width="150px" />
<ItemStyle Height="170px" Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image3">
<EditItemTemplate>
<asp:TextBox ID="TextBox26" runat="server" Text='<%# Eval("image3") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image21" runat="server"
ImageUrl='<%# Eval("image3", GetUrl("{0}")) %>' />
</ItemTemplate>
<ControlStyle Height="170px" Width="150px" />
<ItemStyle Height="170px" Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image4" SortExpression="image4">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("image4") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image3" runat="server" ImageUrl='<%# Eval("image4", GetUrl("{0}")) %>' />
</ItemTemplate>
<ControlStyle Height="170px" Width="150px" />
<ItemStyle Height="170px" Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image5" SortExpression="image5">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("image5") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image4" runat="server" ImageUrl='<%# Eval("image5", GetUrl("{0}")) %>' />
</ItemTemplate>
<ControlStyle Height="170px" Width="150px" />
<ItemStyle Height="170px" Width="150px" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<EmptyDataTemplate>
<asp:Label ID="Label29" runat="server" ForeColor="Red" Text="No Record Found"></asp:Label>
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#FFFFCC" Font-Bold="True" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#000000" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
aspx.cs
aspx.cs
public partial class PopGraduateTracking : System.Web.UI.Page
{
string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Login"] != "True")
{
Response.Redirect("Login.aspx");
}
if (!Page.IsPostBack)
{
BindGrid7();
}
this.RegisterPostBackControl();
}
private void RegisterPostBackControl()
{
foreach (GridViewRow row in GridView7.Rows)
{
LinkButton lnkDownload7 = row.FindControl("lnkDownload7") as LinkButton;
ScriptManager.GetCurrent(this).RegisterPostBackControl(lnkDownload7);
}
}
private void BindGrid7()
{
string strConnectionString = ConfigurationManager.ConnectionStrings[\"ConnectionString\"].ConnectionString;
//create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//Retrieve Statement
String strCommandText = \"select * from GraduateTracks WHERE Name LIKE @Name\";
// \"SELECT od.productID, SUM(od.Quantity) AS Quantity, p.productName, p.unitPrice FROM orderDetails AS od INNER JOIN product AS p ON od.productID = p.productID WHERE p.productName = @productName GROUP BY od.productID, p.productName, p.unitPrice \";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.Add(\"@Name\", SqlDbType.VarChar, 50);
cmd.Parameters[\"@Name\"].Value = \"%\" + TextBox23.Text + \"%\";
myConnect.Open();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
GridView7.DataSource = dt;
GridView7.DataBind();
ViewState[\"dt\"] = dt;
ViewState[\"sort\"] = \"Asc\";
reader.Close();
myConnect.Close();
}
private void BindGrid7()
{
string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//Retrieve Statement
String strCommandText = "select * from GraduateTracks WHERE Name LIKE @Name";
// "SELECT od.productID, SUM(od.Quantity) AS Quantity, p.productName, p.unitPrice FROM orderDetails AS od INNER JOIN product AS p ON od.productID = p.productID WHERE p.productName = @productName GROUP BY od.productID, p.productName, p.unitPrice ";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50);
cmd.Parameters["@Name"].Value = "%" + TextBox23.Text + "%";
myConnect.Open();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
GridView7.DataSource = dt;
GridView7.DataBind();
ViewState["dt"] = dt;
ViewState["sort"] = "Asc";
reader.Close();
myConnect.Close();
}
protected void GridView7_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtResult = (DataTable)ViewState["dt"];
if (dtResult.Rows.Count > 0)
{
if (Convert.ToString(ViewState["Sort"]) == "Asc")
{
dtResult.DefaultView.Sort = e.SortExpression + " Desc";
//ViewState["sort"] == "Desc";
}
else
{
dtResult.DefaultView.Sort = e.SortExpression + " Asc";
//ViewState["sort"] == "Asc";
}
GridView7.DataSource = dtResult;
GridView7.DataBind();
}
推荐答案
Take a look at the answer here - Implement Sorting of GridView inside AJAX UpdatePanel in ASP.Net[^].
Take a look at the answer here - Implement Sorting of GridView inside AJAX UpdatePanel in ASP.Net[^].
这篇关于如何在不使用数据源的情况下使用updatepanel中的gridview进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!