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

Setting Datagrid EditItem Controls to Read Only Dynamically

Hi,

Ive create an line by line editable datagrid using EditItemIndex that works fine. My only problem now is that based on a users user group i need to restrict the ability to edit the data edit. The users usergroup is held in a session variable.

For anonymous users i need the entire datagrid read only and which I think could be simply done by setting the the column for selecting a row to edit as hidden. How can i do this?

Also based on the user group I need to set certain controls in the editable row to read only so they can't be changed. Can this be done?

Im coding in c# but any code snippets I can examine or suggestions would be thankfully recieved.

My Current Code is available here: http://forums.aspfree.com/attachment.php?attachmentid=3631
0
ltarrant
Asked:
ltarrant
  • 6
  • 5
  • 5
2 Solutions
 
the_paabCommented:
Columns you can hide like this:

datagrid1.columns(0). visible = False
datagrid1.columns(1). visible = False
etc.


Controls on the datagrid row you can make read only in databinding handler like this (example for TextBox control):
Sub DataGrid1_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
        if e.Item.ItemType = ListItemType.Item or _
             e.Item.ItemType = ListItemType.AlternatingItem Then
   
             ctype(e.item.findcontrol("id_of_textbox"),textBox).enabled = false
        end if
End Sub
0
 
lesmantecCommented:
Well, I think what you can do is to include two controls (one textbox and one label) for the EditItemTemplate of your template column. Then, inside your DataGrid.ItemDataBound event handler, you set the visibility of one control to true and the other to false according the user access, such as follows:

private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      if(e.Item.ItemType == ListItemType.EditItem)
      {
                   TextBox myTextbox = (TextBox)e.Item.FindControl("MyTextBox");
                   Label myLabel = (Label)e.Item.FindControl("MyLabel");
                   switch((string)Session["UserGroup"])
                   {
                       case "Modifier":
                          myTextbox.Visible = true;
                          myLabel.Visible = false;
                       case "Reader":
                          myLabel.Visible = true;
                          myTextbox.Visible = false;
                   }
               }
}

Remember that you also need to check the user group when handling the data saving inside ItemCommand event handler.

I hope this helps. Good luck.

0
 
ltarrantAuthor Commented:
Hi,

 Thanks for the replies. lesmantec you solutions works. However, I'm keen on the_paab's answer because it seem very simple. But its is VB and my page is in c#.

The datagrid1.columns(0). visible = False should work but i get the following error

CS0118: 'System.Web.UI.WebControls.DataGrid.Columns' denotes a 'property' where a 'method' was expected

I have tried converting the "ctype(e.item.findcontrol("id_of_textbox"),textBox).enabled = false" to c#

I made it this which i think is correct: "((TextBox) e.item.findcontrol("tbCoach1")).Enabled = false;" However, i get this error

CS0122: 'System.Web.UI.WebControls.DataGridCommandEventArgs.item' is inaccessible due to its protection level

Cheers,

Luke
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
the_paabCommented:
I used this tool http://carlosag.net/Tools/CodeTranslator/Default.aspx for converting my code VB to C#:

datagrid1.columns[0].visible = false;
datagrid1.columns[1].visible = false;
...

    void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) {
        if (((e.Item.ItemType == ListItemType.Item)
                    || (e.Item.ItemType == ListItemType.AlternatingItem))) {
            ((textBox)(e.item.findcontrol("id_of_textbox"))).enabled = false;
        }
    }

0
 
lesmantecCommented:
OK,
datagrid1.columns(0).visible = false is supposed to be changed to:
datagrid1.Columns[0].Visible = false;

well the second one, where do you put "((TextBox) e.item.findcontrol("tbCoach1")).Enabled = false;" ?
0
 
ltarrantAuthor Commented:
For the 2nd one i put the code as follows inside the EditCommand:


