2011-07-11 14 views
0

グリッドビューのチェックボックスに列があります。これらのボックスをデータベース内の対応する値に応じてチェックするかチェックを外すかを表示できるようにしたい(現在のところ、この列はどのフィールドにもバインドされていませんが、問題があります)。私に問題をもたらしているのは、私がAjaxを使っているという事実です。 AutoPostBack = trueにすると、チェックボックスが1秒間だけチェックされて表示され、すべてが更新されるとチェックが消えます。どんな助けもありがとう。ページロード時に、チェックボックスをオンまたはオフにします。

.CS

public partial class vieworders : System.Web.UI.Page 
{ 
    private string orderByString; 
    private string fieldString; 
    private string address; 
    private DataGrid dataGrid = new DataGrid(); 


    protected void Page_Load(object sender, EventArgs e) 
    { 

     orderByString = orderByList.SelectedItem.Value; 
     fieldString = searchTextBox.Text; 
     string sqlStatement = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' ORDER BY " + orderByString; 
      //////////////////////////// 


      System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager. 
       OpenWebConfiguration("/Cabot3"); 
      System.Configuration.ConnectionStringSettings connectionString; 

      connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; 

     //TEST 
      for (int i = 0; i < DefaultGrid.Rows.Count; i++) 
      { 
       CheckBox chkUpdate = (CheckBox)DefaultGrid.Rows[i].Cells[1].FindControl("CheckBoxProcess"); 
       if (chkUpdate != null) 
       { 
        OrderBrowser.Text += "Test"; 
       } 
      } 



      // Create an SqlConnection to the database. 
      using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
      { 
       connection.Open(); 
       SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStatement, connection); 


       // create an SqlCommandBuilder - this will automatically generate the 
       // commands, and set the appropriate properties in the dataAdapter 
       SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); 

       // create the DataSet 
       DataSet dataSet = new DataSet(); 
       // fill the DataSet using our DataAdapter 

       dataAdapter.Fill(dataSet, "SecureOrders"); 


       SqlCommand cmd = new SqlCommand("SELECT * FROM SecureOrders", connection); // might not need this 
       SqlCommand bitCmd = new SqlCommand("Select IsNull(processed,0) as processedField From SecureOrders", connection); 
       DataView source = new DataView(dataSet.Tables[0]); 
       DefaultGrid.DataSource = source; 
       DefaultGrid.DataBind(); 
      } 


     } 

    protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e) 
    { 
     GridViewRow row = DefaultGrid.SelectedRow; 
     string name = "Name: " + row.Cells[2].Text + " " + row.Cells[3].Text + "\r\n"; 
     // if (row.Cells[4].Text == "&nbsp;") 
     //{ 
      //address = "Address: " + row.Cells[3].Text + "\r\n   " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n"; 

     // } 
     //else 
     // { 
      // address = "Address: " + row.Cells[3].Text + "\r\n   " + row.Cells[4].Text + "\r\n   " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n"; 
     //} 

     string zip = "Zip: " + row.Cells[4].Text + "\r\n"; 
     string email = "Email: " + row.Cells[5].Text + "\r\n"; 
     //string phone = "Phone: " + row.Cells[10].Text + "\r\n"; 
     //string cctype = "Credit Card Type: " + row.Cells[11].Text + "\r\n"; 
     //string ccnum = "Credit Card Number: " + row.Cells[12].Text + "\r\n"; 
     //string ccexp = "Credit Card Expiration: " + row.Cells[13].Text + "\r\n"; 
     string length = "Length: " + row.Cells[8].Text + "\r\n"; 
     //string delivery = "Delivery: " + row.Cells[15].Text + "\r\n"; 
     string price = "Price: " + row.Cells[7].Text + "\r\n"; 
     string source = "Source: " + row.Cells[6].Text + "\r\n"; 
     //string joined = "Joined: " + row.Cells[18].Text + "\r\n"; 
     //string url = "URL: " + row.Cells[19].Text + "\r\n"; 

     OrderBrowser.Text = name + email + length + price + source; 
    } 

    protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox cb = (CheckBox)sender; 
     GridViewRow gr = (GridViewRow)cb.NamingContainer; 
     if (cb.Checked) 
     { 
      OrderBrowser.Text = "checked"; 
     } 
     else 
     { 
      OrderBrowser.Text = "unchecked"; 
     } 

    } 

    } 

の.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="vieworders.aspx.cs" Inherits="Cabot3.custserv.vieworders" %> 

 <asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true"> 
      <asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem> 
      <asp:ListItem Value="lName">Last Name</asp:ListItem> 
      <asp:ListItem Value="state">State</asp:ListItem> 
      <asp:ListItem Value="zip">Zip Code</asp:ListItem> 
      <asp:ListItem Value="cwaSource">Source</asp:ListItem> 
      <asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem> 
     </asp:DropDownList> 
    </div> 
    <div> 
     <asp:Label runat="server" ID="searchLabel" Text="Search For: " /> 
     <asp:TextBox ID="searchTextBox" runat="server" Columns="30" /> 
     <asp:Button ID="searchButton" runat="server" Text="Search" /> 
    </div> 
