繁体   English   中英

GridView排序在UpdatePanel中不起作用

[英]Gridview sorting not working in updatepanel

<div style="width: 90%; margin-right: 5%; margin-left: 5%; text-align: center">
    <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>

上面是我的Gridview表的代码。 目前,我的问题是我的排序工作不正常。 但是,将其放置在Updatepanel之外时,它可以工作。

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();
        }
    }

这是我进行排序的代码。 任何帮助和建议,将不胜感激。 谢谢!

这是错误

System.FormatException was unhandled by user code
HResult=-2146233033
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ToInt32(Object value)
   at StarsWebSite.PopGraduateTracking.GridView7Data_RowCommand(Object sender, GridViewCommandEventArgs e) in C:\Users\133214J\Desktop\latesttt\StarsWebSite\StarsWebSite\PopGenGraduateTracking.aspx.cs:line 109
   at System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs e)
   at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
   at System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: 

这是我的rowCommand方法

protected void GridView7Data_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName.Equals("editRecord"))
        {
            lblID.Text = GridView7.DataKeys[index].Value.ToString();
            DataTable dt1 = new DataTable();
            dt1 = GetData("select FamilyName,Name,Handphone,EmailAddress,Talent,AdminNo,DiplomaName,PMG,PEM,GPA,OSEP,EntryMode,Scholarship,NameOfScholarship,Award1,Quote1,NameOfDegree,UniversityAdmitted,YearAdmitted,SelfEmployed,LatestEmploymentChecked,LatestEmployer,CurrentPosition,CurrentSalary,Remarks, submittedBy from GraduateTracks where DetailsId = '" + int.Parse(lblID.Text) + "'");

            if (dt1.Rows.Count > 0)
            {
                // DataRow dr = dt.Select("CustomerID=" + GridViewData.DataKeys[gvrow.RowIndex].Value.ToString())[0];
                TBFamilyName.Text = dt1.Rows[0]["FamilyName"].ToString(); //
                TBName.Text = dt1.Rows[0]["Name"].ToString(); ;
                TBHandphone.Text = dt1.Rows[0]["Handphone"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[3].Text);
                TBEmailAddress.Text = dt1.Rows[0]["EmailAddress"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[4].Text);
                TBTalent.Text = dt1.Rows[0]["Talent"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[5].Text);
                lblAdminNo.Text = dt1.Rows[0]["AdminNo"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[6].Text);
                TBDiplomaName.Text = dt1.Rows[0]["DiplomaName"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[7].Text);
                TBPMG.Text = dt1.Rows[0]["PMG"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[8].Text);
                TBPEM.Text = dt1.Rows[0]["PEM"].ToString();//HttpUtility.HtmlDecode(gvrow.Cells[9].Text);
                TBGPA.Text = dt1.Rows[0]["GPA"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[10].Text);
                TBOSEP.Text = dt1.Rows[0]["OSEP"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[11].Text);
                TBEntryMode.Text = dt1.Rows[0]["EntryMode"].ToString(); //
                TBScholarship.Text = dt1.Rows[0]["Scholarship"].ToString(); ;
                TBNameOfScholarship.Text = dt1.Rows[0]["NameOfScholarship"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[3].Text);
                TBAward1.Text = dt1.Rows[0]["Award1"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[4].Text);
                TBQuote1.Text = dt1.Rows[0]["Quote1"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[5].Text);
                TBNameOfDegree.Text = dt1.Rows[0]["NameOfDegree"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[6].Text);
                TBUniversityAdmitted.Text = dt1.Rows[0]["UniversityAdmitted"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[7].Text);
                TBYearAdmitted.Text = dt1.Rows[0]["YearAdmitted"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[8].Text);
                TBSelfEmployed.Text = dt1.Rows[0]["SelfEmployed"].ToString();//HttpUtility.HtmlDecode(gvrow.Cells[9].Text);
                TBLatestEmploymentChecked.Text = dt1.Rows[0]["LatestEmploymentChecked"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[10].Text);
                TBLatestEmployer.Text = dt1.Rows[0]["LatestEmployer"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[11].Text);
                TBCurrentPosition.Text = dt1.Rows[0]["CurrentPosition"].ToString(); //
                TBCurrentSalary.Text = dt1.Rows[0]["CurrentSalary"].ToString(); ;
                TBRemarks.Text = dt1.Rows[0]["Remarks"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[3].Text);
                lblSubmit.Text = dt1.Rows[0]["submittedBy"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[10].Text);
            }
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script type='text/javascript'>");
            sb.Append("$('#editModal').modal('show');");
            sb.Append(@"</script>");
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditModalScript", sb.ToString(), false);

        }
    }

我相信您的错误将是这条线...

protected void GridView7Data_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument); // Error Converting
}

根据上面的注释,您提到您的命令参数为“名称”,不能转换为整数。

根据以下代码轻松获取RowIndex的简便方法

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "xxx")
    {
        // Why LinkButton because your button type is LinkButton...
        GridViewRow gvr = ((LinkButton)e.CommandSource).NamingContainer as LinkButton;
        int rowIndex = gvr.RowIndex;
    }
}

aspx html下方的LinkBut​​ton参考

<asp:LinkButton ID="lnkDownload7" runat="server" CommandArgument='<%# Eval("DetailsId") %>' 
 OnClick="DownloadFile7" Text="Download"></asp:LinkButton>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM