Need to hide rows of a table depending on the permission - c#

I have a table that is being use to hold link and image buttons that link the user to other pages on a web site. I want to hide some of these rows depending on the permission the use has. Right now I have:
// Disable buttons if user does not have admin security level
if (Session["SecurityLevel"] != "A")
{
linkbtnNewEmployee.Visible = false;
imgbtnNewEmployee.Visible = false;
linkbtnViewUserActivity.Visible = false;
imgbtnViewUserActivity.Visible = false;
linkbtnEditEmployees.Visible = false;
imgbtnEditEmployees.Visible = false;
linkbtnManageUsers.Visible = false;
imgbtnManageUsers.Visible = false;
}
which will hide the links and buttons, but the table rows still exist. So I have a row or 2 of space between links. I have tried naming rows and using the "rowToHide.style.display = 'none';" command which does not work because it will not recognize the row. The row id shows up in the source code fine, and I use the same ID in the command. Any suggestions? Thanks for your help!

In markup add an Id for the <tr> and runat="server" tag , like this:
<tr id="rowToHide" runat="server>
<!-- Contents here -->
</tr>
And in the code set the visible property to false, like this:
// Disable buttons if user does not have admin security level
if (Session["SecurityLevel"] != "A")
{
rowToHide.Visible = false;
linkbtnNewEmployee.Visible = false;
imgbtnNewEmployee.Visible = false;
linkbtnViewUserActivity.Visible = false;
imgbtnViewUserActivity.Visible = false;
linkbtnEditEmployees.Visible = false;
imgbtnEditEmployees.Visible = false;
linkbtnManageUsers.Visible = false;
imgbtnManageUsers.Visible = false;
}

Related

How to retrieve or enable Radio Button based on database value?

When I use textbox they successfully working like this:
padd.txtCustomerName.Text = this.dtvContacts.CurrentRow.Cells[20].Value.ToString();
but how to apply in Radio Button.
padd.rbtLOI.Text = this.dtvContacts.CurrentRow.Cells[5].Value.ToString();
Try like this
if (this.dtvContacts.CurrentRow.Cells[5].Value.ToString() == "LOI")
{
padd.rbtLOI.Checked = true;
}
else
{
padd.rbtConfirmPO.Checked = true;
}

Invoke a Jquery function from a webbrowser