<div> 
<asp:UpdatePanel ID = "up" runat="server"> 



    <ContentTemplate> 
    <div style= "overflow:auto; height:150px; width:700px"> 
    <asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName, lName, zip" 
    onselectedindexchanged = "DefaultGrid_SelectedIndexChanged" 
    autogenerateselectbutton = "true" 
    selectedindex="0"> 
    <SelectedRowStyle BackColor="Azure" 
    forecolor="Black" 
    font-bold="true" /> 
    <Columns> 
    <asp:TemplateField HeaderText="Processed"> 
       <ItemTemplate> 
        <asp:CheckBox ID="CheckBoxProcess" OnCheckedChanged="CheckBoxProcess_CheckedChanged" runat="server" Enabled="true" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
    </Columns> 
    </asp:GridView> 
    </div> 
    </div> 
    <asp:TextBox ID="OrderBrowser" columns="70" Rows="14" runat="server" Wrap="false" TextMode="MultiLine" ReadOnly = "true"/> 
    </ContentTemplate> 
    </asp:UpdatePanel> 



</div> 
</form> 

答えて

1

そのあなたのGridViewのが再びバインドされCheckBoxProcess_CheckedChanged前に、AJAXのpostaback中は、チェックボックスの上ごpage_loadイベント火災をクリックしたときからです。それはようにする必要があり

...

If(!IsPostBack) // you missed this condition 
{ 
    orderByString = orderByList.SelectedItem.Value; 
    fieldString = searchTextBox.Text; 
    string sqlStatement = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' ORDER BY " + orderByString; 
     //////////////////////////// 


     System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager. 
      OpenWebConfiguration("/Cabot3"); 
     System.Configuration.ConnectionStringSettings connectionString; 

     connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; 

    //TEST 
     for (int i = 0; i < DefaultGrid.Rows.Count; i++) 
     { 
      CheckBox chkUpdate = (CheckBox)DefaultGrid.Rows[i].Cells[1].FindControl("CheckBoxProcess"); 
      if (chkUpdate != null) 
      { 
       OrderBrowser.Text += "Test"; 
      } 
     } 



     // Create an SqlConnection to the database. 
     using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
     { 
      connection.Open(); 
      SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStatement, connection); 


      // create an SqlCommandBuilder - this will automatically generate the 
      // commands, and set the appropriate properties in the dataAdapter 
      SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); 

      // create the DataSet 
      DataSet dataSet = new DataSet(); 
      // fill the DataSet using our DataAdapter 

      dataAdapter.Fill(dataSet, "SecureOrders"); 


      SqlCommand cmd = new SqlCommand("SELECT * FROM SecureOrders", connection); // might not need this 
      SqlCommand bitCmd = new SqlCommand("Select IsNull(processed,0) as processedField From SecureOrders", connection); 
      DataView source = new DataView(dataSet.Tables[0]); 
      DefaultGrid.DataSource = source; 
      DefaultGrid.DataBind(); 
     } 
} 

編集:あなたのコメントを次したら、チェックボックスの状態を設定するgridiew行を反復処理する必要はありません。 GridViewのRowDataBoundイベントを使用することも、値を直接バインドすることもできます。例えば

<asp:CheckBox ID="CheckBoxProcess" Checked='<%#Eval("processedField") %>' OnCheckedChanged="CheckBoxProcess_CheckedChanged" runat="server" Enabled="true" /> 

更新:これは偉大に見えるこの1

// Create an SqlConnection to the database. 
     using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
     { 
      connection.Open();     

      SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection); 

      // create the DataSet 
      DataSet dataSet = new DataSet(); 
      // fill the DataSet using our DataAdapter    
      dataAdapter.Fill(dataSet, "SecureOrders"); 

      DataView source = new DataView(dataSet.Tables[0]); 
      DefaultGrid.DataSource = source; 
      DefaultGrid.DataBind(); 
     } 
+0

に以下のコードを変更してください - 唯一のことは、実際にデータがここにあるチェックボックスにバインドされていないことである - どのように私はバインドすることができますグリッドビュー内の1つのフィールドを1つの列に変換しますか? –

+0

チェックボックスの値を間違ってバインドしているため、現在実行している行をすべて反復処理する必要はありません。 RowDataBoundイベントを使用することも、チェックボックスの値を直接バインドすることもできます。私の答えを確認してください。 –

+0

これを試してみましたが、私が "ColumnName"に入れなければならないものが私の人生であるかどうかはわかりません。私はテーブルから名前を使ってみましたが、それを認識できませんでした! –

関連する問題