• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

How do I display a message box in C#

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
Shepwedd
Asked:
Shepwedd
  • 11
  • 9
1 Solution
 
WilliamCommented:
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
 
ShepweddAuthor Commented:
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
 
WilliamCommented:
You could place this in the parent form of the edit button
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ShepweddAuthor Commented:
The parent form of the edit button?
0
 
WilliamCommented:
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
 
ShepweddAuthor Commented:
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
 
WilliamCommented:
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
 
ShepweddAuthor Commented:
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
 
WilliamCommented:
What determines if the item has expired?
0
 
ShepweddAuthor Commented:
Within the drop down lists object data source I have a select query that has a where filter on closingdate > todays date function.
0
 
WilliamCommented:
sorry I am tied up..  I wil try and help more later
0
 
WilliamCommented:
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
 
ShepweddAuthor Commented:
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
 
WilliamCommented:
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
 
ShepweddAuthor Commented:
Sorry, I still get the same error :(

I'm using the code in the PreRender event.
0
 
WilliamCommented:
Can you post the page source and class behind?
0
 
ShepweddAuthor Commented:
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
 
ShepweddAuthor Commented:
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
 
WilliamCommented:
Can you post your client side script and where/how you are using it?
0
 
WilliamCommented:
Thanks for posting your script!
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now