I am trying to get a data from a password protected webpage using webbrowser , which uses a div container with 3 JQuery data fields. The 2nd field depends on the 1st one, and the 3rd one of the 2nd one.I need to set those 3 comboboxes and hit submit in order to get the data I want to scrape. The problem is that once the 1st combobox changes, it invokes a function which populates the 2nd and 3rd boxes. I can't figure out how to invoke this function...
This is how the first combobox looks like:
Select s[DCOM1] Selection1
I managed to set the 1st combobox, however the big problem that I have is to invoke viewStep.onSChanged() , which supposed to populate the 2nd box, I just can't make it work.
The onSChanged function is located in a Jquery js. file
self.onsChanged = function(selectadj) {
self.viewBasicsUpdated = true;
log.debug('onsChanged');
if (self.s) {
self.adjEnabled = false;
self.adj = null;
self.auctionType = 'S';
controller.ds.getadjList(self.s.id, function(response) {
controller.applyUpdates(function() {
self.adjList = response.data;
// Add "Add adj" option (as first in array).
self.adjList.unshift({
id : ADD_adj_ID,
fullName : controller.locale.getMessage('app.label.add.adj')
});
self.adjEnabled = true;
self.standardEnabled = true;
self.vixEnabled = (StringUtils.trimWS(self.s.vixFlag) === 'Y') ? true : false;
self.offsiteEnabled = (StringUtils.trimWS(self.s.offsiteFlag) === 'Y') ? true : false;
self.titOnlyEnabled = (StringUtils.trimWS(self.s.titOnlyFlag) === 'Y') ? true : false;
self.salType = self.s.salType;
self.titProcurementFlag = (StringUtils.trimWS(self.s.titProcurmentFlag) === 'Y') ? true : false;
//Selects the adj when a new adj is added.
if(selectadj){
angular.forEach(self.adjList, function(object, index) {
log.debug("object.id++"+object.id);
if(object.id===self.newadjId){
self.adj=object;
log.debug("self.newadjId++"+object.id);
}
});
}
});
});
}else{
controller.applyUpdates(function() {
self.adj = null;
self.adjEnabled = false;
self.auctionType = 'S';
self.standardEnabled = false;
self.vixEnabled = false;
self.offsiteEnabled = false;
self.titOnlyEnabled = false;
self.salType = null;
self.titProcurementFlag = false;
});`
}
};
I have tried to invoke the function in many different options including invokescript, posting an identical post request as the one I see with Fiddler (I guess it fails since the cookie is different) but everything fails. Is there a way to execute this function?
When setting values on input controls with Javascript the DOM events might not fire, depending on how the values are set.
Instead of trying to execute the very same function that the webpage would normally trigger, in this case self.onsChanged, fire the event on the control that you updated.
$("#selectBox1").val("Value A");
$("#selectBox1").change();
https://api.jquery.com/change/

Clicking on List Item on Dropdown

I'm having a problem in interacting with a custom dropdown control. It works fine the 1st 6 times, but after that, since the screen is resized, it could no longer locate and click the option in the dropdown control, returning an exception - can't click on hidden control. I tried putting in a itemField.DrawHighlight(); on the control I'm looking for, and it finds it, however it can't click on it. I also tried a to scroll down, but it seems to be not working.
bool addItemCheck = false;
int scrollCheck = 0;
while (Check == false)
{
var addItem= new HtmlButton(window);
addItem.SearchProperties.Add(HtmlButton.PropertyNames.Id, "add-new-item");
Mouse.Click(addItem);
scrollCheck = scrollCheck + 1;
if (scrollCheck > 6)
{
Mouse.MoveScrollWheel(window, -100);
}
var itemDropDown = new HtmlSpan(window);
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.Class, "item-dropdown");
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.InnerText, "Select an Item");
Mouse.Click(itemDropDown );
addItemCheck = itemDropDown.Exists;
}
bool itemBoxCheck = false;
HtmlCustom itemBox = null;
while (itemBoxCheck == false)
{
itemBox = new HtmlCustom(window);
itemBox.SearchProperties.Add(HtmlCustom.PropertyNames.Id, "item-listbox");
var itemField = new HtmlCustom(itemBox);
itemField .SearchProperties.Add(HtmlCustom.PropertyNames.InnerText, item);
Mouse.Click(itemField);
itemBoxCheck = itemBox.Exists;
}
I would really appreciate any help. Thank you.
Try calling the method InsureClickable() on the control before attempting to click on it.
for example:
itemDropDown.EnsureClickable();
Mouse.Click(itemDropDown);
Edit:
if this doesn't work you'll have to scroll down to the item.
try using:
Mouse.MoveScrollWheel()
if that doesn't work also you'll have to map the scroll control and click on it.

Hide some datagridview checkbox cell

I have a datagridview showing installments of a loan. I created a datagridviewcheckbox column so then I can select all the installments i want to pay for.
This is a screen of the datagrid:
My issue is that I need to disable the checkboxes of the paid intallments. In this case, when "Restante" (what´s left to pay) is = 0.
I read some posts where they used the paint event to not show the checkbox cell, but i didnt like that solution. I thought of hiding the checkbox cell, but i don´t know if it is possible to do that.
Thats what i tried:
foreach (DataGridViewRow row in dgv_Cuotas.Rows)
{
if (Convert.ToDecimal(dgv_Cuotas.Rows[row.Index].Cells[17].Value) == 0)
{
dgv_Cuotas.Rows[row.Index].Cells[16].Visible = false;
}
}
Obviously this does not works, I get a compiler error message saying that the property is read only.
Does somebody knows how to set the checkbox cell to invisible?
Just in case, I attach the DataGridViewCheckboxColumn creation code:
DataGridViewCheckBoxColumn chbox = new DataGridViewCheckBoxColumn();
{
chbox.CellTemplate = new DataGridViewCheckBoxCell();
chbox.HeaderText = "";
chbox.Name = "Seleccionar";
chbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
chbox.FlatStyle = FlatStyle.Standard;
}
dgv_Cuotas.Columns.Insert(16, chbox);
dgv_Cuotas.Columns[16].DisplayIndex = 0;
EDIT:
Some considerations:
I use the cell content click event to handle the checkboxes, so readonly wont work. What I want is to hide the checkbox:
private void dgv_Cuotas_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar")
{
DataGridViewRow row = dgv_Cuotas.Rows[e.RowIndex];
DataGridViewCheckBoxCell cellSeleccion = row.Cells["Seleccionar"] as DataGridViewCheckBoxCell;
int n_cuota = Convert.ToInt32(dgv_Cuotas[2, dgv_Cuotas.CurrentRow.Index].Value);
Cuota cuota_seleccionada = new Cuota();
cuota_seleccionada = Lista_cuotas.Where(x => x.num_cuota == n_cuota).First();
if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == true)
{
cellSeleccion.Value = false;
Actualizar_cuotas_seleccionadas(false, cuota_seleccionada);
}
else
{
if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == false)
{
cellSeleccion.Value = true;
Actualizar_cuotas_seleccionadas(true, cuota_seleccionada);
}
}
}
In the other hand, I´m already using the Onpaint event. Its inherited, thats why I´m trying to avoid using it.
Assign a value to the checkbox cell. Then Convert it to a TextBox with a new value.
Worked for me.
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
Yes, you can do this by Converting the DataGridViewCheckBoxCell to DataGridViewTextBoxCell
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue.ToString().Length == 0) // if (string.IsNullOrWhiteSpace(dataGridView1.Rows[row.Index].Cells[4].EditedFormattedValue.ToString()))
break;
if (Convert.ToDecimal(dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue) == 0)
{
dataGridView1.Rows[row.Index].Cells[16].Value = null;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
}
else
{
//dgv_Cuotas.Rows[row.Index].Cells[16] = new DataGridViewCheckBoxCell();
}
}
Use the cell's ReadOnly attribute to disable any modification.
If you want to turn it to hidden, you need to override the painting code for the cells.
Try to hide it and the value remains, which should prevent runtime errors.
dataGridView1.Rows[row.Index].Cells[16].Style.Padding =
new Padding(dataGridView1.Rows[row.Index].Cells[16].OwningColumn.Width, 0, 0, 0);
I took spajce's answer and tweaked it a bit to make it work for me.
for (var i = 0; i < datagridview1.Count; i++)
{
if ((bool)datagridview1[0, i])
{
datagridview1[0, i] = new DataGridViewTextBoxCell
{
Style = { ForeColor = Color.Transparent,
SelectionForeColor = Color.Transparent }
};
}
}
We're basically iterating through the rows and looking for a 'true' checked box. If it's checked then we're converting the box to a text box and setting its text color to Transparent so the cell looks empty. I hope this helps everyone who had this problem, I spent hours trying to find a workable answer.
There are several ways to accomplish what you want.
For example, you can use the Readonly property of the cell to avoid the user to change the values and change the appeareance of the control to look grayed:
C# DataGridViewCheckBoxColumn Hide/Gray-Out
A simple and effective alternative is to use the chkbox ThreeState property. In code below, if my object does not have an email address, then I don't want to allow the user to tick the checkbox for sending email. To do this, the check box value is set to Unknown and read-only, which means it is displayed as "unselectable" and user cannot modify.
chk = DirectCast(row.Cells(m_idxEmailChecked), DataGridViewCheckBoxCell)
If String.IsNullOrEmpty(contact.EmailAddress) Then
chk.Value = enumTristate.Unknown
chk.ReadOnly = True
Else
chk.ThreeState = False
End If
Note this also requires several associated ThreeState properties to be set on the checkbox.
.ValueType = GetType(enumTristate)
.TrueValue = enumTristate.True
.FalseValue = enumTristate.False
.IndeterminateValue = enumTristate.Unknown
.ThreeState = True
Good luck.
I tried Charlie's way:
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
I got fewer errors, but still kept getting them. So I tried to instantiate the new DataGridViewTextBoxCell separately, and that did the trick for me (I didn't need to set the checkbox cell value either):
DataGridViewTextBoxCell c = new DataGridViewTextBoxCell();
c.Value = "";
dataGridView1.Rows[row.Index].Cells[16] = c;
Hope that helps someone!

Creating Settings form using TreeView in C#

I am developing the settings form for the software developed in C#. I was looking at how different software have implemented their settings form.
In most of the cases that I came across, they seem to be using Treeview on the left pane of the form and configuration settings on the right pane.
Ref URL : http://2.bp.blogspot.com/-nMfQoLurxwM/UDXfiZKd4DI/AAAAAAAAAME/IRf6kmxay4w/s1600/bild1.jpg
I was wondering, how the different controls are designed/displayed on the right pane. Do they hide all the controls depending which node is selected in the TreeView something like this :
if (treeView1.SelectedNode == treeView1.Nodes[0])
{
this.groupBox1.Visible = true;
this.button1.Visible = true;
this.textBox1.Visible = true;
this.textBox2.Visible = true;
this.label1.Visible = true;
this.label2.Visible = true;
this.label3.Visible = true;
}
else
{
this.groupBox1.Visible = false;
this.button1.Visible = false;
this.textBox1.Visible = false;
this.textBox2.Visible = false;
this.label1.Visible = false;
this.label2.Visible = false;
this.label3.Visible = false;
this.groupBox2.Visible = true;
this.button2.Visible = true;
this.textBox3.Visible = true;
this.textBox3.Visible = true;
this.labe4.Visible = true;
this.label5.Visible = true;
this.label6.Visible = true;
// bool success = selectColor();
}
Is my understanding correct ? Or do we have a better design approach for creating a settings form.
Thanks
You can create panels and swap their visibility. So depending on what setting is selected, you fill the controls and display a particular panel.
There are many ways but you can have a combination of Treeview control and Tab control. Check this link. This may help. In this link, the tabpages of tabcontrol are used as treeview control nodes.
Have a look at this link too.

Categories

Resources