Solved

How do I display a message box in C#

Posted on 2009-04-02
20
505 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

17 Experts available now in Live!

Get 1:1 Help Now