各位大侠帮忙啊,我用GRIDVIEW绑定数据源控件SqlDataSource1后,通过改变SelectCommand来改变搜索结果,但是想编辑第一跳记录的时候却跳转到了搜索前的第一条记录,如何解决啊。
前台代码: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" DataSourceID="SqlDataSource1" GridLines="Vertical" CellPadding="3" ForeColor="Black" Height="1px" Width="637px">
<Columns>
<asp:ButtonField CommandName="SingleClick" HeaderText="SingleClick" Text="SingleClick"
Visible="False" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:TemplateField HeaderText="班级名" SortExpression="班级名">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Height="22px" Text='<%# Eval("班级名") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Height="20px" Text='<%# Eval("班级名") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="教材编号" SortExpression="教材编号">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Height="22px" Text='<%# Eval("教材编号") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Height="20px" Text='<%# Eval("教材编号") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="教材名" SortExpression="教材名">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Height="22px" Text='<%# Eval("教材名") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Height="20px" Text='<%# Eval("教材名") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单价" SortExpression="单价">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Height="22px" Text='<%# Eval("单价") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Height="20px" Text='<%# Eval("单价") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量" SortExpression="数量">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Height="22px" Text='<%# Eval("数量") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox5" runat="server" Height="20px" Text='<%# Eval("数量") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="总价" SortExpression="总价">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Height="22px" Text='<%# Eval("总价") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox6" runat="server" Height="20px" Text='<%# Eval("总价") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学年度" SortExpression="学年度">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Height="22px" Text='<%# Eval("学年度") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox7" runat="server" Height="20px" Text='<%# Eval("学年度") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="" LastPageText="" />
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:jiaocaiConnectionString8 %>"
DeleteCommand="DELETE FROM [classbook] WHERE [ID] = @ID" InsertCommand="INSERT INTO [classbook] ([班级名], [教材编号], [教材名], [单价], [数量], [总价], [学年度]) VALUES (@班级名, @教材编号, @教材名, @单价, @数量, @总价, @学年度)"
SelectCommand="SELECT * FROM [classbook]" UpdateCommand="UPDATE [classbook] SET [班级名] = @班级名, [教材编号] = @教材编号, [教材名] = @教材名, [单价] = @单价, [数量] = @数量, [总价] = @总价, [学年度] = @学年度 WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="班级名" Type="String" />
<asp:Parameter Name="教材编号" Type="String" />
<asp:Parameter Name="教材名" Type="String" />
<asp:Parameter Name="单价" Type="String" />
<asp:Parameter Name="数量" Type="String" />
<asp:Parameter Name="总价" Type="String" />
<asp:Parameter Name="学年度" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="班级名" Type="String" />
<asp:Parameter Name="教材编号" Type="String" />
<asp:Parameter Name="教材名" Type="String" />
<asp:Parameter Name="单价" Type="String" />
<asp:Parameter Name="数量" Type="String" />
<asp:Parameter Name="总价" Type="String" />
<asp:Parameter Name="学年度" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
前台代码: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" DataSourceID="SqlDataSource1" GridLines="Vertical" CellPadding="3" ForeColor="Black" Height="1px" Width="637px">
<Columns>
<asp:ButtonField CommandName="SingleClick" HeaderText="SingleClick" Text="SingleClick"
Visible="False" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:TemplateField HeaderText="班级名" SortExpression="班级名">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Height="22px" Text='<%# Eval("班级名") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Height="20px" Text='<%# Eval("班级名") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="教材编号" SortExpression="教材编号">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Height="22px" Text='<%# Eval("教材编号") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Height="20px" Text='<%# Eval("教材编号") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="教材名" SortExpression="教材名">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Height="22px" Text='<%# Eval("教材名") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Height="20px" Text='<%# Eval("教材名") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单价" SortExpression="单价">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Height="22px" Text='<%# Eval("单价") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Height="20px" Text='<%# Eval("单价") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量" SortExpression="数量">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Height="22px" Text='<%# Eval("数量") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox5" runat="server" Height="20px" Text='<%# Eval("数量") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="总价" SortExpression="总价">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Height="22px" Text='<%# Eval("总价") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox6" runat="server" Height="20px" Text='<%# Eval("总价") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学年度" SortExpression="学年度">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Height="22px" Text='<%# Eval("学年度") %>' Width="82px"></asp:Label>
<asp:TextBox ID="TextBox7" runat="server" Height="20px" Text='<%# Eval("学年度") %>'
Visible="False" Width="97px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="" LastPageText="" />
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:jiaocaiConnectionString8 %>"
DeleteCommand="DELETE FROM [classbook] WHERE [ID] = @ID" InsertCommand="INSERT INTO [classbook] ([班级名], [教材编号], [教材名], [单价], [数量], [总价], [学年度]) VALUES (@班级名, @教材编号, @教材名, @单价, @数量, @总价, @学年度)"
SelectCommand="SELECT * FROM [classbook]" UpdateCommand="UPDATE [classbook] SET [班级名] = @班级名, [教材编号] = @教材编号, [教材名] = @教材名, [单价] = @单价, [数量] = @数量, [总价] = @总价, [学年度] = @学年度 WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="班级名" Type="String" />
<asp:Parameter Name="教材编号" Type="String" />
<asp:Parameter Name="教材名" Type="String" />
<asp:Parameter Name="单价" Type="String" />
<asp:Parameter Name="数量" Type="String" />
<asp:Parameter Name="总价" Type="String" />
<asp:Parameter Name="学年度" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="班级名" Type="String" />
<asp:Parameter Name="教材编号" Type="String" />
<asp:Parameter Name="教材名" Type="String" />
<asp:Parameter Name="单价" Type="String" />
<asp:Parameter Name="数量" Type="String" />
<asp:Parameter Name="总价" Type="String" />
<asp:Parameter Name="学年度" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
{
private const int _firstEditCellIndex = 2; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Convert.ToString(Session["操作员姓名"]) == "")
{
Response.Write("<script language = javascript>alter ('请登陆');location = '../login.aspx'</script>");
return;
} this.GridView1.DataBind(); }
if (this.GridView1.SelectedIndex > -1)
{
this.GridView1.UpdateRow(this.GridView1.SelectedIndex, false);
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//从第一个单元格内获得LinkButton控件
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
//返回一个字符串,表示对包含目标控件的ID和事件参数的回发函数的Javascript调用
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
//给每一个可编辑的单元格增加事件
for (int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++)
{
//增加列索引作为事件参数
string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());
//给单元格增加onclick事件
e.Row.Cells[columnIndex].Attributes["onclick"] = js;
//给单元格增加鼠标经过时指针样式
e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
}
}
//this.GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender;
switch (e.CommandName)
{
case ("SingleClick"): //获得行索引
int _rowIndex = int.Parse(e.CommandArgument.ToString());
//解析事件参数(RowDataBound中增加的),从而获得被选中的列的索引
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
//设置GridView被选中的行的索引(每次回发后判断GridView1.SelectedIndex>-1则更新
_gridView.SelectedIndex = _rowIndex;
//绑定
_gridView.DataBind(); //获得被选中单元格的显示控件并设置其不可见
Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[1];
_displayControl.Visible = false;
//获得被选中单元格的编辑控件并设置其可见
Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[3];
_editControl.Visible = true;
//清除被选中单元格属性以删除click事件
_gridView.Rows[_rowIndex].Cells[_columnIndex].Attributes.Clear(); //设置焦点到被选中的编辑控件
ClientScript.RegisterStartupScript(GetType(), "SetFocus", "<script>document.getElementById('" + _editControl.ClientID + "').focus();</script>");
//编辑控件时TextBox则选中文本框内文本
if (_editControl is TextBox)
{
((TextBox)_editControl).Attributes.Add("onfocus", "this.select()");
}
break; }
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView _gridView = (GridView)sender;
string key = "";
string value = "";
//NewValues集合里的key
string[] _columnKeys = new string[] { "班级名","教材编号", "教材名", "单价", "数量", "总价", "学年度" }; if (e.RowIndex > -1)
{
//循环每一列
for (int i = _firstEditCellIndex; i < _gridView.Columns.Count; i++)
{
//获得单元格里的控件
Control _displayControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[1];
Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[3]; //获得列的key
key = _columnKeys[i - _firstEditCellIndex]; //如果单元格处于编辑模式的话,那么从编辑控件中获取值
if (_editControl.Visible)
{
if (_editControl is TextBox)
{
value = ((TextBox)_editControl).Text.ToString();
}
// 增加key/value对到NewValues集合
e.NewValues.Add(key, value);
}
else
{
value = ((Label)_displayControl).Text.ToString(); // 增加key/value对到NewValues集合
e.NewValues.Add(key, value);
}
}
}
}
//注册动态创建的客户端脚本
protected override void Render(HtmlTextWriter writer)
{
//RowDataBound中创建的自定义事件必须要在页中注册
//通过重写Render方法来调用ClientScriptManager.RegisterForEventValidation.
//通过GridViewRow.UniqueId返回行的唯一ID,按钮的唯一ID通过在行的唯一ID后附加“¥ct100”而生成。
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ct100", columnIndex.ToString());
}
}
}
base.Render(writer);
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "班级名")
{
this.SqlDataSource1.SelectCommand = "select * from classbook where 班级名 = '" + this.TextBox8.Text + "'";
this.SqlDataSource1.DataBind();
}
if (DropDownList1.SelectedValue == "教材名")
{
this.SqlDataSource1.SelectCommand = "select * from classbook where 教材名 = '" + this.TextBox8.Text + "'";
this.SqlDataSource1.DataBind();
} if (DropDownList1.SelectedValue == "学年度")
{
this.SqlDataSource1.SelectCommand = "select * from classbook where 出版社 = '" + this.TextBox8.Text + "'";
this.SqlDataSource1.DataBind();
}
}
if (this.GridView1.SelectedIndex > -1)
{ //先调用这个方法。 this.GridView1.UpdateRow(this.GridView1.SelectedIndex, false);
}
在page_load 里每次都执行