How to refresh a gridview with ajax? - asp.net

I want to refresh a GridView after selecting values in an AJAX ModalPopup. But the refresh happens only after clicking again on the select button. The refresh should happen straight after click on OK... What do I am wrong?
Greets
Marco
<div id="container" runat="server" />
<input id="dummy" type="button" style="display:none" runat="server"/>
<ajaxToolkit:ModalPopupExtender runat="server"
ID="mpeThePopup"
TargetControlID="dummy"
PopupControlID="pnlModalPopUpPanel"
DropShadow="true"/>
<asp:Panel ID="pnlModalPopUpPanel" runat="server">
<asp:UpdatePanel ID="updatePanel2" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:ListBox ID="availableCategories" runat="server" DataTextField="CategoryName" DataValueField="CategoryID" SelectionMode="Multiple"/>
<asp:Button ID="moveRight" runat="server" Text=">" OnClick="moveRightClick" />
<asp:Button ID="moveLeft" runat="server" Text="<" OnClick="moveLeftClick" />
<asp:ListBox ID="selectedCategories" runat="server" DataTextField="CategoryName" DataValueField="CategoryID" SelectionMode="Multiple"/>
<asp:Button ID="okButton" runat="server" Text="OK" OnClick="okClick" />
<asp:Button ID="cancelButton" runat="server" Text="Cancel" OnClick="cancelClick" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="okButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class Select : System.Web.UI.UserControl
{
private ObjectSet<Category> osCategories;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
NorthwindEntities northwindEntities = new NorthwindEntities();
categories.DataSource = northwindEntities.Categories;
availableCategories.DataSource = northwindEntities.Categories;
DataBind();
}
Button showModal = new Button();
showModal.Text = "Select";
showModal.Click += new EventHandler(showModal_Click);
container.Controls.Add(showModal);
}
void showModal_Click(object sender, EventArgs e)
{
mpeThePopup.Show();
}
protected void moveRightClick(object sender, EventArgs e)
{
foreach(ListItem listItem in availableCategories.Items)
{
if(listItem.Selected)
{
selectedCategories.Items.Add(listItem);
//availableCategories.Items.Remove(listItem);
}
}
}
protected void moveLeftClick(object sender, EventArgs e)
{
foreach(ListItem listItem in selectedCategories.Items)
{
if(listItem.Selected)
{
availableCategories.Items.Add(listItem);
//selectedCategories.Items.Remove(listItem);
}
}
}
protected void okClick(object sender, EventArgs e)
{
categories.DataSource = null;
categories.DataBind();
categories.DataSource = availableCategories.SelectedItem;
categories.DataBind();
mpeThePopup.Hide();
}
protected void cancelClick(object sender, EventArgs e)
{
mpeThePopup.Hide();
}
}
}

Place your categories GridView inside an UpdatePanel with a Click AsyncPostBackTrigger on the okButton.

Related

asp.net button click timer not start

i want to run timer_Tick event on button click and stop timer pressing another button. this is my code
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label2.Text = "Timer started";
//string png = IpAddress.Text.ToString(); //"192.168.153.12";//
if (IpAddress.Text == "" || IpAddress.Text == null)
{
Response.Write("Error");
}
else
{
ping1(IpAddress.Text);
ping2(Ipaddres1.Text);
}
}
Use update panel and write code Something like this.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click1" />
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2000" Enabled="False">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
And code behind,
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Timer started";
//string png = IpAddress.Text.ToString(); //"192.168.153.12";//
if (IpAddress.Text == "" || IpAddress.Text == null)
{
Label2.Text = "Error!!!";
}
else
{
ping1(IpAddress.Text);
ping2(Ipaddres1.Text);
}
}

asp button on click inside item template not firing

Not able to make this work.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick ="btnApprove_Click" />
</ItemTemplate>
</asp:TemplateField>
code behind:
protected void btnApprove_Click(object sender, EventArgs e)
{
Response.Redirect("viewprofile.aspx");
}
not even firing when button is clicked. any tricks on this?
Set EnableEventValidation="false" right at the top in your Page directive:
<%# Page EnableEventValidation="false" Language="C#"...
Just beware that setting this value to false can expose your website to security vulnerabilities.As an alternative, instead of setting EnableEventValidation="false" you can handle the grid views OnRowCommand:
.ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Approve" CommandName="Approve" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
public partial class delete_me : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//THIS IS IMPORTANT.GridView1_RowCommand will not fire unless you add this line
{
var p1 = new Person() { Name = "Person 1" };
var p2 = new Person() { Name = "Person 2" };
var list = new List<Person> { p1, p2 };
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Diagnostics.Debugger.Break();
}
}
public class Person
{
public string Name { get; set; }
}
You Just put on your gridview.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
</ItemTemplate>
</asp:TemplateField>
Also put code behind
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
Try!!!! it's working fine...

