选择命令和搜索命令在gridview中无法正常工作

时间:2015-02-24 02:56:32

标签: c# asp.net gridview

我的所有命令都正常工作但问题是 在我执行我的搜索命令后,然后单击GridView上的选择链接按钮,我的网格视图刷新并选择网格视图中的第一行而不是我搜索的那一行。例如:首先我运行索引页然后是GridView从数据库中获取所有记录(10个填充记录)并显示到索引..(没问题) 然后我输入用户的名字来搜索它,然后我点击搜索按钮,然后将其过滤并再次显示到gridview图像 enter image description here

然后我点击"选择"来自Gridview的链接按钮和发生的事情

enter image description here

发生了什么......我点击了" marcy"记录"上的选择链接按钮。然后它刷新并获得第一行RASI记录的值......

但是当我的点击选择没有搜索它工作得很好

这是我的搜索按钮代码

 string strconn;


        strconn = "select * from User_TBL_DB where (Firstname like '%'+@search+'%' and Middlename like '%'+@search2+'%' and Lastname like '%'+@search3+'%')";
            SqlCommand xp = new SqlCommand(strconn, conn);
            xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = firstname_search.Text;
            xp.Parameters.Add("@search2", SqlDbType.NVarChar).Value = middlename_search.Text;
            xp.Parameters.Add("@search3", SqlDbType.NVarChar).Value = lastname_search.Text;


            conn.Open();
            xp.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = xp;
            DataSet ds = new DataSet();
            da.Fill(ds, "Name");

            GridView1.DataSource = ds;
            GridView1.DataBind();
            conn.Close();

这是我在gridview上选择链接按钮的代码

 row = GridView1.SelectedRow;
        firstname_tb.Text = (row.FindControl("lbl_Firstname") as Label).Text;
        middlename_tb.Text = (row.FindControl("lbl_Middlename") as Label).Text;
        lastname_tb.Text = (row.FindControl("lbl_Lastname") as Label).Text;
        age_tb.Text = (row.FindControl("lbl_Age") as Label).Text;
        id_tb.Text = (row.FindControl("lbl_ID") as Label).Text;
        string gender = (row.FindControl("lbl_Sex") as Label).Text;

        if (gender == "FEMALE")
        {
            female_rb.Checked = true;
            male_rb.Checked = false;
        }
        else
        {
            male_rb.Checked = true;
            female_rb.Checked = false;
        }

这是我在Gridview上的ASP代码

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC" 
                    BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateColumns="False" 
                        onselectedindexchanged="GridView1_SelectedIndexChanged" Width="581px" 
                        onrowcommand="GridView1_RowCommand"
                        onselectedindexchanging="GridView1_SelectedIndexChanging">
                        <Columns>
                            <%--<asp:TemplateField ShowHeader="False">
                                <ItemTemplate>
                                    <asp:LinkButton ID="Select_button" CommandArgument ='<%# Eval("ID") %>' CommandName="SelectRow" ForeColor="#8C4510" runat="server">Select</asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>--%>

                            <asp:CommandField ShowSelectButton="True"></asp:CommandField>
                            <asp:TemplateField HeaderText="Firstname" SortExpression="Firstname">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Firstname") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Firstname" runat="server" Text='<%# Bind("Firstname") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Middlename" SortExpression="Middlename">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Middlename") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Middlename" runat="server" Text='<%# Bind("Middlename") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Lastname" SortExpression="Lastname">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Lastname") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Lastname" runat="server" Text='<%# Bind("Lastname") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Age" SortExpression="Age">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Age" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Sex" SortExpression="Sex">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Sex") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Sex" runat="server" Text='<%# Bind("Sex") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="ID" SortExpression="ID">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>

                        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                        <HeaderStyle BackColor="#003399" Font-Bold="True" 
    ForeColor="#CCCCFF" />
                        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" 
    HorizontalAlign="Left" />
                        <RowStyle BackColor="White" ForeColor="#003399" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" 
    ForeColor="#CCFF99" />
                        <SortedAscendingCellStyle BackColor="#EDF6F6" />
                        <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                        <SortedDescendingCellStyle BackColor="#D6DFDF" />
                        <SortedDescendingHeaderStyle BackColor="#002876" />

                    </asp:GridView>

我将它连接到sqlDatasource1并断开连接并保持代码不变。我使用了Showselectbutton =&#34; true&#34;

我的问题希望你能帮助我,非常感谢你

0 个答案:

没有答案