Populate different gridview by clicking different button - asp.net

In my web page header, there are several buttons. What I want is to click each button to display a desired table in a gridview.
click button1
Display table in gridview 1.
click button2 Display table in gridview 2, Gridview 1 disappeared.
The header and buttons are persisting in the page. I don't want to redirect the current page to different pages. I only want to implement the function in one web page.

ASPX
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</div>
C#
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
GridView2.Visible = !GridView1.Visible;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.Visible = false;
GridView2.Visible = !GridView1.Visible;
GridView2.DataBind();
}
You may need to pass in the .DataBind() call depending on how you are binding your data to the gridviews.

On each button click just bind your GridView as needed. For example:
Button1 clicked do:
protected void Button1_OnClick(object sender, EventArgs e)
{
grdYourGrid1.DataSource = yourDataSource;
grdYourGrid1.DataBind();
grdYourGrid1.Visible = true;
grdYourGrid2.DataSource = null;
grdYourGrid2.DataBind();
grdYourGrid2.Visible = false;
}
Then do the opposite for Button2. I suggest rebinding and nulling the non visible grid so the ViewState doesn't get bloated when not required.

You can put this in the Page_Load
button1.Click+=(s,ev)=>
{
gridview1.visible = true;
gridview1.DataBind();
};
button2.Click+=(s,ev)=>
{
gridview2.DataBind();
gridview1.visible = false;
};

C#
buttin1_Onclick(object sender, EventArgs e)
{ gridview1.visible = true; gridview1.DataBind(); gridview2.visible = false;}
buttin2_Onclick(object sender, EventArgs e)
{ gridview2.visible = true; gridview2.DataBind(); gridview1.visible = false;}
or
buttin1_Onclick(object sender, EventArgs e)
{ gridview1.DataBind(); gridview2.Rows.Clear();}
buttin2_Onclick(object sender, EventArgs e)
{ gridview2.DataBind(); gridview1.Rows.Clear();}

Related

Repeater with dynamic UC

I have a formular with dynamic data and UC.
my ascx file looks like:
<asp:Repeater ID="rMyData" runat="server" OnItemDataBound="OnRepeaterItemDataBound">
<ItemTemplate>
<asp:Panel ID="panelAdditional" runat="server"/>
</ItemTemplate>
</asp:Repeater>
and the cs file:
public partial class MyFormular
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PrepareList();
DataBind();
}
}
private void PrepareList()
{
rMyData.DataSource = GetData();
rMyData.DataBind();
}
protected void OnRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem repeaterItem = e.Item;
Panel panel = (Panel)repeaterItem.FindControl("panelAdditional");
if (panel != null)
{
var datarow= (Data)repeaterItem.DataItem;
if (datarow!= null)
{
panel.Controls.Clear();
MyUC ctrl;
ctrl = (MyUC)Page.LoadControl("~/MyUC.ascx");
ctrl.Enabled = true;
ctrl.DataRow = datarow;
panel.Controls.Add(ctrl);
}
}
}
protected void SaveNewsletterSubscription(object sender, EventArgs e)
{
...
}
}
My problem is, it doesn't show my UC.
If I put my PrepareList() and DataBind() outside of the PostBack, it works perfectly fine and shows my UC. But after I press the save Button, it reloads the page and rebind my repeater. So I lost everything in the UC.
Why does it only show my UC if I rebind it in every pageload?
I hope the I posted enough information.

ASP.NET Setting Visible = false doesn't stay

