button events not fire in asp.net - asp.net

The if (!Page.IsPostBack) is always false went the page loads after i click a linkbutton and it never goes into the linkbutton event. Desperately need help! Googled as much as I can. I am kinda new to asp
This is the code that i have in server:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaultView();
}
}
private void SetDefaultView()
{
MultiView1.ActiveViewIndex = 0;
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
}
// and below..
<td>
<asp:LinkButton ID="LinkButton1" runat="server"
onclick="LinkButton1_Click">Tab1</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Tab2</asp:LinkButton>
</td>

Is the code part of a dynamically loaded UserControl? If so, in what stage of the asp.net lifecycle are you adding the UserControl?
If that is not the case, how do you access the Page?

Related

asp.net repeater in update panel databind outside page_load

I have repeater in updatePanel asp.net. It works fine when I run databind() in Page_load method. But when databind() is called by some event outside Page_load, repeater cleared. Databind() does not work in this case! What can be reason for this?
this is code ...
this works:
protected void Page_Load(object sender, EventArgs e)
{
........
populateCalendar(Int32.Parse(DDL_YearsList.SelectedValue), Int32.Parse(DDL_MonthsList.SelectedValue), Int32.Parse(DDL_EmployeesList.SelectedValue));
}
private void populateCalendar(int year, int month, int idEmploee)
{
.......
monthShower.DataSource = listWeeks;
monthShower.DataBind();
}
protected void DDL_EmployeesList_SelectedIndexChanged(object sender, EventArgs e)
{
//populateCalendar(Int32.Parse(DDL_YearsList.SelectedValue), Int32.Parse(DDL_MonthsList.SelectedValue), Int32.Parse(DDL_EmployeesList.SelectedValue));
}
this does not work, when DDL_EmployeesList_SelectedIndexChanged fired:
protected void Page_Load(object sender, EventArgs e)
{
........
// populateCalendar(Int32.Parse(DDL_YearsList.SelectedValue), Int32.Parse(DDL_MonthsList.SelectedValue), Int32.Parse(DDL_EmployeesList.SelectedValue));
}
private void populateCalendar(int year, int month, int idEmploee)
{
.......
monthShower.DataSource = listWeeks;
monthShower.DataBind();
}
protected void DDL_EmployeesList_SelectedIndexChanged(object sender, EventArgs e)
{
populateCalendar(Int32.Parse(DDL_YearsList.SelectedValue), Int32.Parse(DDL_MonthsList.SelectedValue), Int32.Parse(DDL_EmployeesList.SelectedValue));
}
<asp:Repeater id ="monthShower" runat ="server"> <ItemTemplate> <custom:DayID="Day1" runat ="server" TblDay =<%# DataBinder.Eval(Container.DataItem, "Monday") %>></custom:Day> </ItemTemplate> </asp:Repeater>
So i did a small test and posted the code
Html:
<div id="test">
<asp:DropDownList runat="server" ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<table>
<asp:Repeater runat="server" ID="repTeste" OnItemDataBound="repTeste_ItemDataBound">
<ItemTemplate>
<tr>
<td runat="server" id="tdTeste">
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
C# - I used pop() in my pageLoad just to populate the ddl:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
populateCalendar(DropDownList1.SelectedValue, 2, 3);
}
private void populateCalendar(String year, int month, int idEmploee)
{
List<String> lis = new List<String>();
lis.Add(year);
repTeste.DataSource = lis;
repTeste.DataBind();
}
private void pop()
{
ListItem li = new ListItem("1", "1");
li.Attributes.Add("title", "1");
DropDownList1.Items.Add(li);
li = new ListItem("2", "2");
li.Attributes.Add("t2itle", "2");
DropDownList1.Items.Add(li);
li = new ListItem("3", "3");
li.Attributes.Add("ti3tle", "3");
DropDownList1.Items.Add(li);
}
protected void repTeste_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
String inte = (String)e.Item.DataItem;
HtmlTableCell td = (HtmlTableCell)e.Item.FindControl("tdTeste");
td.InnerText = inte;
}
it updates correctly.
Since you didn't post your html and the fact that someone deleted my post, i posted my code here for you, you can use it to compare it or you can post your Html and ill give it a look.

redirection between two pages Asp.net

I have a asp.net form with several text boxes and a link-button which is redirected to the other page. here is my form:
<table>
<tr>
<td >
<asp:TextBox ID="TextBox_Name" runat="server" Width="100%"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox_FatherName" runat="server" Width="100%"></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="LinkButton_BirthPlace" runat="server" OnClick="LinkButton_BirthPlace_Click" >Search</asp:LinkButton> </td>
</tr>
and it is my method for link button :
protected void LinkButton_BirthPlace_Click(object sender, EventArgs e)
{
Response.Redirect("~/Profile/CitySearch.aspx");
}
my problem is: When I enter value in my text boxes and then click on link button , redirected to CitySearch.aspx page and when I come back to base page my values in text boxes were deleted.I need those values that I enterned.
What should I do?
You need to repopulate your textboxes with your information from the Session. Put a method similar to this on your "base page"
Psuedocode:
protected void Page_Load(object sender, EventArgs e)
{
string Birthplace = Session["Birthplace"].ToString();
MyBirthplaceTextBox.Text = Birthplace;
}
You'll need multiple Session values to store multiple pieces of information. You'll also want to code in some checks to make sure your session variables aren't null, and take action accordingly.
Try like This on your First Page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["search"]!=null)
{
TextBox_Name.Text = Session["search"].ToString();
}
}
}
Try like this,
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack==false)
{
if (Session["search"]!=null)
{
TextBox_Name.Text = Session["search"].ToString();
}
}
}
protected void LinkButton_BirthPlace_Click(object sender, EventArgs e)
{
Session["search"] = TextBox_Name.Text;
Response.Redirect("~/Profile/CitySearch.aspx");
}

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

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

Setting viewstate on postback

I am trying to set a ViewState-variable when a button is pressed, but it only works the second time I click the button. Here is the code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
lblInfo.InnerText = String.Format("Hello {0} at {1}!", YourName, DateTime.Now.ToLongTimeString());
}
}
private string YourName
{
get { return (string)ViewState["YourName"]; }
set { ViewState["YourName"] = value; }
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
YourName = txtName.Text;
}
Is there something I am missing? Here is the form-part of the design-file, very basic just as a POC:
<form id="form1" runat="server">
<div>
Enter your name: <asp:TextBox runat="server" ID="txtName"></asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" Text="OK" onclick="btnSubmit_Click" />
<hr />
<label id="lblInfo" runat="server"></label>
</div>
</form>
PS: The sample is very simplified, "use txtName.Text instead of ViewState" is not the correct answer, I need the info to be in ViewState.
Page_Load fires before btnSubmit_Click.
If you want to do something after your postback events have fired use Page_PreRender.
//this will work because YourName has now been set by the click event
protected void Page_PreRender(object sender, EventArgs e)
{
if (Page.IsPostBack)
lblInfo.InnerText = String.Format("Hello {0} at {1}!", YourName, DateTime.Now.ToLongTimeString());
}
The basic order goes:
Page init fires (init cannot access ViewState)
ViewState is read
Page load fires
Any events fire
PreRender fires
Page renders

Resources