(this is another question, since my original post, I asked too many questions in one)
Let me state this first. I am pretty much completely new to ASP coding. I am working on a little side project that requires me to use ASP instead of PHP that I sort of did before. I have been looking at solutions for past 2 days, and have tried many things, but can't seem to get it to work with my code. I have been part of this site for some time, so I do know how it works. I would not be asking here if I wasn't already trying to do this on my own for some time. I have learned a huge amount of information about SQL on here, so I hope to do the same with ASP.
Question:
Sorting/Paging
I know that I need the OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting"
and the Code Behind to make it work for my set otherwise I get the : "The GridView 'GridView1' fired event Sorting which wasn't handled." error. Just any solution I find out there, I just can't make it work within my code.
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 name and Password taken out):
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();
}
Additional 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;
}
Added this to Code Behind :
protected void GridView1_PageIndexChanging(object sender, EventArgs e)
{ }
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { }
Now, when clicking on the column name or the paging page, it just flashes the page, and nothing happens.
After I added following lines to page aspx.cs file, error gone away: