refresh gridview after search - asp.net

I have a search on a grid view which limits the results. I would like the grid view to repopulate with all entries if a. the search box is empty or b. the user hits a button to refresh.
Protected Sub btnSeach_Click(sender As Object, e As EventArgs) Handles btnSeach.Click
StaffDetailsStaffGridView.DataSourceID = ""
StaffDetailsStaffGridView.DataSource = ObjectDataSource1
StaffDetailsStaffGridView.DataBind()
If txtFnameSearch.text = " " Then
StaffDetailsStaffGridView.DataBind()
End If
End Sub
Protected Sub btnRefreshSearch_Click(sender As Object, e As EventArgs) Handles btnRefreshSearch.Click
StaffDetailsStaffGridView.DataBind()
End Sub
End Class
StaffDetailsStaffGridView.DataBind() obviously doesn't work.
how do I do this properly?

The best way to repopulate the GridView is by having a method specifically for binding your data and calling when needed.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostback)
BindGrid();
}
private void BindGrid()
{
StaffDetailsStaffGridView.DataSource = ObjectDataSource1;
StaffDetailsStaffGridView.DataBind();
}
protected void btnRefreshSearch_Click(object sender, EventArgs e)
{
BindGrid();
}
protected void btnSeach_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtFnameSearch.text))
{
BindGrid();
}
}
I'm assuming you are filtering your data directly via the ObjectDataSource.

You should get the datasource again and set it as the DataSource and then bind again.
Something like this
Protected Sub btnRefreshSearch_Click(sender As Object, e As EventArgs) Handles btnRefreshSearch.Click
Dim searchKey as String
searchKey =txtFnameSearch.Text.Trim()
Dim staffSearchREsults=MyService.GetSearchResults(searchKey)
StaffDetailsStaffGridView.DataSource = staffSearchREsults
StaffDetailsStaffGridView.DataBind()
End Sub
Assuming MyService.GetSearchResults method will return you a valid search result based on the search key.

This will not work because, after postback, gridview's datasource will be lost. So you always need to set datasource before DataBind() is called.
StaffDetailsStaffGridView.DataSource = ObjectDataSource1
StaffDetailsStaffGridView.DataBind()
You can also save ObjectDataSource1 into session and use it later for binding:
Session["MyObjectDataSource"] = ObjectDataSource1;
...
Protected Sub btnRefreshSearch_Click(sender As Object, e As EventArgs) Handles btnRefreshSearch.Click
StaffDetailsStaffGridView.DataSource = Session["MyObjectDataSource"]
StaffDetailsStaffGridView.DataBind()
End Sub

If you're using a declarative datasource, you can just call DataBind() again on the GridView:
StaffDetailsStaffGridView.DataBind()

Related

itemdatabound event of a nested listview

I have a nested listview which I databind on the parent 'ItemDataBound' event, but how do i access/register the nested listview's itemdatabound event?
Thanks!
Edits
My parent listview itemdatabound now looks like so,
Protected Sub lvwManagePolicy_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvwManagePolicy.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim rv As DataRowView = CType(e.Item.DataItem, DataRowView)
Me.dsAccoutnTransactionHistory = Wrap.getWrapAccountTransactionHistory(rv!PLATFORM_ID, False)
Dim lvwTransactionHistory As ListView = DirectCast(e.Item.FindControl("lvwTransactionHistory"), ListView)
lvwTransactionHistory.ItemDataBound += New EventHandler(Of ListViewItemEventArgs)(lvwTransactionHistory_ItemDataBound)
lvwTransactionHistory.DataSource = dsAccoutnTransactionHistory
lvwTransactionHistory.DataBind()
End If
End Sub
but i get an error
BC32022: 'Public Event ItemDataBound(sender As Object, e As
System.Web.UI.WebControls.ListViewItemEventArgs)' is an event, and
cannot be called directly. Use a 'RaiseEvent' statement to raise an
event.
Before assigning the data to nested control in your parent control you can register the event like below under your parent ItemBoundData
ListView f = new ListView();
f.ItemDataBound += new EventHandler<ListViewItemEventArgs>(f_ItemDataBound);
protected void f_ItemDataBound(object sender, ListViewItemEventArgs e)
{
}
You can this :
<asp:ListView onitemcommand="inner_ItemCommand" ...
protected / public item command method need :
public void inner_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandArgument == "delete")
{
//do delete here
}
}

