Solved

How do I display a message box in C#

Posted on 2009-04-02
20
503 Views
Last Modified: 2013-12-17
I have written a C# Web Site within visual studio 2008. I am using an asp.net 3.5 listview control. Within the edit template I have a drop down list bound to an object data source control which has a select query restricting items to those only created after the current date. Due to this when users try to edit an item that was created before the current date the following error is thrown:

Sys.WebForms.PageRequestManagerServerErrorException: 'DropDownControlID' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value. Code:0

How can I catch this error and display a message box to the user stating that "edits cannot be made as this item has expired"?
0
Comment
Question by:Shepwedd
  • 11
  • 9
20 Comments
 
LVL 11

Expert Comment

by:William
ID: 24050030
You are not looking for a messagebox: you need java alert to pop the display(pseudo msgbox).
I use something like this in one of my pages for a confirm delete... You can do similar with the alert
 
I place this in the OnPrerender

  string delete = "return confirm('Are you sure you want to delete the Customer Problem Record id: " + myvariable + " ?');";

            this.BtnDeleteCurrentRecord.Attributes.Add( "onclick" , delete );

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24050376
Thanks.

So I declare OnPreRender within my listview attributes, i.e. OnPreRender="listview_PreRender".

I then refer to this in my code behind along with your suggested code as attached. Do I have the right EventArgs declared? The "EditButton" reference is the ID of my Edit linkbutton found in my listviews ItemTemplate but unfortunately the ID can't seem to be found? Do I need a findcontrol or something?
protected void listview_PreRender(object sender, ListViewItemEventArgs e)

    {

        string editExpired = "return confirm('Edits cannot be made to this item as it has expired');";

        this.EditButton.Attributes.Add("onclick", editExpired);

    }

Open in new window

0
 
LVL 11

Expert Comment

by:William
ID: 24050580
You could place this in the parent form of the edit button
0
 

Author Comment

by:Shepwedd
ID: 24050617
The parent form of the edit button?
0
 
LVL 11

Expert Comment

by:William
ID: 24050780
yeah - you should be able to use the "this." and see the edit button (if your button is called that.) if not it goes to the attributes of the control you want and the event you want to attatch the attribute to.
0
 

Author Comment

by:Shepwedd
ID: 24050960
The ID of my edit button is "ITEditButton" but this is not an option under the intelisense when I start typing "this." ...My listview ID is available and my ItemTemplate (this.listview.ItemTemplate) but again my edit buttons ID is not available so I cannot find the control?
0
 
LVL 11

Expert Comment

by:William
ID: 24051013
is the edit button part of the list view? if so then I believe you are right and you will have to 'find the control'
0
 

Author Comment

by:Shepwedd
ID: 24051225
Yes, the edit button is within the ItemTemplate of the listview. I tried the attached code but I get the error: Object reference not set to an instance of an object? lvApplicants id the ID of my listview.
protected void lvApplicants_PreRender(object sender, EventArgs e)

    {

        //LinkButton btn_Edit = (LinkButton)lvApplicants.FindControl("ITEditButton");
 

        LinkButton btn_Edit = this.lvApplicants.FindControl("ITEditButton") as LinkButton;
 

        string editExpired = "return confirm('Edits cannot be made to this item as it has expired');";

        btn_Edit.Attributes.Add("OnClick", editExpired);

    }

Open in new window

0
 
LVL 11

Expert Comment

by:William
ID: 24053422
What determines if the item has expired?
0
 

Author Comment

by:Shepwedd
ID: 24059312
Within the drop down lists object data source I have a select query that has a where filter on closingdate > todays date function.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:William
ID: 24065045
sorry I am tied up..  I wil try and help more later
0
 
LVL 11

Expert Comment

by:William
ID: 24068396
Can you run the page (without the code) and then right click the page, view source and try to find the name of the edit button "ITEditButton". It may have an ID of something more like a guid. That will be the way to make sure you have found the right control. (Note: you may not see the control as it may be in the inner html....
0
 

Author Comment

by:Shepwedd
ID: 24077099
Within the view source my edit link button is coded as attached...

I'm not too sure what to make of that?

Thanks.
<a id="ctl00_ctl00_MainContent_MainContent_lvApplicants_ctrl1_ITEditButton" href="javascript:__doPostBack('ctl00$ctl00$MainContent$MainContent$lvApplicants$ctrl1$ITEditButton','')">Edit</a>

Open in new window

0
 
LVL 11

Expert Comment

by:William
ID: 24078601
this is the id of the button "ctl00_ctl00_MainContent_MainContent_lvApplicants_ctrl1_ITEditButton" that is what you will have to search for to attatch the script.
 
So.... the below hopefully will work.

LinkButton btn_Edit = this.lvApplicants.FindControl("ctl00_ctl00_MainContent_MainContent_lvApplicants_ctrl1_ITEditButton") as LinkButton;

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24078711
Sorry, I still get the same error :(

I'm using the code in the PreRender event.
0
 
LVL 11

Expert Comment

by:William
ID: 24079152
Can you post the page source and class behind?
0
 

Author Comment

by:Shepwedd
ID: 24087658
I can't post as my listview is bound to a linq datasource and I also have drop downs bound to data sources within the insert and edit templates thus you won't be able to build the web site for your investigations. If you have a data source just simply drop the listview onto a page and bind it to your data source so that you can create the listview templates. You could then attempt to find a control within the listviews ItemTemplate. Let me know if you manage it.
0
 

Accepted Solution

by:
Shepwedd earned 0 total points
ID: 24089199
I solved this by using client side scripting.

For those who are interested in my solution to my findcontrol issue this was resolved by using the below code in my listviews databound event:

LinkButton btn_Edit = (LinkButton)lvApplicants.Items[0].FindControl("ITEditButton");

In the above the "Items[0]" refers to the listview row. I had a itemtemplate and an alternatingitemtemplate so I had to reference both row 0 and 1 in my code behind to find these controls.
0
 
LVL 11

Expert Comment

by:William
ID: 24089253
Can you post your client side script and where/how you are using it?
0
 
LVL 11

Expert Comment

by:William
ID: 24125900
Thanks for posting your script!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now