Gridview排序在updatepanel中不起作用

时间:2015-10-05 03:37:24

标签: c# asp.net gridview webforms gridview-sorting

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

        }
    }

1 个答案:

答案 0 :(得分:0)

我相信你的错误就是这一行...

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/formvalidation/0.6.1/css/formValidation.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/formvalidation/0.6.1/js/formValidation.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/formvalidation/0.6.1/js/framework/bootstrap.min.js"></script>
<form id="contactForm" class="form-horizontal">
    <div class="form-group required">
        <label for="address1" class="col-xs-12 col-sm-2 col-md-2 control-label">Address</label>
        <div class="col-xs-12 col-sm-9 col-md-9">
            <input type="text" class="form-control" name="address1" id="address1">
        </div>
    </div>
        
    <div class="form-group required">
        <div class="col-xs-12 col-sm-3 col-md-3 col-sm-offset-2 col-md-offset-2">
            <input type="text" class="form-control" name="address_city" id="AddressCity">
            <label class="control-label control-label-under" for="address_city">City</label>
        </div>
            
        <p class="row clearfix  visible-xs-block" form-group required></p>
            
        <div class="col-xs-12 col-sm-3 col-md-3 contact-state">
            <input type="text" class="form-control" name="address_state" id="AddressEmirate">
            <label class="control-label control-label-under" for="address_state">Emirate</label>
        </div>
            
        <p class="row clearfix  visible-xs-block " ></p>
            
        <div class="col-xs-12 col-sm-3 col-md-3">
            <input type="text" class="form-control" name="address_zip" id="AddressPostal">
            <label class="control-label control-label-under"></label>
        </div>
    </div>
    <div class="form-group">
        <div class="col-xs-9 col-xs-offset-3">
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
</form>

根据上面的评论你提到你的命令参数是“名称”无法转换为整数..

根据以下代码轻松获取RowIndex

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

参考LinkBut​​ton,如下面的aspx html

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