I have a lot of buttons that I want to appear when I tell them to and so far I have been using Visible = true or false depending on when I want them to appear or not. I want the button to disappear after the click event has occurred on it. I have two buttons in the code and when I click on btnOption131 it disappears but than when I click on btnOption132 it disappears and btnOption131 re-appears. Is there anyway I can stop this from happening?
<asp:Button ID="btnOption131" runat="server" Text="Think" OnClick="btnOption131_Click" Visible="False" CssClass="button"/>
<asp:Button ID="btnOption132" runat="server" Text="Go Crazy" OnClick="btnOption132_Click" Visible="False"/>
Code Behind:
protected void btnOption131_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[8].ToString();
btnOption131.EnableViewState = false;
btnOption131.Visible = false;
}
protected void btnOption132_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[9].ToString();
btnOption132.EnableViewState = false;
btnOption132.Visible = false;
}
I also tried:
protected void btnOption131_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[8].ToString();
btnOption131.Visible = false;
}
protected void btnOption132_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[9].ToString();
btnOption132.Visible = false;
}
You have to set the button Visibility to 'true' somewhere. My guess is that it is not wrapped inside an IsPostBack check:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
btnOption131.Visible = true;
}
}
If you do not do this the visibility will always be set to true when the next PostBack (= button click) occurs.

manual gridview paging

i'm am pulling 10 out of 100 records back from the database and placing into gridview (no datasource objects here).
how do i enable paging that comes with the gridview? I know the total records is 100 can I use that somehow to activate the paging?
I know I can do this easily with DataSource objects but was just wondering if I could do it completely manually as far as the GridView is concerned.
Markup
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting">
</asp:GridView>
</div>
</form>
Code-behind
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetCustomers();
GridView1.DataBind();
}
strong text
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback) {BindData();}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.DataSource = GetCustomers();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
void BindData()
{
GridView1.DataSource = GetCustomers();
GridView1.DataBind();
}
Also you need to add this to gridview markup:
OnPageIndexChanging="GridView1_PageIndexChanging"

I want RowCommand executed before RowCreate reloading OR something like this

I have
<asp:GridView>
<asp:TemplateField HeaderText="PsyHealth">
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="PsyHealth" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="-">
<ItemTemplate>
<asp:LinkButton ID="Gen" CommandName="Gen" runat="server" Text="gen" />
</ItemTemplate>
</asp:TemplateField>
and
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dataItem = e.Row.DataItem as ViewModels.UserTestorViewModel;
var psyHealth = e.Row.FindControl("PsyHealth") as PlaceHolder;
if (psyHealth != null)
{
psyHealth.Controls.Add(dataItem.PsyHeath);
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//bla bla bla
}
but when I clicked the Gen LinkButton on the page. The GridView1_RowCreated was triggered first and threw the error Object reference not set to an instance of an object because the e.Row.DataItem was null.
Edit: The Code Behind
protected void Page_Load(object sender, EventArgs e)
{
List<ViewModels.UserTestorViewModel> utViewModelList = new List<ViewModels.UserTestorViewModel> { };
utViewModelList = utRepo.GetUserTestorViewModelListByHrId();
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
if (!IsPostBack)
{
}
}
protected void Page_Init(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
}
Can you store the utViewModelList in Session first time you get it? If so then you can get the UserTestorViewModel instance from saved by the selected row's DataKey value.
When you click any button in the gridview , your page is postbacked and the page load event is called before it goes into the RowCommand event. In the page load event you are binding your gridview again and that's why your RowCreated Event is called.
You have to bind your gridview under if (!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<ViewModels.UserTestorViewModel> utViewModelList = new List<ViewModels.UserTestorViewModel> { };
utViewModelList = utRepo.GetUserTestorViewModelListByHrId();
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
}
}
Edit: Now I got your issue after you posted code..
The problem is here in Page_Init, can you remove the event handler from here and try the following:
protected void Page_Init(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
}
add here
<asp:GridView onrowcreated="GridView1_RowCreated">

How to change the GridView page Index

How to change Gridview pages based on the dropdown list selected index? the dropdown list is not inside the gridview, any help would be appreciated.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageIndex = DropDownList1.SelectedItem.Value;
GridView1.DataSource = datatable;
GridView1.DataBind();
}
What you need to do is set the GridView's PageSize property to the value of your DropDownList's selected item value:
private void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
gridview.PageSize = DropDownListPageSize.SelectedValue;
}

Resources