更新进入第一记录 [英] Update Goes to First Record

查看:62
本文介绍了更新进入第一记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.我在设计模式下为该gridview提供了gridview1和sqldatasource1,我将sqldatasource1分配给gridview1

2.i有3个下拉列表,每个下拉列表都有不同的字段,例如Country State和City
3.我选择了其中三个,然后单击搜索,根据国家和城市显示结果

4.当我尝试编辑任何结果时(即,当我单击gridview上的edit时),该编辑将转到数据库中的第一条记录

5.这意味着当我搜索结果是否为8条记录时是否有10条记录,如果我想修改8条记录则有9条记录,那么编辑字段将移至1条记录

6.我需要一种情况,例如当我尝试编辑第8条和第9条记录时,我必须编辑第8条记录,然后当我单击重新加载时,它必须重新加载整个gridview

我的.aspx代码在下面
----------------------

1.I have a gridview1 and sqldatasource1 for that gridview in design mode i assigned sqldatasource1 to gridview1

2.i have 3 dropdownlists each have different fields like Country State aand City
3.I select three of them and click on search the result is shown according to the country state and city

4.when i try to edit any of the result (ie when i click on edit on the gridview) the edit is goes to first record in the database

5.that means if there are 10 records when i searched if the result is 8 and 9 th records if i wanted to modify 8th record the edit field goes to 1st record

6.I need a situation like when i try to edit 8 and 9 th records must be there and i have to edit 8th record then when i click on reload it has to reload whole gridview

My .aspx Code is below
----------------------

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Country :
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 

            DataSourceID="CountrySqlDataSource" DataTextField="Country" 

            DataValueField="Country">
        </asp:DropDownList>
        <asp:SqlDataSource ID="CountrySqlDataSource" runat="server" 

            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 

            SelectCommand="SELECT DISTINCT [Country] FROM [GridViewDemo]">
        </asp:SqlDataSource>
        Stste :
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 

            DataSourceID="StateSqlDataSource" DataTextField="State" DataValueField="State">
        </asp:DropDownList>
        <asp:SqlDataSource ID="StateSqlDataSource" runat="server" 

            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 

            SelectCommand="SELECT DISTINCT [State] FROM [GridViewDemo] WHERE ([Country] = @Country)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Country" 

                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        City :
        <asp:DropDownList ID="DropDownList3" runat="server" 

            DataSourceID="CitySqlDataSource" DataTextField="City" DataValueField="City">
        </asp:DropDownList>
    
        <asp:SqlDataSource ID="CitySqlDataSource" runat="server" 

            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 

            SelectCommand="SELECT DISTINCT [City] FROM [GridViewDemo] WHERE (([Country] = @Country) AND ([State] = @State))">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Country" 

                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="DropDownList2" Name="State" 

                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    
        <asp:Button ID="ButtonSearch" runat="server" onclick="ButtonSearch_Click" 

            Text="Search" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="ButtonNameSearch" runat="server" 

            onclick="ButtonNameSearch_Click" Text="Search" />
    
    </div>
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

            DataKeyNames="ID" DataSourceID="GridviewSqlDataSource" 

            onrowediting="GridView1_RowEditing">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 

                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Country" HeaderText="Country" 

                    SortExpression="Country" />
                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="GridviewSqlDataSource" runat="server" 

            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 

            DeleteCommand="DELETE FROM [GridViewDemo] WHERE [ID] = @ID" 

            InsertCommand="INSERT INTO [GridViewDemo] ([Name], [Country], [State], [City]) VALUES (@Name, @Country, @State, @City)" 

            SelectCommand="SELECT * FROM [GridViewDemo]" 

            UpdateCommand="UPDATE [GridViewDemo] SET [Name] = @Name, [Country] = @Country, [State] = @State, [City] = @City WHERE [ID] = @ID">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="State" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="State" Type="String" />
                <asp:Parameter Name="City" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>


-------------------
我的.cs文件


-------------------
My .cs File

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        GridviewSqlDataSource.SelectCommand = "SELECT *FROM GridViewDemo WHERE Country=''" + DropDownList1.SelectedItem.ToString() + "'' AND State=''" + DropDownList2.SelectedItem.ToString() + "'' AND City=''" + DropDownList3.SelectedItem.ToString() + "''";
    }
    protected void ButtonNameSearch_Click(object sender, EventArgs e)
    {
        GridviewSqlDataSource.SelectCommand = "SELECT *FROM GridViewDemo WHERE Name LIKE ''%"+TextBox1.Text+"%'' ";

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridviewSqlDataSource.SelectCommand = "SELECT *FROM GridViewDemo WHERE Name LIKE ''%" + TextBox1.Text + "%'' ";
    }
}



