?
Solved

Setting Datagrid EditItem Controls to Read Only Dynamically

Posted on 2006-05-04
16
Medium Priority
?
588 Views
Last Modified: 2008-01-09
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
Comment
Question by:ltarrant
  • 6
  • 5
  • 5
16 Comments
 
LVL 6

Expert Comment

by:the_paab
ID: 16603844
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
 
LVL 4

Accepted Solution

by:
lesmantec earned 260 total points
ID: 16604051
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
 

Author Comment

by:ltarrant
ID: 16604152
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Assisted Solution

by:the_paab
the_paab earned 240 total points
ID: 16604200
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
 
LVL 4

Expert Comment

by:lesmantec
ID: 16604266
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
 

Author Comment

by:ltarrant
ID: 16604408
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
 
LVL 4

Expert Comment

by:lesmantec
ID: 16611831
((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
 

Author Comment

by:ltarrant
ID: 16612526
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
 
LVL 4

Expert Comment

by:lesmantec
ID: 16612554
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
 

Author Comment

by:ltarrant
ID: 16622087
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
 
LVL 6

Expert Comment

by:the_paab
ID: 16622401
Just Idea. What happens when you put call of the BindList procedure in front of switch command?
0
 

Author Comment

by:ltarrant
ID: 16632496
Hi, I tried doing that already but still no luck!
0
 
LVL 4

Expert Comment

by:lesmantec
ID: 16636918
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
 
LVL 6

Expert Comment

by:the_paab
ID: 16637041
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
 
LVL 6

Expert Comment

by:the_paab
ID: 16637765
I found typing error in my last answer, instead of "PrePrenderComplete" should be be "PreRenderComplete".
0
 

Author Comment

by:ltarrant
ID: 16645943
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

621 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