微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在添加之前验证 Gridview 中是否存在选定的 Dropdownlist 值?

如何解决如何在添加之前验证 Gridview 中是否存在选定的 Dropdownlist 值?

目前,我有一个网格视图和带有与数据库绑定的下拉列表的表格。

根据用户的选择和他们的 ID,我想在将值插入列(类别)之前验证 gridview 中是否存在下拉列表值。我可以帮助我实现这一目标吗?

我尝试过的:

WebForm.aspx

显示数据的Gridview

            <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="Gridview1_PageIndexChanging" OnRowCancelingEdit="Gridview1_RowCancelingEdit" OnRowCommand="Gridview1_RowCommand" OnRowDeleting="Gridview1_RowDeleting" ShowFooter="True" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" ShowHeaderWhenEmpty="True" Width="100%" CssClass="table table-responsive table-bordered" OnSelectedindexChanged="Gridview1_SelectedindexChanged" DataKeyNames="CID">
            <Columns>
                <asp:TemplateField HeaderText="Transaction ID">
                    <EditItemTemplate>
                        <asp:Label ID="lbleditid" runat="server" Text='<%# Bind("CID") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%# Bind("CID") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle CssClass="hidden-xs" />
                    <ItemStyle CssClass="hidden-xs" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CCID">
                    <EditItemTemplate>
                        <asp:Label ID="lblccid2" runat="server"></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblccid" runat="server" Text='<%# Bind("CCID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField datafield="Category" HeaderText="Category" />
                <asp:TemplateField HeaderText="Last Edited">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDateTime1" runat="server" Text='<%# Bind("Last_Edited") %>' ReadOnly="True"></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>

                        <asp:TextBox ID="txtDateTime" runat="server" ToolTip="DD/MM/YYYY" ReadOnly="True"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblDateTime" runat="server" Text='<%# Bind("Last_Edited") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton3" runat="server" OnClick="PassData">Select</asp:LinkButton>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="true" CommandName="AddNew" Text="Add New"></asp:LinkButton>
                    </FooterTemplate>

                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                No record shown
            </EmptyDataTemplate>
        </asp:GridView>

将数据插入到 Gridview 的表

  <table style="width: 100%;">
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlTovalidate="DropDownList1" ErrorMessage="CustomValidator" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">Category</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sqlDataSource2" DataTextField="Desc" DataValueField="Desc">
                    </asp:DropDownList>
                    <asp:sqlDataSource ID="sqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>" SelectCommand="SELECT [Desc] FROM [Categories]"></asp:sqlDataSource>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="Label2" runat="server"></asp:Label>
                </td>
                <td>
    <asp:Button ID="Button1" runat="server" Text="Add Category" OnClick="Button1_Click" />


                </td>
                <td>&nbsp;</td>
            </tr>
        </table>

WebForm.aspx.cs

                protected void Button1_Click(object sender,EventArgs e)
    {
        sqlConnection con = new sqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
        con.open();

        string query = "SELECT CATEGORY FROM CATEGORY WHERE C_UserName = '" + Session["id"] + "'  AND  CATEGORY = '" + DropDownList1.SelectedItem.Value + "' ";
        sqlCommand cmd = new sqlCommand(query,con);

        sqlDataReader reader = cmd.ExecuteReader();
        if (reader.HasRows)
        {
            cmd.Parameters.AddWithValue("@CATEGORY",DropDownList1.SelectedItem.Value);

            lblResult.Text = "You have selected this category. Please select a new category";
            con.Close();

        }
        else
        {
            sqlCommand cmd1 = new sqlCommand("UPDATE SET CATEGORY CCID@CCID (CATEGORY,C_USERNAME,CCID) VALUES (@CATEGORY,@C_USERNAME,@CCID)",con);
            cmd1.Parameters.AddWithValue("CATEGORY",DropDownList1.SelectedItem.Value);
            cmd1.Parameters.AddWithValue("C_USERNAME",Session["id"]);
            cmd1.Parameters.AddWithValue("CCID",Label1.Text);

            con.open();
            int i = cmd1.ExecuteNonQuery();
            con.Close();


            if (i != 0)
            {

                Label2.Text = " Your data is been saved in the database";
                Label2.ForeColor = System.Drawing.Color.ForestGreen;


            }
            else
            {
                Label2.Text = "Something went wrong with selection";
                Label2.ForeColor = System.Drawing.Color.Red;

            }


        }
    }

一个更新:现在,它一直提示连接未关闭错误

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。