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"
Compl开发者_运维问答etionSetCount="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 think you have to put your code in the page load and not in the button click and then you check first if the textbox contains data and if it contains data, apply your filter.
I think your problem is because when the page loads for the second time, the datagrid datbind function get called which gets all the data again.
try this update
<asp:LinqDataSource ID="LinqDataSource3" runat="server"
ContextTypeName="TeacherAssistantDataContext" OnSelected="LinqDataSource1_Selected"
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>
Code behind:
protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
if(!string.IsNullOrEmpty( myTextBox.Text))
{
go1();
}
if(!string.IsNullOrEmpty( TextBox1.Text))
{
go2();
}
}
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();
}
}
精彩评论