将筛选器应用于连接到GridView的linqdatasource后,单击编辑将我带回旧数据 [英] After applying a filter to linqdatasource connected to GridView ,clicking edit bring me back to the old data
问题描述
当我按下编辑按钮时,我会回到旧数据,第一行处于编辑模式
,如下所示
这是我使用的代码当搜索学生的名字,或日期....
LinqDataSource1.Where =pay_date.Contains(+
(Convert.ToString(Convert.ToDateTime(TextBox1.Text)))+);
我尝试使用AJAX,没有工作
我在linqdatasource中找到了一个属性所谓的linqdatasource storeoriginalvaluesinviewstate
我做了它的假但同样的问题,
.ascx
<%@ Control Language =C#AutoEventWireup =trueCodeFile =EDPayment.ascx.csInherits =Admin_ED_EDPayment%>
< asp:ScriptManagerProxy ID =ScriptManagerProxy1runat =server>
<服务>
< asp:ServiceReference Path =〜/ Admin / ED / Student_AutoComplete.asmx/>
< /服务>
< / asp:ScriptManagerProxy>
< br />
< asp:UpdatePanel ID =UpdatePanel1runat =server>
< ContentTemplate>
< div>
< asp:Label ID =Label3runat =server
Text =按学生名称搜寻:>< / asp:Label>
< asp:TextBox runat =serverID =myTextBoxWidth =300ontextchanged =myTextBox_TextChanged/>
< asp:autocompleteextender
runat =server
ID =autoComplete1
TargetControlID =myTextBox
ServicePath =〜/ Admin / ED / Student_AutoComplete .asmx
ServiceMethod =GetCompletionList
MinimumPrefixLength =1
CompletionInterval =1000
EnableCaching =true
CompletionSetCount =12/> gt ;
& nbsp;< asp:Button ID =Button1runat =serveronclick =Button1_Click
Text =Search/>
& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; NBSP;
< br />
< br />
< asp:Label ID =Label4runat =serverText =Search By Date:>< / asp:Label>
< asp:TextBox ID =TextBox1runat =serverontextchanged =TextBox1_TextChanged>< / asp:TextBox>
< asp:CalendarExtender ID =TextBox1_CalendarExtenderrunat =server
BehaviorID =TextBox1_CalendarExtenderEnabled =True
TargetControlID =TextBox1>
< / asp:CalendarExtender>
& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP; &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;
< asp:Button ID =Button2runat =serveronclick =Button2_ClickText =Search/>
< br />
< br />
< asp:Button ID =Button3runat =serveronclick =Button3_Click
Text =按日期查找& amp; amp; amp; amp; amp; amp; amp; amp; amp;
< br />
< asp:Label ID =Label5runat =server>< / asp:Label>
< br />
< asp:Button ID =Button4runat =serveronclick =Button4_Click
Text =显示所有付款/>< / div>
< asp:GridView ID =GridView1runat =serverAllowPaging =True
AllowSorting =TrueAutoGenerateColumns =FalseDataKeyNames =pay_id
DataSourceID =LinqDataSource1onrowediting =GridView1_RowEditing>
<列>
< asp:CommandField ShowDeleteButton =TrueShowEditButton =True/>
< asp:BoundField DataField =pay_idHeaderText =pay_idInsertVisible =False
ReadOnly =TrueSortExpression =pay_id/>
< asp:BoundField DataField =pay_amountHeaderText =pay_amount
SortExpression =pay_amount/>
< asp:BoundField DataField =pay_dateHeaderText =pay_date
SortExpression =pay_date/>
< asp:TemplateField HeaderText =pay_st_idSortExpression =pay_st_id>
< EditItemTemplate>
< asp:DropDownList ID =DropDownList1runat =server
DataSourceID =LinqDataSource2DataTextField =st_fullname
DataValueField =st_idSelectedValue ='<%#绑定(pay_st_id)%>'>
< / asp:DropDownList>
< asp:LinqDataSource ID =LinqDataSource2runat =server
ContextTypeName =TeacherAssistantDataContext
OrderBy =st_fname,st_mname,st_lnameSelect =new(st_fullname,st_id)
TableName =students>
< / asp:LinqDataSource>
< / EditItemTemplate>
< ItemTemplate>
< asp:Label ID =Label1runat =serverText ='<%#Bind(pay_st_id)%>'
Visible =False>< ASP:标签>
< asp:DropDownList ID =DropDownList2runat =server
DataSourceID =LinqDataSource3DataTextField =st_fullname
DataValueField =st_idEnabled =False>
< / asp:DropDownList>
ContextTypeName =TeacherAssistantDataContext
OrderBy =st_fname,st_mname,st_lnameSelect =new(st_id,st_fullname)
TableName =studentsWhere =st_id == @st_id>
< WhereParameters>
< asp:ControlParameter ControlID =Label1Name =st_idPropertyName =Text
Type =Int32/>
< / WhereParameters>
< / asp:LinqDataSource>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>
< asp:LinqDataSource ID =LinqDataSource1runat =server
ContextTypeName =TeacherAssistantDataContextEnableDelete =True
EnableUpdate =TrueOrderBy =pay_date,pay_amount,pay_st_id
TableName =payments>
< / asp:LinqDataSource>
< / ContentTemplate>
< / asp:UpdatePanel>
.ascx.cs
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
public partial class Admin_ED_EDPayment:System.Web.UI.UserControl
{
protected void Page_Load(object sender,EventArgs e)
{
}
protected void Button1_Click(object sender,EventArgs e)
{
go1();
}
protected void go1()
{
int [] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
LinqDataSource1.Where =pay_st_id ==+ id [0] .ToString();
//参数p = new Parameter(,System.Data.DbType.Int32,id [0] .ToString());
//LinqDataSource1.WhereParameters.Add(p);
// LinqDataSource1.DataBind();
// fix();
$ b protected void GridView1_SelectedIndexChanged(object sender,EventArgs e)
{
}
protected void Button2_Click(object sender,EventArgs e )
{
go2();
}
protected void go2()
{
LinqDataSource1.Where =pay_date == DateTime.Parse(\+ TextBox1.Text +\) ;
fix();
}
void fix()
{
// LinqDataSource1。
protected void Button3_Click(object sender,EventArgs e)
{
int [] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
LinqDataSource1.Where =pay_st_id ==+ id [0] .ToString()+AND+pay_date == DateTime.Parse(\+ TextBox1.Text +\) ;
protected void myTextBox_TextChanged(object sender,EventArgs e)
{
go1();
protected void TextBox1_TextChanged(object sender,EventArgs e)
{
go2();
protected void Button4_Click(object sender,EventArgs e)
{
LinqDataSource1.TableName =payments;
LinqDataSource1.DataBind();
protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
$ b}
}
我发现了一个基于这个主题的解决方案:防止LinqDataSource Where子句在回发时重置? :来源
我所需要的是保存linqdatasource的where子句,并在page_load事件中重新加载它,最终代码变为
pre $ 使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
public partial class Admin_ED_EDPayment:System.Web.UI.UserControl
{
// private Boolean b1 = false,b2 = false,b3 = false;
public string MyLinqSourceWhere
{
get {return(string)this.ViewState [MyLinqSourceWhere]; }
set {this.ViewState [MyLinqSourceWhere] = value; }
}
protected void Page_Load(object sender,EventArgs e)
{
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
protected void Button1_Click(object sender,EventArgs e)
{
go1();
}
protected void go1()
{
int [] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
this.MyLinqSourceWhere =pay_st_id ==+ id [0] .ToString();
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
}
protected void go3()
{
int [] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
this.MyLinqSourceWhere =pay_st_id ==+ id [0] .ToString()+AND+pay_date == DateTime.Parse(\+ TextBox1.Text +\) ;
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
protected void Button2_Click(object sender,EventArgs e)
{
go2();
}
protected void go2()
{
this.MyLinqSourceWhere =pay_date == DateTime.Parse(\+ TextBox1.Text +\) ;
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
protected void Button3_Click(object sender,EventArgs e)
{
go3();
protected void myTextBox_TextChanged(object sender,EventArgs e)
{
go1();
protected void TextBox1_TextChanged(object sender,EventArgs e)
{
go2();
protected void Button4_Click(object sender,EventArgs e)
{
LinqDataSource1.TableName =payments;
LinqDataSource1.DataBind();
}
}
Like once said a picture worth one thousand word
when I press the edit button I go back to the old data, with first row in the edit mode like the following
this is the code I use when searching with student name , or date ....
LinqDataSource1.Where = "pay_date.Contains(" +
(Convert.ToString(Convert.ToDateTime(TextBox1.Text))) + ")";
I tried to use AJAX , didn't work
I found in the linqdatasource a property called linqdatasource storeoriginalvaluesinviewstate
I made it false but go the same problem ,
.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EDPayment.ascx.cs" Inherits="Admin_ED_EDPayment" %>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Services>
<asp:ServiceReference Path="~/Admin/ED/Student_AutoComplete.asmx" />
</Services>
</asp:ScriptManagerProxy>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:Label ID="Label3" runat="server"
Text="Search By Student Name:"></asp:Label>
<asp:TextBox runat="server" ID="myTextBox" Width="300" ontextchanged="myTextBox_TextChanged" />
<asp:autocompleteextender
runat="server"
ID="autoComplete1"
TargetControlID="myTextBox"
ServicePath="~/Admin/ED/Student_AutoComplete.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="1"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="12" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Search" />
<br />
<br />
<asp:Label ID="Label4" runat="server" Text="Search By Date:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server"
BehaviorID="TextBox1_CalendarExtender" Enabled="True"
TargetControlID="TextBox1">
</asp:CalendarExtender>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Search" />
<br />
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
Text="Search By Date & Name" />
<br />
<asp:Label ID="Label5" runat="server"></asp:Label>
<br />
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
Text="Show All Payments" /></div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="pay_id"
DataSourceID="LinqDataSource1" onrowediting="GridView1_RowEditing">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="pay_id" HeaderText="pay_id" InsertVisible="False"
ReadOnly="True" SortExpression="pay_id" />
<asp:BoundField DataField="pay_amount" HeaderText="pay_amount"
SortExpression="pay_amount" />
<asp:BoundField DataField="pay_date" HeaderText="pay_date"
SortExpression="pay_date" />
<asp:TemplateField HeaderText="pay_st_id" SortExpression="pay_st_id">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="LinqDataSource2" DataTextField="st_fullname"
DataValueField="st_id" SelectedValue='<%# Bind("pay_st_id") %>'>
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource2" runat="server"
ContextTypeName="TeacherAssistantDataContext"
OrderBy="st_fname, st_mname, st_lname" Select="new (st_fullname, st_id)"
TableName="students">
</asp:LinqDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("pay_st_id") %>'
Visible="False"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="LinqDataSource3" DataTextField="st_fullname"
DataValueField="st_id" Enabled="False">
</asp:DropDownList>
<asp:LinqDataSource ID="LinqDataSource3" runat="server"
ContextTypeName="TeacherAssistantDataContext"
OrderBy="st_fname, st_mname, st_lname" Select="new (st_id, st_fullname)"
TableName="students" Where="st_id == @st_id">
<WhereParameters>
<asp:ControlParameter ControlID="Label1" Name="st_id" PropertyName="Text"
Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="TeacherAssistantDataContext" EnableDelete="True"
EnableUpdate="True" OrderBy="pay_date, pay_amount, pay_st_id"
TableName="payments">
</asp:LinqDataSource>
</ContentTemplate>
</asp:UpdatePanel>
.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_ED_EDPayment : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
go1();
}
protected void go1()
{
int[] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
LinqDataSource1.Where = "pay_st_id == " + id[0].ToString();
// Parameter p = new Parameter("", System.Data.DbType.Int32, id[0].ToString());
//LinqDataSource1.WhereParameters.Add(p);
// LinqDataSource1.DataBind();
//fix();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
go2();
}
protected void go2()
{
LinqDataSource1.Where = "pay_date == DateTime.Parse(\"" + TextBox1.Text + "\")";
fix();
}
void fix()
{
// LinqDataSource1.
}
protected void Button3_Click(object sender, EventArgs e)
{
int[] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
LinqDataSource1.Where = "pay_st_id == " + id[0].ToString() + " AND " + "pay_date == DateTime.Parse(\"" + TextBox1.Text + "\")";
}
protected void myTextBox_TextChanged(object sender, EventArgs e)
{
go1();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
go2();
}
protected void Button4_Click(object sender, EventArgs e)
{
LinqDataSource1.TableName = "payments";
LinqDataSource1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
}
I found a solution based on this topic : How can I prevent the LinqDataSource Where clause from resetting on postback? :Source All I needed is to save the where clause of the linqdatasource and reload it again in the page_load event the final code become
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_ED_EDPayment : System.Web.UI.UserControl
{
//private Boolean b1 = false, b2 = false, b3 = false;
public string MyLinqSourceWhere
{
get { return (string)this.ViewState["MyLinqSourceWhere"]; }
set { this.ViewState["MyLinqSourceWhere"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
}
protected void Button1_Click(object sender, EventArgs e)
{
go1();
}
protected void go1()
{
int[] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
this.MyLinqSourceWhere = "pay_st_id == " + id[0].ToString();
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
}
protected void go3()
{
int[] id = Searcher._Student.searchByst_fullName2(myTextBox.Text);
this.MyLinqSourceWhere = "pay_st_id == " + id[0].ToString() + " AND " + "pay_date == DateTime.Parse(\"" + TextBox1.Text + "\")";
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
}
protected void Button2_Click(object sender, EventArgs e)
{
go2();
}
protected void go2()
{
this.MyLinqSourceWhere = "pay_date == DateTime.Parse(\"" + TextBox1.Text + "\")";
this.LinqDataSource1.Where = this.MyLinqSourceWhere;
}
protected void Button3_Click(object sender, EventArgs e)
{
go3();
}
protected void myTextBox_TextChanged(object sender, EventArgs e)
{
go1();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
go2();
}
protected void Button4_Click(object sender, EventArgs e)
{
LinqDataSource1.TableName = "payments";
LinqDataSource1.DataBind();
}
}
这篇关于将筛选器应用于连接到GridView的linqdatasource后,单击编辑将我带回旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!