-------------------
我的SQL脚本供您参考



-------------------
My SQL script for your reference

CREATE DATABASE GridviewDemo

USE [GridviewDemo]

CREATE TABLE [dbo].[GridViewDemo](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Country] [varchar](50) NULL,
    [State] [varchar](50) NULL,
    [City] [varchar](50) NULL,
 CONSTRAINT [PK_GridViewDemo] PRIMARY KEY CLUSTERED
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

INSERT INTO GridviewDemo VALUES('Krishna','India','Andhra Pradesh','Hyderabad')

推荐答案

ConnectionStrings:GridviewDemoConnectionString %> " =" 选择不同的国家/地区[GridViewDemo]" < /asp:SqlDataSource > 斯特斯特(Stste): < asp:DropDownList ID =" runat 服务器" AutoPostBack 真实" DataSourceID="StateSqlDataSource" DataTextField="State" DataValueField="State"> < /asp:DropDownList > <asp:SqlDataSource ID="StateSqlDataSource" runat="server" ConnectionString="<%
ConnectionStrings:GridviewDemoConnectionString %>" SelectCommand="SELECT DISTINCT [Country] FROM [GridViewDemo]"> </asp:SqlDataSource> Stste : <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="StateSqlDataSource" DataTextField="State" DataValueField="State"> </asp:DropDownList> <asp:SqlDataSource ID="StateSqlDataSource" runat="server" ConnectionString="<%


ConnectionStrings:GridviewDemoConnectionString %>" SelectCommand="SELECT DISTINCT [State] FROM [GridViewDemo] WHERE ([Country] = @Country)"> < SelectParameters > <asp:ControlParameter ControlID="DropDownList1" Name="Country" PropertyName =" =" String" /> < /SelectParameters > < /asp:SqlDataSource > 城市 : <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="CitySqlDataSource" DataTextField="City" DataValueField="City"> < /asp:DropDownList > <asp:SqlDataSource ID="CitySqlDataSource" runat="server" ConnectionString="<%
ConnectionStrings:GridviewDemoConnectionString %>" SelectCommand="SELECT DISTINCT [State] FROM [GridViewDemo] WHERE ([Country] = @Country)"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="Country" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:SqlDataSource> City : <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="CitySqlDataSource" DataTextField="City" DataValueField="City"> </asp:DropDownList> <asp:SqlDataSource ID="CitySqlDataSource" runat="server" ConnectionString="<%


ConnectionStrings:GridviewDemoConnectionString %>" SelectCommand="SELECT DISTINCT [City] FROM [GridViewDemo] WHERE (([Country] = @Country) AND ([State] = @State))"> < SelectParameters > <asp:ControlParameter ControlID="DropDownList1" Name="Country" PropertyName =" =" String" /> <asp:ControlParameter ControlID="DropDownList2" Name="State" PropertyName =" =" String" /> < /SelectParameters > < /asp:SqlDataSource > <asp:Button ID="ButtonSearch" runat="server" onclick="ButtonSearch_Click" Text="Search" /> < asp:TextBox ID =" runat 服务器" < > <asp:Button ID="ButtonNameSearch" runat="server" onclick="ButtonNameSearch_Click" Text="Search" /> < /div > < div > < asp:GridView ID =" runat 服务器" AutoGenerateColumns 错误" DataKeyNames="ID" DataSourceID="GridviewSqlDataSource" onrowediting="GridView1_RowEditing"> < > < asp:CommandField ShowEditButton =" / <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly =" =" ID" / < asp:BoundField 数据字段 =" HeaderText 名称" SortExpression 名称" / <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" /> < asp:BoundField 数据字段 =" HeaderText 城市" SortExpression 城市" / < /列 > < /asp:GridView > <asp:SqlDataSource ID="GridviewSqlDataSource" runat="server" ConnectionString="<%
ConnectionStrings:GridviewDemoConnectionString %>" SelectCommand="SELECT DISTINCT [City] FROM [GridViewDemo] WHERE (([Country] = @Country) AND ([State] = @State))"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="Country" PropertyName="SelectedValue" Type="String" /> <asp:ControlParameter ControlID="DropDownList2" Name="State" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:SqlDataSource> <asp:Button ID="ButtonSearch" runat="server" onclick="ButtonSearch_Click" Text="Search" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="ButtonNameSearch" runat="server" onclick="ButtonNameSearch_Click" Text="Search" /> </div> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="GridviewSqlDataSource" onrowediting="GridView1_RowEditing"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" /> <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="GridviewSqlDataSource" runat="server" ConnectionString="<%


这篇关于更新进入第一记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