(这是另一个问题,因为我的原帖,我在一个问题中提出了太多问题)
让我先说明一下。我对ASP编码几乎全新。我正在研究一个需要我使用ASP而不是PHP的小方案项目。我一直在寻找过去2天的解决方案,并尝试了很多东西,但似乎无法使用我的代码。我已经成为这个网站的一部分,所以我知道它是如何工作的。我不会在这里询问我是否已经有一段时间没有尝试过这样做了。我在这里学到了大量有关SQL的信息,所以我希望对ASP也这样做。
问题:
我知道我需要OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting"
和Code Behind使其适用于我的设置,否则我得到:“GridView'GridView1'触发事件排序,但未处理。”错误。我找到的任何解决方案,我都无法在我的代码中运行。
GridView1:
<asp:TextBox ID="TextBox1" runat="server" Width="265px" Height="22px" CssClass="myBox"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search Fields" CssClass="myButton" />
<asp:GridView ID="GridView1" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" runat="server" AutoGenerateColumns="true" CellPadding="4" EnableModelValidation="True" EnableTheming="True" ForeColor="#333333" GridLines="None" Width="100%" style="margin-top: 0px; text-align: center;" AllowPaging="True" AllowSorting="True" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
Code Behind(DB名称和密码取出):
SqlConnection vid = new SqlConnection("Data Source=ENF;Initial Catalog=***Database Name***;Persist Security Info=True;User ID=sa;Password=***Password***");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String str = "SELECT ab.NAME as [Customer] ,ISNULL(ab.TELEPHONE1,'') as [Phone #] ,ISNULL(pb.NAME,'') as [Product] ,ISNULL(aeb.NEW_PRODUCTVERSION,'') as [Version] ,CASE WHEN ab.STATUSCODE = 1 THEN 'Active' ELSE 'Inactive' END as [Status] ,ISNULL('Sal : ' + c.SALUTATION + ' / ','') + ISNULL('Title : ' + c.JOBTITLE + ' / ','') + ISNULL(a.PRIMARYCONTACTIDNAME,'') as [Primary Contact] ,ISNULL(c.TELEPHONE1,'') as [Contact Phone] FROM ACCOUNTBASE ab LEFT JOIN ACCOUNTEXTENSIONBASE aeb on ab.ACCOUNTID = aeb.ACCOUNTID LEFT JOIN PRODUCTBASE pb on aeb.NEW_PRIMARYPRODUCTID = pb.PRODUCTID LEFT JOIN ACCOUNT a on ab.ACCOUNTID = a.ACCOUNTID LEFT JOIN CONTACT c on a.PRIMARYCONTACTID = c.CONTACTID WHERE ((ab.NAME LIKE '%' + @search + '%') OR (aeb.NEW_PRODUCTVERSION LIKE '%' + @search + '%') OR (pb.NAME LIKE '%' + @search + '%') OR (a.PRIMARYCONTACTIDNAME LIKE '%' + @search + '%')) ORDER BY ab.NAME";
SqlCommand xp = new SqlCommand(str, vid);
xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
GridView1.DataSource = ds;
GridView1.DataBind();
vid.Close();
}
其他GetData()
private DataSet GetData()
{
String str = "SELECT ab.NAME as [Customer] ,ISNULL(ab.TELEPHONE1,'') as [Phone #] ,ISNULL(pb.NAME,'') as [Product] ,ISNULL(aeb.NEW_PRODUCTVERSION,'') as [Version] ,CASE WHEN ab.STATUSCODE = 1 THEN 'Active' ELSE 'Inactive' END as [Status] ,ISNULL('Sal : ' + c.SALUTATION + ' / ','') + ISNULL('Title : ' + c.JOBTITLE + ' / ','') + ISNULL(a.PRIMARYCONTACTIDNAME,'') as [Primary Contact] ,ISNULL(c.TELEPHONE1,'') as [Contact Phone] FROM ACCOUNTBASE ab LEFT JOIN ACCOUNTEXTENSIONBASE aeb on ab.ACCOUNTID = aeb.ACCOUNTID LEFT JOIN PRODUCTBASE pb on aeb.NEW_PRIMARYPRODUCTID = pb.PRODUCTID LEFT JOIN ACCOUNT a on ab.ACCOUNTID = a.ACCOUNTID LEFT JOIN CONTACT c on a.PRIMARYCONTACTID = c.CONTACTID WHERE ((ab.NAME LIKE '%' + @search + '%') OR (aeb.NEW_PRODUCTVERSION LIKE '%' + @search + '%') OR (pb.NAME LIKE '%' + @search + '%') OR (a.PRIMARYCONTACTIDNAME LIKE '%' + @search + '%')) ORDER BY ab.NAME";
SqlCommand xp = new SqlCommand(str, vid);
xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
vid.Close();
return ds;
}
将此添加到Code Behind:
protected void GridView1_PageIndexChanging(object sender, EventArgs e)
{ }
protected void GridView1_Sorting(object sender,GridViewSortEventArgs e) { }
现在,当点击列名称或分页页面时,它只是闪烁页面,没有任何反应。
答案 0 :(得分:0)
您必须实施Sorting事件
<asp:GridView OnSorting="GridView1_Sorting" />
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//sort the data
}
答案 1 :(得分:0)
在我将以下行添加到页面aspx.cs文件后,错误消失了:
protected void GridView1_PageIndexChanging(object sender, EventArgs e)
{
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}