How to close radwindow manager form server side button click

Hi all I have designed a radwindow manager which will have a dropdown and a textbox and a button which are placed inside a update panel.
Every thing works fine but on button click I am closing my window manager as follows
RadWindowManager1.Windows[0].VisibleOnPageLoad = false;
But this didn't work can some one help me this is my design and code
<telerik:RadWindowManager ID="RadWindowManager1" runat="server" Behavior="Default"
InitialBehavior="None">
<Windows>
<telerik:RadWindow ID="RadWindow1" runat="server" Behaviors="Default" InitialBehaviors="None"
OpenerElementID="btn" Width="650" Height="480" VisibleOnPageLoad="false">
<ContentTemplate>
<asp:UpdatePanel ID="up" runat="server" OnUnload="up_Unload">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdcmb" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="radbtn" EventName="Click" />
</Triggers>
<ContentTemplate>
<telerik:RadComboBox ID="rdcmb" runat="server" AutoPostBack="true" OnSelectedIndexChanged="sel">
<Items>
<telerik:RadComboBoxItem Text="One" Value="One" />
<telerik:RadComboBoxItem Text="Two" Value="Two" />
<telerik:RadComboBoxItem Text="Three" Value="Three" />
<telerik:RadComboBoxItem Text="Four" Value="Four" />
</Items>
</telerik:RadComboBox>
<telerik:RadTextBox ID="rdText" runat="server">
</telerik:RadTextBox>
<telerik:RadButton ID="radbtn" runat="server" Text="Save" OnClick="btn_Click">
</telerik:RadButton>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</telerik:RadWindow>
</Windows>
</telerik:RadWindowManager>
<telerik:RadButton ID="rdbtnwhAnother" Text="Add WithHolding" runat="server" ButtonType="LinkButton"
OnClick="rdbtnwhAnother_Click">
</telerik:RadButton>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadWindowManager1.Windows[0].VisibleOnPageLoad = false;
}
}
protected void rdbtnwhAnother_Click(object sender, EventArgs e)
{
RadWindowManager1.Windows[0].VisibleOnPageLoad = true;
}
protected void btn_Click(object sender, EventArgs e)
{
RadWindowManager1.Windows[0].VisibleOnPageLoad = false;
}
protected void sel(object sender, EventArgs e)
{
rdText.Text = rdcmb.SelectedItem.Text;
}
protected void up_Unload(object sender, EventArgs e)
{
/* Cast sender as an updatePanel, and use reflection to invoke * * the page's scriptmanger registerUpdatePanel() method * * */
//RadWindowManager1.Windows[0].VisibleOnPageLoad = false;
UpdatePanel aUpdatePanel = sender as UpdatePanel;
MethodInfo m = (
from methods in typeof(ScriptManager).GetMethods(
BindingFlags.NonPublic | BindingFlags.Instance
)
where methods.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")
select methods).First<MethodInfo>();
m.Invoke(ScriptManager.GetCurrent(aUpdatePanel.Page), new object[] { aUpdatePanel });
}
<script type="text/javascript">
function getRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function clientClose(arg) {
getRadWindow().close(arg);
}
</script>
<telerik:RadButton ID="radbtn" runat="server" Text="Save" OnClick="btn_Click">
</telerik:RadButton>
protected void btn_Click(object sender, EventArgs e)
{
string script = "clientClose('');";
ScriptManager.RegisterStartupScript(Page, typeof(Page),
"closeScript", script, true);
}

update the textbox text property through loop

