Null Reference exception when accessing property in OnClick - c#

Please help me to figure out what is wrong with this code:
I have ASP.NET page with one button visible.
When user clicks it - it instances MyClass (its implementation is in AppCode directory) and turns invisible (button2 becomes visible).
When I click button2 it raises "Object reference not set to an instance of an object" Exception.
What seems to be the problem?
{
public MyClass noviTest;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
noviTest = new MyClass(TextBox1.Text);
Button1.Visible = false;
Button2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text=noviTest.getID; //this is the critical line
}
}

Since on postback the noviTest reference to MyClass is not recreated.
You can add it to the viewstate to keep a reference to it. (Assuming MyClass is serializable).
In Button1_Click:
ViewState("noviTest") = noviTest;
Then in Button2_Click:
var noviTest = ViewState("noviTest");

noviTest is null inside Button2_Click.
Try something like this:
protected void Page_Load(object sender, EventArgs e)
{
noviTest = new MyClass(TextBox1.Text);
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Visible = false;
Button2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = noviTest.getID;
}
This will cause noviTest to be instantiated on each page request, regardless of which button was clicked. This may not be the best solution for your particular application (I am unfamiliar with the rest of the logic in this class) but you get the idea.

Each visit to the code-behind is kind of like running the code from scratch, nothing is set up or preserved for you between visits to page.
So when you hit the second button noviTest is not initialised and therefore when you attempt to call .getID to you get a null reference exception.
To deal with this you need to ensure that noviTest is initialised. If you want to have it persisted between visits to the page you need to use some mechanism to either store or recreate the instance. To store it you would (probably) use session. To recreate you would use either session or viewdata to persist a key value that would then allow you to retrieve the object state from storage of some kind.

Related

ASP.NET ImageButton Click event

I have a dynamic ImageButton and I want it to trigger a method when it's clicked. I have the following code, which unfortunatelly it's not working.
ImageButton imb = new ImageButton();
imb.Attributes.Add("runat", "server");
imb.Click += new ImageClickEventHandler(ImageButton1_Click);
imb.ID = "ID";
imb.ImageUrl = "link to image";
Panel1.Controls.Add(imb);
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
//it should go here
}
you must add your ImageButton to a suitable continer.
e.g:
form1.Controls.Add(Imb);
/// form1 must have runat=server
you can find useful tips at here.
Adding dynamic controls to the page is a tricky matter. Unfortunately, you cannot just declare an object of WebControl derived type and expect it to work.
In ASP.NET WebForms, there is a concept called Page Lifecycle. It is a broad matter, but my point is that there is a particular order you must obey to plug-in the dynamic control into the page. A practical example follows.
Start with dynamic control declaration at the class level.
protected ImageButton imb = null;
Then you initialize it durring the page initialization. One possibility is to handle the PreInit event.
protected void Page_PreInit(object sender, EventArgs e)
{
imb = new ImageButton()
{
ID = "ID",
ImageUrl = "link to image"
};
imb.Click += Imb_Click;
Panel1.Controls.Add(imb);
}
private void Imb_Click(object sender, ImageClickEventArgs e)
{
// Do your stuff
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set initial properties as appropriate. E.g.
imb.AlternateText = "...";
}
}
You're good to go.

ViewState value not being retained during postback

How can I make this piece of code work? I am dealing with a bigger issue but if I can make this work then I will know what to do.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Value"] = "Button clicked";
}
Page_Load event happens before Button1_Click and hence you wont be able to access a value that is not already set.
You will need to use an event that happens after Button1_Click like Page_PreRender as you have used in the answer.
Please go through this link to understand Page Life Cycle, which is invaluable in Asp.Net Webforms development.
I was able to solve my problem by putting my logic in pageLoad method in the page_PreRender method like this:
protected void Page_PreRender(object sender,EventArgs e)
{
if (IsPostBack)
{
Response.Write(ViewState["Value"].ToString());
}
}

Why does my variable lose its value?

