dimanche 29 novembre 2015

ASP.NET C# Gridview not detecting checkboxes being checked.

What i'm trying to do is execute some code when a checkbox in a gridview is checked, with the code being executed row by row. I debugged the code and every time returns false despite the checkboxes being checked. The code i'm trying to execute works if the conditional statement is removed.

ShoppingCartButton is the focus, but including the load code just in case.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.OleDb;
using System.Text.RegularExpressions;

public partial class results : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
    Regex rgx = new Regex("^[0-9A-Za-z ]+$");
    var myconn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["GamesConnectionString"].ConnectionString);
    StringBuilder sb2 = new StringBuilder();
    string genre = Request["GenreDropdown"];
            if (genre == "" || genre==null) genre = " ";
    string platform = Request["PlatformDropdown"];
    if (platform == "" || platform == null) platform = " ";
    string title = Request["TitleSearch"];
    if (title == "" || title==null)
    {
        title = " ";
    }
    string checkedout = Request["CheckedOutBox"];
    if (checkedout ==null || checkedout == "")
    {
        checkedout = " ";
    }
    if (!rgx.IsMatch(title) || !rgx.IsMatch(platform) || !rgx.IsMatch(genre) || !rgx.IsMatch(checkedout))
  {
     Label1.Text = "Invalid Data";
     Response.Redirect("search.aspx");
   }
    else
    {
        //used as a default value for both genre and platform
        string DefaultVal = "All";
        string StockVal = "0";
        StringBuilder sb = new StringBuilder();
        sb.Append("SELECT * FROM GAMETABLE WHERE (GameTitle LIKE '%" + title + "%' AND ");

        SqlDataSource objDS = new SqlDataSource();
        objDS.ProviderName = System.Configuration.ConfigurationManager.ConnectionStrings["GamesConnectionString"].ProviderName;
        objDS.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GamesConnectionString"].ConnectionString;
        //checking the values of the optional fields

        if (genre == DefaultVal || genre == " ")
        {
            genre = "";
        }
        else
        {
            sb.Append("Genre =" + genre + " AND ");
        }
        if (platform == DefaultVal || platform == " ")
        {
            platform = "";
        }
        else
        {
            sb.Append("Platform =" + platform + " AND ");
        }
        if (checkedout != " " || checkedout != null)
        {
            StockVal = checkedout;
        }
        sb.Append("NumberInStock >=" + StockVal + ")");
        if (title == " " || title == null)
        {
            objDS.SelectCommand = "select * from GameTable";
            GridView1.DataSource = objDS;
            GridView1.DataBind();
        }
        else
        {
            string sample = sb.ToString();
            objDS.SelectCommand = sample;
            GridView1.DataSource = objDS;
            GridView1.DataBind();
        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            string unpredictable1 = row.Cells[1].Text;
            string unpredictable = row.Cells[8].Text;
            sb2.Append(unpredictable1);
          //  if (unpredictable == "0")
         //   {

         ///       row.Cells[0].Controls.Clear();
         //   }
        }
        Label2.Text = sb2.ToString();
        if (Session["UserID"] == null)
        {
            ShoppingCartButton.Visible = false;
        }
    }
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void ShoppingCartButton_Click(object sender, EventArgs e)
{
    Label1.Text = "HIH0HI";

    OleDbConnection myconn = new OleDbConnection();
        myconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|ProjectDatabase.accdb";

    string UserID = Session["UserID"].ToString();
    StringBuilder sb3 = new StringBuilder();
    foreach (GridViewRow row in GridView1.Rows)
    {
        {
        string unpredictable = "";
        bool hi = true;
        unpredictable = row.Cells[1].Text;
        CheckBox chk = row.Cells[0].Controls[1] as CheckBox;
        hi = chk.Checked;
        sb3.Append(hi.ToString());
        if (hi==true)
        {
            string command1 = "insert into Cart ([Username],[GameID]) values (@Username, @GameID)";

            OleDbCommand cmd = new OleDbCommand(command1, myconn);
            cmd.Parameters.AddWithValue("@Username", UserID);
            cmd.Parameters.AddWithValue("@GameID", unpredictable);
            myconn.Open();
            cmd.ExecuteNonQuery();
            myconn.Close();
        }



    }
              Label1.Text =  sb3.ToString();


    }
}
}

Markup for the button and the gridview

  <asp:GridView ID="GridView1" runat="server"         AutoGenerateColumns="True" EmptyDataText ="Data Entry Error">
        <Columns>

<asp:TemplateField HeaderText ="Add to Cart?">
            <ItemTemplate>
                <asp:CheckBox ID="checkbx" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
     </Columns>
    </asp:GridView>
    <asp:Button runat="server" id="ShoppingCartButton" Text="Add to     shopping cart" OnClick="ShoppingCartButton_Click" />  




Aucun commentaire:

Enregistrer un commentaire