I want to update the text property of my textbox control in Asp.Net from within loop.I have tried using Ajax updatepanel with the timer control but unable to update the text property.I have been searching this since last week but unable to find any answer any help would be higly appreciated
Following is the detail:
This is my button click event in this code you can see I am updating the
TxtContent.Text
within a loop the text is being returned by a user defined function called
ReturnBodyText()
afterwards I am assigning the text to the viewstate so that at the timer tick event I can recall the text and update the textbox text property and then at the timer tick event assigning the viewstate values to the textbox.
protected void Button4_Click(object sender, EventArgs e)
{
ArrayList FilePath = (ArrayList)ViewState["ArrayList"];
int i = 0;
int b = 1;
foreach(string[] sr in FilePath)
{
string Month = sr[i];
string datewithzero;
datewithzero = String.Format("{0:00}", b);
string[] FilesArray = (string[])listfiles(Month, datewithzero).ToArray(typeof(string));
foreach (string FileNameWithExtension in FilesArray)
{
ListBox4.Items.Add(FileNameWithExtension);
TxtContent.Text = ReturnBodyText(Month, datewithzero, FileNameWithExtension);
ViewState["Content"] = TxtContent.Text;
Timer1.Enabled = true;
Timer1_Tick(ViewState["Content"], e);
}
i = i + 1;
b = b + 1;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
TxtContent.Text =(string) ViewState["Content"];
TxtContent.DataBind();
}
I Hope the above description will help you guys in understanding my problem
The complete aspx file and the code behind is as follows:
Aspx Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:ListBox ID="ListBox1" runat="server" Height="509px" Width="59px">
</asp:ListBox>
<asp:Button ID="Button2" runat="server" Height="26px" onclick="Button2_Click"
Text="To be pressed first" Width="130px" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
Text="To be pressed 2nd" Width="131px" Height="26px" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000"
ontick="Timer1_Tick" Enabled="False">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TxtContent" runat="server" Height="508px" AutoPostBack="True"
TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Height="26px" onclick="Button4_Click"
Text="To be pressed Third" Width="122px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
- Codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Collections;
public partial class _Default : ftp
{
protected void Button2_Click(object sender, EventArgs e)
{
string[] array = (string[])listfiles().ToArray(typeof(string));
ViewState["FolderName"] = array;
foreach (string FileName in array)
{
ListBox1.Items.Add(FileName);
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string[] arraylistbox2 = (string[])ViewState["FolderName"];
string[] arrays = new string[12];
ArrayList ListPath = new ArrayList();
int dateFolder;
foreach (string FileName in arraylistbox2)
{
dateFolder = Convert.ToInt16(FileName);
dateFolder = dateFolder - 1;
ListPath.Add((string[])(listfiles(FileName).ToArray(typeof(string))));
}
ViewState["ArrayList"] = ListPath;
}
protected void Button4_Click(object sender, EventArgs e)
{
ArrayList FilePath = (ArrayList)ViewState["ArrayList"];
int i = 0;
int b = 1;
foreach(string[] sr in FilePath)
{
string Month = sr[i];
string datewithzero;
datewithzero = String.Format("{0:00}", b);
string[] FilesArray = (string[])listfiles(Month, datewithzero).ToArray(typeof(string));
foreach (string FileNameWithExtension in FilesArray)
{
TxtContent.Text = ReturnBodyText(Month, datewithzero, FileNameWithExtension);
ViewState["Content"] = TxtContent.Text;
Timer1.Enabled = true;
Timer1_Tick(ViewState["Content"], e);
}
i = i + 1;
b = b + 1;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
TxtContent.Text =(string) ViewState["Content"];
TxtContent.DataBind();
}
Sadly, your code did not help me much. However, I wrote this which I believe does what you want -partially-. It seems to me nothing more can be done though. Here is the code:
.aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer runat="server" ID="Timer1" Interval="1000" Enabled="true"
ontick="Timer1_Tick" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
aspx.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected static string keeper;
protected static bool inUse = false;
protected void Page_Load(object sender, EventArgs e) {}
protected void Button1_Click(object sender, EventArgs e)
{
Thread worker = new Thread(new ThreadStart(workerFunction));
worker.Start();
return;
}
protected void workerFunction()
{
inUse = true;
for (int i = 0; i < 3; i++)
{
TextBox1.Text += "foo";
keeper = TextBox1.Text;
Thread.Sleep(1000);
}
inUse = false;
keeper = "";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
if (inUse)
{
TextBox1.Text = Convert.ToString(keeper);
}
}
}
}

After applying a filter to linqdatasource connected to GridView ,clicking edit bring me back to the old data

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 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();
}
}

Resources