The GridView 'GridView1' fired event PageIndexChanging which wasn't handled

I have created:
one master page and one content page called Detail.
On Button click event, displaying data in grid view.
In grid view, columns are autogenerated.
I wanted to show 11 column in grid view, but it is more than page
size.
What to do for this?
I have created sql helper file for database connection code and calling that method, not using sqldatasource for connection.
When I trying to do paging, getting error:
The GridView 'GridView1' fired event PageIndexChanging which wasn't
handled.
You need to declare a method on your code behind that handles the PageIndexChanging event.
Something similar to this:
protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
private void bindGridView()
{
GridView1.DataSource=getData();
GridView1.DataBind();
}
Providing sample code:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
protected void Page_Load(object sender , EventArgs e)
{
if(!IsPostBack)
bindGridView();
}
//this is some sample data
private void bindGridView()
{
DataTable t = new DataTable();
t.Columns.Add("Col1");
t.Columns.Add("Col2");
DataRow r = null;
for (int i = 0; i < 25; i++)
{
r = t.NewRow();
r.ItemArray = new object[] { "Val" + i, " Another " + i };
t.Rows.Add(r);
}
GridView1.DataSource = t;
GridView1.DataBind();
}
And this is the markup:
<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">
Produces this:
For Paging you can use OnPageIndexChanging for this....
For Example
you have to use OnPageIndexChanging="gvdetails_PageIndexChanging" in your GridView...
You have to write below code into event in code behind like
protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvdetails.PageIndex = e.NewPageIndex;
BindData();
}
For more detail you can check the below link here I am using page Index change in my article...
Here I use PageIndexChange
I hope this will helps you....Share it with others...Thanks!
This is the final answer:
Imports System.Collections.Generic ' library
Protected Sub grdEmployees_PageIndexChanging1(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdEmployees.PageIndexChanging
grdEmployees.PageIndex = e.NewPageIndex
LoadEmployeeList() 'FUNCTION FOR DATASET
grdEmployees.DataBind()
End Sub
you simply add this to your code :
protected void GridViewTrsEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewTrsEmail.PageIndex = e.NewPageIndex;
GridViewTrsEmail.DataBind();
}
To fix this, I had to take a closer look at my datasource and datakeys. I have a set of records that are returned from SQL Server and what I was doing is binding them to a POCO. This class had several public properties of type Integer. These Integers were my datakeys on the grid. I replaced their type with a string instead to bypass the casting issue.

RiseEvent from usercontrol or communicate between usercontrols

