Repeater with dynamic UC - asp.net

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.

Related

DropDownList bug - OnSelectedIndexChanged fires only when DDL is filled in HTML

Step 1. The DropDownList is filled with items on Page_Load.
Step 2. I select an item and it initiates postback. During this postback Page refreshes first, then OnSelectedIndexChanged fires and "ddl1_Select" function runs.
If I statically fill the DropDownList with data in Aspx (HTML) file, it works properly.
But, if I fill it in code-behind, the OnSelectedIndexChanged event never fires and DDL1_Select procedure doesn't start. Page just posts back and skips my procedure. Why doesn't this event fire and how to make it work?
ASPX:
<%# Page Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true"
CodeFile="mypage.aspx.cs" Inherits="mypage" Title="mypage" EnableViewState="False" %>
...
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ddl1_Select" AutoPostBack="True"/>
ASPX.CS:
protected void Page_Load(object sender, EventArgs e)
{
ddl1.Items.Add("1");
ddl1.Items[0].Value = "1";
ddl1.Items.Add("2");
ddl1.Items[1].Value = "2";
ddl1.Items.Add("3");
ddl1.Items[2].Value = "3";
if (Session["NewSelection"] != null) // see note 1
{
string itemValue = Session["NewSelection"].ToString();
ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByValue(itemValue));
Session["NewSelection"] = null; // see note 1
}
}
protected void DDL1_Select(object o, EventArgs e)
{
Session["NewSelection"] = ddl1.SelectedValue;
Page.Response.Redirect("mypage.aspx?test=" + Session["NewSelection"].ToString());
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddl1.Items.Add("1");
ddl1.Items[0].Value = "1";
ddl1.Items.Add("2");
ddl1.Items[1].Value = "2";
ddl1.Items.Add("3");
ddl1.Items[2].Value = "3";
if (Session["NewSelection"] != null) // see note 1
{
string itemValue = Session["NewSelection"].ToString();
ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByValue(itemValue));
Session["NewSelection"] = null; // see note 1
}
}
}

ASP.NET listview double click

Can I use double-click on a asp.net listview? I want to call a function on double-click rather than single i.e ItemCommand.
Is it at all possible?
Ta!
ASPX:
<asp:ListView ID="ListView1" runat="server"
onitemdatabound="ListView1_ItemDataBound" onitemcommand="ListView1_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandName="DoubleClick" runat="server">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:ListView>
CS:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
LinkButton LinkButton1 = (LinkButton)e.Item.FindControl("LinkButton1");
string _jsDouble = ClientScript.GetPostBackClientHyperlink(LinkButton1, "");
LinkButton1.Attributes["ondblclick"] = _jsDouble;
LinkButton1.Attributes["onclick"] = "return false;";
}
}
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "DoubleClick")
{
}
}
If calling function is referring to server side call simple thing would be
private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
{
MessageBox.Show("Double Click Event Called");
}
For more info on same check:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doubleclick.aspx
And if calling function refers to any client side (Javascript/Jquery call) then you can use:
$('#target').dblclick(function() {
alert('Handler for .dblclick() called.');
})
for more info check here

Dropdownlist in gridview not firing selectedindex changed event

I have problem with not firing selected index changed event of dropdownlist in gridview. I gone through the SO Thread . It is not worked wholly for me. I have implementation like below.
.ASPX
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL1_SelectedIndexChanged">
<asp:ListItem Text="Review" Value="Review" Selected="True">Review</asp:ListItem>
<asp:ListItem Text="Level1" Value="lvl1">Send Back to Level1</asp:ListItem>
</asp:DropDownList>
.CS
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
}
protected void grdPoll_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(Page.IsPostBack)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DDL1") as DropDownList;
if(ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
}
}
}
}
When i keep if(!Page.IsPostBack) block only then it works fine. But i want else block also. Whats going wrong with implentation. Can you please suggest the solutions
The problem is block after !Page.IsPostBack block, which is not event else part as you said. You are binding grid again on post back which results in loss of the event being fired. You do not have to bind it again to have the changes in the grid.
Remove this code.
{
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
//DataBindGrid(); //remove DataBindGrid(); from else
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
DataBindGrid();
}
replace event name "Page_Load" with "Page_PreRender"

SelectedIndexChanged not firing in ascx?

I use masterpage.In ascx page my selectedindexchange event not firing.
This is my code:
My ascx :
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="FilterList.ascx.cs"
Inherits="F8.B2B.WEB.UserControls.Common.FilterList.FilterList" %>
<asp:UpdatePanel UpdateMode="Always" runat="server">
<ContentTemplate>
<div id="filterList" runat="server">
</div>
</ContentTemplate>
</asp:UpdatePanel>
My ascx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
createFilterLists();
}
}
private void createFilterLists()
{
ListBox dpList = new ListBox()
{
ID = ControlID
};
dpList.Items.Clear();
if (lst_ListItem != null)
{
foreach (ListItem item_ in lst_ListItem)
{
dpList.Items.Add(item_);
}
dpList.Items[0].Selected = true;
dpList.AutoPostBack = true;
dpList.EnableViewState = true;
dpList.SelectedIndexChanged += new EventHandler(myListBox_SelectedIndexChanged);
filterList.Controls.Add(dpList);
}
}
protected void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
// might be entered on change
}
Because your list box is dynamically generated by code, you need to add it in every page load even if it is a postback.
Create dynamic controls and add event handlers in the OnInit event
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
myDataGrid.SomeEventHandler += new ...
}
Bind data and fill controls in the OnLoad event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillControls();
myDataGrid.DataSource = somedatasource;
myDataGrid.DataBind();
}
}
private void CreateControls()
{
ListBox dpList = new ListBox()
{
ID = ControlID
};
dpList.AutoPostBack = true;
dpList.EnableViewState = true;
dpList.SelectedIndexChanged += new EventHandler(myListBox_SelectedIndexChanged);
filterList.Controls.Add(dpList);
}
private void FillControls()
{
dpList.Items.Clear();
if (lst_ListItem != null && lst_ListItem.Count > 0)
{
foreach (ListItem item_ in lst_ListItem)
{
dpList.Items.Add(item_);
}
dpList.Items[0].Selected = true;
}
}

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">

Resources