Hiding LinkButton - asp.net

In VB.NET, I have a LinkButton that, when clicked, creates a report. We want to disable this functionality based on whom is logged into the system. I have a session variable to indicate whether this should be visible or not but when I set idLinkButton.visible = False, it has no effect. Any ideas on what I need to do to be able to turn this on and off?
'code-behind
' in page_load
If Not Convert.ToBoolean(HttpContext.Current.Session("HideReports")) Then
PDFbutton.Visible = True
Else If Convert.ToBoolean(HttpContext.Current.Session("HideReports")) And HttpContext.Current.Session("LoadFromConsole") Then
PDFbutton.Visible = True
end if

First, have you tried setting .Visible = false outside the if/then logic?
If that works, then you want to troubleshoot the logic. Add my first line to what you show above, then change your first line to my second line.
Dim hideRpt as Boolean = Convert.ToBoolean(HttpContext.Current.Session("HideReports"))
If Not hideRpt Then
Now put a breakpoint on the "If Not..." line.
If you aren't getting what you expect, add another line to get a string for the session value, to make sure it really should convert to a bool.
Basic troubleshooting, man, break it down into smaller parts. Sometimes I'll start a fresh windows app just to have a blank slate on which to work out details.

Related

Controls and null reference exception

I'm aware that the NullReferenceException is pretty much the equivalent to the check engine light on a car but with this particular case I can't seem to pinpoint why its not working properly, I've never really messed with controls so I'm a bit unfamiliar in with the technicalities of it. What I have works, but I keep getting the exception when I run a trycatch around it. Here is what I have.
Dim TypeControl As Control
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
DBTable = MaterialStuff.GetMaterial(ID)
Using DBTable
If DBTable.Rows.Count > 0 Then
Try
CType(TypeControl, DropDownList).SelectedItem.Text = (DBTable.Rows(0).Item("MaterialTypeDescription").ToString)
Catch ex As NullReferenceException
trace.runAllErrorLogging(ex.ToString)
End Try
End If
A NullReferenceException doesn't have anything to do with "controls" specifically. It's just an indication that your code assumes an object exists when at runtime it doesn't exist. For example, if you do this:
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
CType(TypeControl, DropDownList).SelectedItem.Text = ...
Then your code assumes that TypeControl has a value on the second line. If it doesn't, trying to use .SelectedItem will fail because TypeControl is null. So you're assuming that .FindControl() actually found something. It doesn't make that guarantee implicitly.
Instead of making this assumption, you should verify:
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
If TypeControl Is Not Nothing Then
CType(TypeControl, DropDownList).SelectedItem.Text = ...
End If
That way the code only executes if there's a value that it can use. You can add an Else to handle the condition where no value is found. (Display an error? Log the error? Silently continue? It's up to you how the condition should be handled.)
There are two possible problems here:
1 - Does FindControl actually find the control you seek? Add a check in to make sure you are actually finding it:
Dim TypeControl As Control
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
If TypeControl Is Nothing Then Debug.Writeline("Could not find control")
2 - The SelectedItem of the control could also be Nothing so you may need to add a check here:
Dim ddl = CType(TypeControl, DropDownList)
If ddl.SelectedItem Is Nothing Then Debug.Writeline("Could not find selectedItem")

How to refresh after changing ValueNumericOptions.Format?

I'm using the DevExpress xtraChart to display some data. In the CustomDrawSeries event, I'm checking the series name and changing SeriesTemplate.Label.PointOptions.ValueNumericOptions.Format. It works... partially.
The idea is to change ValueNumericOptions.Format from NumericFormat.FixedPoint to NumericFormat.Percent and vice versa based on the name. The problem is the change is not displayed immediately. In order to see the change, the user must select another cell and then the change is visible immediately.
How can I force a refresh to the series and see the changes immediately without needing to select another cell?
Update -
After the change is made via code, I inspected Format and it is being set correctly. This confirms, in my mind anyway, this is a refresh issue.
I do not know what the real cause of the problem, but your approach seems to be not quite optimal. You modify the global settings (template) while your goal is just to change settings for a certain series.
What you want to achieve can be done during the ChartControl initialization, without having to handle events. The following code can be used to apply a percent format to a certain series:
DevExpress.XtraChartsPointOptions pointOptions = new DevExpress.XtraChartsPointOptions();
pointOptions.ValueNumericOptions.Format = DevExpress.XtraCharts.NumericFormat.Percent;
DevExpress.XtraCharts.SideBySideBarSeriesLabel label = new DeveExpress.XtraCharts.SideBySideBarSeriesLabel();
label.PointOptions = pointOptions;
DevExpressXtraChartsSeries series = xtraChart1.Series["Series Name"];
series.Label = label;
series.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;
By the sounds of it you are registering to the Refresh event of the view controller which will change the format of the chart on refresh, assign your code also in the OnViewControlsCreated event to initialize your code upon first creating the view.