I have problem with bubbleup event from my dynamic loaded ascx control (katalogbooklist.ascx) to it´s parent control (ViewAJBarnboksKatalog.ascx)
i need to fire/run sub uppdateraAndraModuler in the parent control when the event addMultiVotes_command fires in the chiled control.
is there any one who knows or have an idea on how to do this?
/
Andreas
(the code lives in a dotnetnuke cms modules, if that's any help)
Partial Class ViewAJBarnboksKatalog '<--------Partial codebehind file for ViewAJBarnboksKatalog.ascx
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
.. code for adding for loading katalogbooklist.ascx dynamic...
Me.Controls.Add(..code.. add katalogbooklist.ascx ..) '
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As Execkoden = New Execkoden()
AddHandler t.onAjeventtest, AddressOf Uppdateramod
End Sub
Public Sub Uppdateramod(sender As Object, e As ajeventArgs)
uppdateraAndraModuler()
End Sub
Public Sub uppdateraAndraModuler()
'..do some code
End Sub
End Class
Partial Class katalogenBookList '<--------Partial codebehind file for katalogbooklist.ascx
Protected Sub addMultiVotes_command(ByVal sender As Object, ByVal e As System.EventArgs)
'..more code...
Dim te As New Execkoden ' <----- i want to use the constructor to raise the event in class Execkoden can´t raiseevent directly it wont´t fire
'... more code...
End sub
End Class
Public Class ajeventArgs : Inherits EventArgs
Public Sub New()
End Sub
End Class
Public Delegate Sub Uppdatera(sender As Object, e As ajeventArgs)
Public Class Execkoden
Public Event onAjeventtest As Uppdatera
Public Sub New()
RaiseEvent onAjeventtest(Me, New ajeventArgs)
End Sub
End Class
Create an event handler in the child control, like this:
public event EventHandler DeleteButtonClick;
When a button is clicked in the child control, do this:
protected void DeleteClick(object sender, EventArgs e)
{
if (this.DeleteButtonClick != null)
this.DeleteButtonClick(sender, e);
}
And in your parent control, in the markup:
<UC:SomeUserControl ID="UserControl1" runat="server" OnDeleteButtonClick="UserControl1_DeleteClick" ...>
And in the code behind of the parent control:
protected void UserControl1_DeleteClick(object sender, EventArgs e)
{
//do something
}
I would suggest using the IMC or Inter Module Communication feature that's built in to DotNetNuke.

Handle Click Event for LinkButton in User Control from Parent ASP.NET page

I have a LinkButton within a User Control and it has handled with:
Private Sub LoginLinkLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkLinkButton.Click
Response.Redirect("/", True)
End Sub
On certain ASPX pages I would like to handle the click from the page's code behind, opposed to within the user control. How do I override the handle within the control from the parent's code behind page?
Update:
Based on the answer, I have the updated the User Control:
Public Event LoginLinkClicked As OnLoginClickHandler
Public Delegate Sub OnLoginClickHandler(ByVal sender As Object, ByVal e As EventArgs)
[...]
Private Sub LoginLinkLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkLinkButton.Click
**If OnLoginClickHandler IsNot Nothing Then**
RaiseEvent LoginLinkClicked(Me, e)
Else
Response.Redirect("/", True)
End If
End Sub
The problem is determining the correct syntax for the if line because the above is invalid.
You'll have to expose a new event from the user control. Apologies as the following code is all in C# and it's been a long time since I touched VB.Net, but you should get the idea:
You can use a delegate event by adding the following to your UserControl:
public event OnLoginClickHandler LoginClick;
public delegate void OnLoginClickHandler (object sender, EventArgs e);
Then call the following to your LinkButton Click event:
protected void LoginLinkLinkButton_Click(object sender, EventArgs e)
{
// Only fire the event if there's a subscriber
if (OnLoginClickHandler != null)
{
OnLoginClickHandler(sender, e);
}
else
{
// Not handled, so perform the standard redirect
Response.Redirect("/", true);
}
}
You can then just hook up into this within your aspx markup:
<asp:LinkButton runat="server" ID="Foo" OnLoginClick="Foo_LoginClick" />
And your server side event handler on your Page will be as follows:
protected void Foo_LoginClick_Click(object sender, EventArgs e)
{
// This event was fired from the UserControl
}
UPDATE
I think this is how you translate the event subscription check to VB.Net:
If LoginClick IsNot Nothing Then
RaiseEvent LoginClick(sender, e)
End If
I think you should have a look into below code and links.
Parent Page aspx.cs part
public partial class getproductdetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button btnYes = (Button)ucPrompt.FindControl("btnYes");
btnYes.Click += new EventHandler(ucPrompt_btnYes_Click);
}
void ucPrompt_btnYes_Click(object sender, EventArgs e)
{
//Do Work
}
}
Read more details how it works (Reference) :-
Handle events of usercontrol in parent page in asp.net
Calling a method of parent page from user control

Adding multile row in the girdview

Adding the row one by one in the button click..
(i tried but its overwirting the same row)
Here it is in vb for you. Tested it, seems to work fine. Declare dt as shared
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Shared dt As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
dt = New DataTable
dt.Columns.Add("Colum1")
dt.Columns.Add("Colum2")
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
dt.Rows.Add(TextBox1.Text, TextBox2.Text)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
End Class
A gridview should be bound to a datasource. You'll need to add a record to this datasource and then call myGridView.Bind() to bind to it.
Lots of info on MSDN about GridViews:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx
You can do something like this...
static DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add("Colum1");
dt.Columns.Add("Colum2");
}
private void button1_Click(object sender, EventArgs e)
{
dt.Rows.Add(textBox1.Text, textBox2.Text);
dataGridView1.DataSource = dt;
}

Resources