如何在oncheckedchanged事件中找到datakeys?

时间:2010-06-14 10:19:44

标签: c# asp.net

                <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>

并在代码中

        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";
        }
    }

我想知道如何在checked事件中找到数据值,因为checked事件中的update Query正在生成异常。

1 个答案:

答案 0 :(得分:0)

您可以添加一个asp:按钮而不是使用GridView上的复选框并触发OnRowUpdating事件吗?

或者,您可以将索引值存储在一个可变的会话中,以便在您的chkbox点击时获取。

编辑: found this post有什么帮助吗?

他们建议您必须检查每一行以查看值是否已更改。