void Edit_Command(Object sender, DataGridCommandEventArgs e)
{
    myDataGrid.EditItemIndex = e.Item.ItemIndex;
   
    switch((string)Session["UserGroup"])
    {
        case "Admin":
             ((TextBox) e.item.findcontrol("tbDate")).Enabled = true;
             ((TextBox) e.item.findcontrol("tbCoach1")).Enabled = true;
             ((TextBox) e.item.findcontrol("tbCoach2")).Enabled = true;
             ((TextBox) e.item.findcontrol("tbCoach3")).Enabled = true;
             ((TextBox) e.item.findcontrol("tbCoach4")).Enabled = true;
        break;

        case "Mod":
             ((TextBox) e.item.findcontrol("tbDate")).Enabled = false;
             ((TextBox) e.item.findcontrol("tbCoach1")).Enabled = true;
             ((TextBox) e.item.findcontrol("tbCoach2")).Enabled = true;
             ((TextBox) e.item.findcontrol("tbCoach3")).Enabled = true;
             ((TextBox) e.item.findcontrol("tbCoach4")).Enabled = true;
        break;
       
       default:
             ((TextBox) e.item.findcontrol("tbDate")).Enabled = false;
             ((TextBox) e.item.findcontrol("tbCoach1")).Enabled = false;
             ((TextBox) e.item.findcontrol("tbCoach2")).Enabled = false;
             ((TextBox) e.item.findcontrol("tbCoach3")).Enabled = false;
             ((TextBox) e.item.findcontrol("tbCoach4")).Enabled = false;
       break;
        }
   
    BindList();
}
0
 
lesmantecCommented:
((TextBox) e.item.findcontrol("tbCoach4")).Enabled = false;  -> is definitely wrong

Should be:
((TextBox) e.Item.FindControl("tbCoach4")).Enabled = false; -> put attention on the capital letter. item is a private field of DataGridCommandEventArgs object, while Item is a public property.


Hope this helps.

0
 
ltarrantAuthor Commented:
Cheers again lesmantec. The page now loads. However, when I click the edit button I get this error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 43:          {
Line 44:              case "admin":
Line 45:                   ((TextBox)e.Item.FindControl("tbDate")).Enabled = true;
Line 46:                   ((TextBox)e.Item.FindControl("tbCoach1")).Enabled = true;
Line 47:                   ((TextBox)e.Item.FindControl("tbCoach2")).Enabled = true;
 

Source File: C:\Documents and Settings\ltarrant\Desktop\dg\rota.aspx    Line: 45
0
 
lesmantecCommented:
well, make sure that you set the IDs of your textboxes to tbDate, tbCoach1, tbCoach2. Also, where do you put those three textboxes? Within the itemtemplate or edititemtemplate? If you put under edititemtemplate (I suppose you do), then you have to move your statement inside the if condition to check the item type:

if (e.Item.ItemType == ListItemType.EditItem)
{
  //put your code that access those textboxes.
}

Good luck
0
 
ltarrantAuthor Commented:
Hi, yes the TextBoxes have there ID's set correctly and they are in the EditItemTemplate. I was missing the "if (e.Item.ItemType == ListItemType.EditItem)2 part. I now don't get any compilation errors. However, it simply doesnt work. Even setting the box to readonly or visible to flase the text box always appears and can be edited regardless of user group.

Any ideas?
0
 
the_paabCommented:
Just Idea. What happens when you put call of the BindList procedure in front of switch command?
0
 
ltarrantAuthor Commented:
Hi, I tried doing that already but still no luck!
0
 
lesmantecCommented:
OK, try to put all those dynamic textbox control statements inside the PreRender event handler for the datagrid. You should of course include the ItemType checking for each of the DataGridItem to check whether the item is of type EditItem.
0
 
the_paabCommented:
If you set datasource of your datagrid via DataSourceID, try to enable/disable Controls even in PrePrenderComplete of the page. Or do it in Bind time of the datagrid:

void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) {
        if (e.Item.ItemType == ListItemType.EditItem) {
            //
        }
    }
0
 
the_paabCommented:
I found typing error in my last answer, instead of "PrePrenderComplete" should be be "PreRenderComplete".
0
 
ltarrantAuthor Commented:
Hi Guys,

I been meaning to close this post. I managed to fix the problem. I had all the right code thanks to you guys I just didnt realise it wouldnt work unless i put it inside the itemdatabound command and i had it in the edit command method. I notice you suggest that above now.

Anyway. Thanks again for your help. You both been really good so im going to split to points to avoid either being disappointed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now