How to detect if an ASP ComboBox is blank.empty

I have an ASP ComboBox that is populated on page load with a list of information and a selected index is set. I want the user to be able to type a new value into the box and run a routine that saves that value.
So far, I've managed to set it so when you run the save routine it can detect the text of the box using;
cboFreeBody.SelectedItem.Text
If the box loads with 'arm' and I change it to 'leg', when I run my routine it grabs the new value and processes it, however if I delete 'arm' and leave it blank when I run the routine the above code returns 'arm', where I'd expect it to return ''.
I'm sorry if I haven't explained myself fully here, I'm rather new to ASP and VB.NET so I'm probably just splurting useless information here, but any help would be greatly appreciated.
I ma not sure but thats might help you..
If com_box.SelectedItem = vbEmpty Then
MsgBox("its empty")
End If
I use the isnullorwhitespace function usually.
if string.isnullorwhitespace(cboFreeBody.SelectedItem.Text) then
else
end if

Session variable not taking effect in asp.net

I have a session variable in my asp.net application. The session variable holds a value from the database, that reflects a customized HTML color value.
In my application, I have an asp button with server side code
btnContinue.BackColor = System.Drawing.Color.FromName(Session("ContinueColor"))
Issue: However, when I run the application, the color value is not being reflected in the button.
I did double check, and the session variable does hold the correct value.
There are other objects, that use session variables to display colors, and they are working fine.
How can I resolve this issue?
Update: When I force a color "btnContinue.BackColor = Drawing.Color.Blue", that works perfectly fine.
If it's a hex code, you might want to use ColorTranslator instead:
btnContinue.BackColor = System.Drawing.ColorTranslator.FromHtml(Session("ContinueColor").ToString());
Looking at the color information you posted in your comment, I think you just need to cast the session object as type Color:
btnContinue.BackColor = DirectCast(Session("ContinueColor"), System.Drawing.Color)
EDIT
I found the solution:
btnContinue.BackColor = System.Drawing.Color.FromName("{Name=48E8DD, ARGB=(0, 0, 0, 0)}")
In your case, it would be:
btnContinue.BackColor = System.Drawing.Color.FromName(Session("ContinueColor").ToString())
Which part of the page life cycle are you setting the color? Maybe the session data has not been read yet? Session should be ready after PageLoad.
I recommend setting a break point immediately after this line of code to make sure nothing else is overriding it - theme.
btnContinue.BackColor = System.Drawing.Color.FromName(Session("ContinueColor"))

how to use QTable::editCell()

I don't understand how QTable::editCell() should be used. I am trying to do some error checking based on entries made by user in a QTable as mentioned in my another question.
I would like to give user an opportunity to re-edit the cell which has error. For example, if name column entry has some special characters such as '(', the user should be prompted for the error and the control should go back to same cell in edit mode. I tried using QTable::editCell() in my code as shown below.
Table->editCell(row, 0, TRUE);
name = Table->text(row, 0);
However, this doesn't work as expected. The control doesn't stay in the cell at all and obviously the name is not correctly collected. So, my question is how to ensure from within code that a cell of QTable can be edited so that the edited contents can be accessed immediately in next statement (as shown in above code).
Note: I am working with qt 3.3.8 only.
I don't think you can do that. You'll have to go back to the event loop, and wait for one of the signals (like valueChanged(row,col)) to be fired to re-validate the data.
A blocking wait on a GUI object is often not a good approach (except for modal dialogs).
I know I'm a little late here but you should use the following connect statement with your own custom function to do your specific needs such as below. You can also use this method to disable users from entering in special characters within you custom function. That way they wont ever have to correct undesirable characters.
connect(ui->tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(customFunction(int,int)));
void updateTable
{
//remove invalid characters
}

Resources