From the selectedindexchanged event, my variable has a value, but when it reaches the btn_click() event, the variable no longer has a value. Why is that?
public partial class TestingDatapass
{
private string item = null;
private string itemprice = null;
private int totalprice = 0;
protected void item_selectedindexchanged(object sender, EventArgs e)
{
//Both have a value here
item = item.SelectedValue;
itemprice = item.SelectedValue.Text;
}
protected void btn_click(object sender, EventArgs e)
{
//no value here
totalprice = Convert.ToInt32(itemprice)*Convert.ToInt32(item);
MessageBox.Show(totalprice);
}
}
EDIT
And to answer the ? posed in comments, the order of occurrence is the selectedindexchange THEN the btn_click()
EDIT REGARDING View State
So then would this be a proper way to set up what I am trying to achieve?
public partial class TestingDatapass
{
private int totalprice = 0;
protected void item_selectedindexchanged(object sender, EventArgs e)
{
ViewState["item"] = item.SelectedValue;
ViewState["itemprice"] = item.SelectedValue.Text;
}
protected void btn_click(object sender, EventArgs e)
{
totalprice = Convert.ToInt32(ViewState["item"])*Convert.ToInt32(ViewState["itemprice"]);
}
}
When a page is requested, ASP.NET creates an instance of TestingDatapass class and initialize itemprice,totalprice etc. fields. Now when you change your dropdown from client (which I assume looking at your item_selectedindexchanged method), Postback happens and it assign values you have mentioned in item_selectedindexchanged. Finally it destroys the instance, generates the html and sends it back to browser.
Now, when you press the button in your page then a new instance is created, your fields are re-initialized and you don't see the changed values in btn_click. This is how it works.
Thus if you want to preserve any data across postback, Consider using any State Management technique like ViewState, HiddenField etc.
Also, as a side note, MessageBox.Show is not available in ASP.NET.
Update:
I answered in the context of why it is not retaining the value in button click event, there are many ways to do it. But to answer your question, I don't see any reason to store the values in item_selectedindexchanged event as you are not doing anything there. You can directly access the dropdown selected values in button click handler like this:-
protected void btn_click(object sender, EventArgs e)
{
totalprice = Convert.ToInt32(item.SelectedValue) *
Convert.ToInt32(item.SelectedItem.Text);
}
Also, please note it's item.SelectedItem.Text and not SelectedValue.Text.

C# how to stop a control's event

I have an .aspx page that has a page_load as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.Name != "")
{
....
}
else
{
FormsAuthentication.RedirectToLoginPage();
return;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
}
The issue I am noticing is that when the user clicks the submit button it goes though the Page_Load (goes though the else) and then tries to run though the protected void btnSubmit_Click(object sender, EventArgs e).
Is there a way to make it redirect to the login page and not continue to the next void?
This is NOT an issue. This is the normal ASP.NET Page Life Cycle.
Controls events fire right after the Page_Load event.
Note that the FormsAuthentication.RedirectToLoginPage method does not end the request by calling HttpResponse.End. This means that code that follows the RedirectToLoginPage method call will run.
Assuming you want to stop further processing from within the Page_Load (which I believe is your intention with return):
protected void Page_Load(Object sender, EventArgs e)
{
if (/*I want to kill processing*/)
{
// Method one:
Response.End(); // Though admittedly ugly
// Method two:
this.Context.ApplicationInstance.CompleteRequest();
return; // return as normal (short-circuit)
}
}

Calling page event when ASP.NET user control event is invoked

Suppose there is a user control in a page called Paging.ascx that is embedded in PageWithResults.aspx. This control has the necessary properties to keep track of various details about what page you're on (ie: CurrentPage, TotalRecordsInResults, RecordsPerPage, etc..). It also contains events that fire off when you click on a hyperlink ("next page" or "previous page"). Example below. I need to tell PageWithResults.aspx that one of these LinkButton web controls was clicked. I think I need to assign a delegate in the page, so that when this user control event is called (hyperlink is clicked), it also calls some other method/event in the page class. That way I can quickly check what the new value of CurrentPage is (based on what was called in the event below) and get a new result set for the new page (based on the CurrentPage property). But I'm not sure of the best approach. I'm thinking this will require a delegate, but I'm not sure how to wire it up. If you need more details, please ask.
protected void btnNext_Click(object sender, EventArgs e)
{
this.CurrentPage = this.CurrentPage + 1;
if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
}
I'm thinking I have to put my delegate here somewhere. ??
protected void btnNext_Click(object sender, EventArgs e)
{
this.CurrentPage = this.CurrentPage + 1;
if (OnPageChanged != null) OnPageChanged(this.CurrentPage);
//delegate to call object.method or something
}
Using an event would work fine.
You would create the event within your UserControl like so:
public event EventHandler ButtonClicked;
Then invoke the event when required:
protected void Button1_Click(object sender, EventArgs e)
{
if (ButtonClicked != null)
ButtonClicked(this, new EventArgs());
}
In your page you would need to assign an event handler:
protected void Page_Load(object sender, EventArgs e)
{
UserControl1.ButtonClicked += new EventHandler(UserControl1_ButtonClicked);
}
void UserControl1_ButtonClicked(object sender, EventArgs e)
{
}
As well as using the above approach you can also cast the Page reference in the UserControl and call a public method directly:
MyPage page = (MyPage)Page;
page.AMethod();
Hope this helps.

Categories