2010-06-14 37 views
1
   <asp:Panel ID="pnlFocusAreaPanel" runat="server" GroupingText="Focus Area" Width="800"> 
       <table cellspacing="0" cellpadding="0" width="750"> 
        <tr> 
         <td> 
          <asp:GridView ID="dgFocusAreaDetails" runat="server" Width="100%" CssClass="dgStyle" 
           AutoGenerateColumns="False" BorderColor="Silver" BorderWidth="1px" CellPadding="0" 
           ShowHeader="False" OnSelectedIndexChanged="dgFocusAreaDetails_SelectedIndexChanged" 
           DataKeyNames="PK_ID"> 
           <FooterStyle Font-Underline="True" HorizontalAlign="Center" VerticalAlign="Middle"> 
           </FooterStyle> 
           <Columns> 
            <asp:TemplateField HeaderText="Focus Area" HeaderStyle-BackColor="Silver"> 
             <ItemStyle Width="90%" /> 
             <ItemTemplate> 
              <asp:Label runat="server" ID="lblFocusArea" Text='<%# DataBinder.Eval(Container.DataItem, "FOCUS_AREA_NAME").ToString()%>'> 
              </asp:Label> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="Is Current Valid" HeaderStyle-BackColor="Silver"> 
             <ItemStyle Width="10%" HorizontalAlign="Center" /> 
             <ItemTemplate> 
              <asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="OnCheckChangedEvent" 
               AutoPostBack="true" /> 
             </ItemTemplate> 
            </asp:TemplateField> 
           </Columns> 
          </asp:GridView>        
         </td> 
        </tr> 
        <tr> 
         <td align="left"> 
          <asp:TextBox ID="txtFocusArea" runat="server" Width="300px" CausesValidation="true" CssClass="txtStyle" ValidationGroup="focusArea"> 
          </asp:TextBox> 
          <cc1:TextBoxWatermarkExtender ID="txtFocusAreaWaterMark" runat="server" TargetControlID="txtFocusArea" 
           WatermarkText="Add Focus Area" WatermarkCssClass="WaterMarkStyle"> 
          </cc1:TextBoxWatermarkExtender> 
          <asp:Button ID="btnAddFocusArea" runat="server" Text="AddFocusArea" CssClass="btnStyle" 
           OnClick="btnAddFocusArea_Click" CausesValidation="true" ValidationGroup="focusArea" /> 
          <asp:RequiredFieldValidator ID="reqtxtFocusArea" runat="server" ControlToValidate="txtFocusArea" 
           ErrorMessage="Please enter focus area" Display="Dynamic" ValidationGroup="focusArea"> 
          </asp:RequiredFieldValidator> 
          <asp:RegularExpressionValidator ID="regexFocusArea" runat="server" ErrorMessage="Invalid characters(<,>,#)" 
           ControlToValidate="txtFocusArea" ValidationExpression="^([^&lt;#&gt;]*)$" ValidationGroup="focusArea" 
           Display="Dynamic" SetFocusOnError="true"> 
          </asp:RegularExpressionValidator> 
         </td> 
        </tr> 
       </table> 
      </asp:Panel> 

,並在後面如何在oncheckedchanged事件中查找數據鍵?

 protected void btnAddFocusArea_Click(object sender, EventArgs e) 
    { 
     string strFocusArea = txtFocusArea.Text; 
     OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]); 
     if (strFocusArea == string.Empty || strFocusArea == txtFocusAreaWaterMark.WatermarkText) 
     { 
      lblError.Text = "Focus area is not entered"; 
     } 
     else 
     { 
      string insertQuery = "INSERT INTO LK_CODECAT_FOCUS_AREA(FOCUS_AREA_NAME,FK_ADDED_BY,ADDED_ON) "+ 
           " VALUES('" + strFocusArea + "', "+Session["UserID"] +", GETDATE())"; 

      try 
      { 
       myConnection.Open(); 
       OleDbCommand myCommandToInsert = new OleDbCommand(insertQuery, myConnection); 
       myCommandToInsert.ExecuteNonQuery(); 
      } 
      catch(Exception exc) 
      { 
       ExceptionLogger.LogException(exc); 
      } 
      finally 
      { 
       if (myConnection != null) 
       { 
        myConnection.Close(); 
       } 
      } 
      PopulateFocusArea(); 

     } 
     txtFocusArea.Text = txtFocusAreaWaterMark.WatermarkText; 
    } 

    private void PopulateFocusArea() 
    { 
     OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]); 

     string selectQuery = "SELECT PK_ID, FOCUS_AREA_NAME, IS_CURRENT_FOCUS FROM LK_CODECAT_FOCUS_AREA LKCFA ORDER BY FOCUS_AREA_NAME"; 

     if (Session["RoleID"].ToString() == "1" || ((Session["WorkID"].ToString() != "9") || 
        (Session["WorkID2"].ToString() != "9") || 
        (Session["WorkID3"].ToString() != "9"))) 
     { 
      try 
      { 
       myConnection.Open(); 
       OleDbCommand myCommandFocusArea = new OleDbCommand(selectQuery, myConnection); 
       OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommandFocusArea); 
       DataSet ds = new DataSet(); 
       myAdapter.Fill(ds); 
       dgFocusAreaDetails.DataSource = ds; 
       dgFocusAreaDetails.ShowHeader = true; 

       dgFocusAreaDetails.DataBind(); 

      } 
      catch (Exception exc) 
      { 
       ExceptionLogger.LogException(exc); 
      } 
      finally 
      { 
       if (myConnection != null) 
       { 
        myConnection.Close(); 
       } 
      } 
     } 
    } 

    protected void dgFocusAreaDetails_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string selectedCategory = dgFocusAreaDetails.SelectedRow.Cells[1].Text; 
     int index = dgFocusAreaDetails.SelectedIndex; 
     lblError.Text = dgFocusAreaDetails.DataKeys[index].Value.ToString(); 
    } 

    public void OnCheckChangedEvent(object sender, EventArgs e) 
    { 
     CheckBox chk = (CheckBox)sender; 
     string chkfocusarea = ((Control)chk).ID; 

     //string focusAreaDetails = dgFocusAreaDetails.SelectedRow.Cells[0].Text; 
     int index =Convert.ToInt32(dgFocusAreaDetails.SelectedIndex); 
     if (chk.Checked) 
     { 
      string updateQuery = "UPDATE [LK_CODECAT_FOCUS_AREA] SET [IS_CURRENT_FOCUS] = 1 WHERE PK_ID = " 
            +Convert.ToInt32(dgFocusAreaDetails.DataKeys[index].Value)+" "; 
     } 
     else 
     { 
      lblError.Text = "unchecked"; 
     } 
    } 

我想知道我怎麼找到關於檢查事件datakeyvalue因爲updateQuery託運事件產生異常代碼。

回答

0

您可以添加一個asp:按鈕,而不是使用GridView上的複選框並觸發OnRowUpdating事件嗎?

或者,您可以將索引值存儲在會話變量中,以便在您的chkbox點擊中選取。

編輯:found this post是否有幫助?

他們建議您必須檢查每一行以查看值是否已更改。

+0

會話變量不工作 和我有更新時複選框,單擊 我不能使用按鈕 – Subbu 2010-06-14 11:06:56

+0

記錄將ü建議一些其他方法 – Subbu 2010-06-14 11:20:08

+0

這個循環會增加數據庫負載 – Subbu 2010-06-14 13:34:50

相關問題