locked
Read-only form RRS feed

  • Question

  • User-1462328531 posted

    Hi All,

          In one of my projects of asp.net2.0, I have to make the forms read-only as per the user who has logged-in. Because some users will have only read permission on the pages and some will have read-write both.

        Can anyone suggest me how to do it (with C# as scripting language)?

     Thanx a lot in advance.

    Monday, October 15, 2007 5:56 AM

Answers

  • User-168748142 posted

    Hi pppooriamadani,

                                 textbox.enabled = false will disable it only and it will be greyed out but you can see the textbox and the data. You can view the textbox. textbox.visibility=false will make the textbox unavailable and you cant see it. I thought there is a read-only property,that should also work I guess.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2007 10:12 AM
  • User-1658042184 posted

    hi snehal_bhayani145,

    I think you can do it something like this.

    For disabling all the controls in a page

    foreach(control in page.controls) 

    {

    control.enabled=false;

    I think this will helpful.

    Regards

    Ramesh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2007 3:40 AM
  • User-333244544 posted

    Hi.

     I've cut down my datagrid, and put the code to disable the edit control in C#, and here's what I came up with...

     

    1    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisableCSharp.aspx.cs" Inherits="DisableCSharp" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml">
    6    <head runat="server">
    7        <title>Disabling GridView Editing Programatically in C#</title>
    8    </head>
    9    <body>
    10       <form id="form1" runat="server">
    11       <div>
    12           <asp:LoginView ID="LoginView1" runat="server">
    13               <loggedintemplate>
    14                   Welcome,
    15                   <asp:LoginName ID="LoginName1" runat="server" />
    16                   <br />
    17                   <br />
    18                   <br />
    19                   <asp:LoginStatus ID="LoginStatus1" runat="server" />
    20                   <br />
    21               </loggedintemplate>
    22               <AnonymousTemplate>
    23                   <asp:Login ID="Login1" runat="server">
    24                   </asp:Login>
    25               </AnonymousTemplate>
    26           </asp:LoginView>
    27           <br />
    28           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CityID"
    29               DataSourceID="srcCity" >
    30               <Columns>
    31                   <asp:CommandField visible="false" ShowEditButton="True"></asp:CommandField>
    32                   <asp:BoundField DataField="CityNaam" HeaderText="City" SortExpression="CityNaam"></asp:BoundField>
    33               </Columns>
    34           </asp:GridView>
    35           <asp:SqlDataSource ID="srcCity" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
    36               SelectCommand="SELECT * FROM [tblCity]" ConflictDetection="CompareAllValues"
    37               DeleteCommand="DELETE FROM [tblCity] WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
    38               InsertCommand="INSERT INTO [tblCity] ([CountryID], [CityNaam]) VALUES (@CountryID, @CityNaam)"
    39               UpdateCommand="UPDATE [tblCity] SET [CountryID] = @CountryID, [CityNaam] = @CityNaam WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
    40               OldValuesParameterFormatString="original_{0}">
    41               <DeleteParameters>
    42                   <asp:Parameter Name="original_CityID" Type="Int16" />
    43                   <asp:Parameter Name="original_CountryID" Type="Int16" />
    44                   <asp:Parameter Name="original_CityNaam" Type="String" />
    45               </DeleteParameters>
    46               <UpdateParameters>
    47                   <asp:Parameter Name="CountryID" Type="Int16" />
    48                   <asp:Parameter Name="CityNaam" Type="String" />
    49                   <asp:Parameter Name="original_CityID" Type="Int16" />
    50                   <asp:Parameter Name="original_CountryID" Type="Int16" />
    51                   <asp:Parameter Name="original_CityNaam" Type="String" />
    52               </UpdateParameters>
    53               <InsertParameters>
    54                   <asp:Parameter Name="CountryID" Type="Int16" />
    55                   <asp:Parameter Name="CityNaam" Type="String" />
    56               </InsertParameters>
    57           </asp:SqlDataSource>
    58       </div>
    59       </form>
    60   </body>
    61   </html>
    62   
    

     and now the codebehind...

    1    using System;
    2    using System.Data;
    3    using System.Configuration;
    4    using System.Collections;
    5    using System.Linq;
    6    using System.Web;
    7    using System.Web.Security;
    8    using System.Web.UI;
    9    using System.Web.UI.WebControls;
    10   using System.Web.UI.WebControls.WebParts;
    11   using System.Web.UI.HtmlControls;
    12   using System.Xml.Linq;
    13   
    14   public partial class DisableCSharp : System.Web.UI.Page
    15   {
    16   
    17       DataView countryData;
    18       // this DataView will hold all of the countries, loaded at Page_Load
    19   
    20       protected void Page_Load(object sender, EventArgs e)
    21       {
    22       if (Roles.IsUserInRole(User.Identity.Name, "Admins"))
    23           {
    24               GridView1.Columns[0].Visible = true;
    25           }
    26       }
    27   }
    

     

    You can see this at http://liberator.boldlygoingnowhere.org/PlaySite/DisableCSharp.aspx User name Admin password Administrator!

    If you've got lots of controls, you should be able to extrapolate on this...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2007 1:23 PM

All replies

  • User-168748142 posted

    you can disabled all the controls int he form.

    so if a form has  one textbox.

    in C# or even vb.net you have

    textbox1.enabled = false

    textbox1 is the ID of the textbox

    Monday, October 15, 2007 9:29 AM
  • User-1116124093 posted

    Hi snehal,

     Thanks for your questions.

     No don't set enable to flase becuase you can't view that textbox anymore .

    There are many way to solve this problem that I'm going to suggest you one of them that can be use full for you .

    <input type="text" id="text1" runat="server" readonly="true" value="pooria" />

    if you use Html textbox and set runat="server" to it and place it on server side form you could access it from your server side code.And if you set readonly value from server side code to true the user only can see the value but won't be able to change that and if you set that to false the user can do anything with that text that you want.[Yes]

    Monday, October 15, 2007 10:09 AM
  • User-168748142 posted

    Hi pppooriamadani,

                                 textbox.enabled = false will disable it only and it will be greyed out but you can see the textbox and the data. You can view the textbox. textbox.visibility=false will make the textbox unavailable and you cant see it. I thought there is a read-only property,that should also work I guess.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2007 10:12 AM
  • User-1462328531 posted

    Hi all,

                Thanx a lot for your reply. But in this manner I will have to code for each and every textbox of the page as textbox1.readonly=true and when needed textbox1.readonly=false.

                 Actually I had posted this forum to know if there is any other method by which the whole form can be made readonly. Also what about other server controls like checkbox, dropdown, radiobuttons,etc......... on the form. 

                 So will I have to code for each and every server control individually by taking their ids. Wont this get too cumbersome and there can be possibility of missing a control on the form.

                 Thanx a lot once again.

    Tuesday, October 16, 2007 1:19 AM
  • User-333244544 posted

    Two methods: 

    1.  You could set up a MultiView on your page, one of which has editing enabled, and one which doesn't.
    You can then decide on which view is visible dependant on the user's privileges.

    2.  You could set up two separate pages, one of which has editing enabled, and one which doesn't.
    You can then send your user to the page dependant on their privileges.

    (There's probably a way of programatically disabling the "controls" column, but I haven't got time to look at that right now - must get off to work.)

    Hope this helps :)

    Tuesday, October 16, 2007 2:11 AM
  • User-333244544 posted

    I've had a play with this, and there is, indeed a way of disabling the "edit" column programatically.

    This page will allow users access to the editing funcions as long as they are in the "Admins" role...

     First the .aspx file...

     

    1    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DisableEdit.aspx.vb" Inherits="DisableEdit" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml">
    6    <head runat="server">
    7        <title>Untitled Page</title>
    8    </head>
    9    <body>
    10       <form id="form1" runat="server">
    11       <div>
    12           <asp:LoginView ID="LoginView1" runat="server">
    13               <loggedintemplate>
    14                   Welcome,
    15                   <asp:LoginName ID="LoginName1" runat="server" />
    16                   <br />
    17                   <br />
    18                   <br />
    19                   <asp:LoginStatus ID="LoginStatus1" runat="server" />
    20               </loggedintemplate>
    21               <AnonymousTemplate>
    22                   <asp:Login ID="Login1" runat="server">
    23                   </asp:Login>
    24               </AnonymousTemplate>
    25           </asp:LoginView>
    26           <br />
    27           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CityID"
    28               DataSourceID="srcCity" OnRowDataBound="cityRowDatabound">
    29               <Columns>
    30                   <asp:CommandField visible="false" ShowEditButton="True"></asp:CommandField>
    31                   <asp:BoundField DataField="CityNaam" HeaderText="City" SortExpression="CityNaam"></asp:BoundField>
    32                   <asp:TemplateField HeaderText="Country">
    33   
    34                       <EditItemTemplate>
    35                           <asp:DropDownList ID="ddl1" runat="server" DataSourceID="srcCountry" DataTextField="CountryNaam"
    36                               DataValueField="CountryID" SelectedValue='<%# Bind("CountryID") %>'>
    37                           </asp:DropDownList>
    38                       </EditItemTemplate>
    39   
    40                       <ItemTemplate>
    41                           <asp:Label ID="lbl1" runat="server" Text=""></asp:Label>
    42                       </ItemTemplate>
    43   
    44                   </asp:TemplateField>
    45               </Columns>
    46           </asp:GridView>
    47           <asp:SqlDataSource ID="srcCity" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
    48               SelectCommand="SELECT * FROM [tblCity]" ConflictDetection="CompareAllValues"
    49               DeleteCommand="DELETE FROM [tblCity] WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
    50               InsertCommand="INSERT INTO [tblCity] ([CountryID], [CityNaam]) VALUES (@CountryID, @CityNaam)"
    51               UpdateCommand="UPDATE [tblCity] SET [CountryID] = @CountryID, [CityNaam] = @CityNaam WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
    52               OldValuesParameterFormatString="original_{0}">
    53               <DeleteParameters>
    54                   <asp:Parameter Name="original_CityID" Type="Int16" />
    55                   <asp:Parameter Name="original_CountryID" Type="Int16" />
    56                   <asp:Parameter Name="original_CityNaam" Type="String" />
    57               </DeleteParameters>
    58               <UpdateParameters>
    59                   <asp:Parameter Name="CountryID" Type="Int16" />
    60                   <asp:Parameter Name="CityNaam" Type="String" />
    61                   <asp:Parameter Name="original_CityID" Type="Int16" />
    62                   <asp:Parameter Name="original_CountryID" Type="Int16" />
    63                   <asp:Parameter Name="original_CityNaam" Type="String" />
    64               </UpdateParameters>
    65               <InsertParameters>
    66                   <asp:Parameter Name="CountryID" Type="Int16" />
    67                   <asp:Parameter Name="CityNaam" Type="String" />
    68               </InsertParameters>
    69           </asp:SqlDataSource>
    70           <asp:SqlDataSource ID="srcCountry" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
    71               SelectCommand="SELECT * FROM [tblCountry]" />
    72       </div>
    73       </form>
    74   </body>
    75   </html>
    76   
    

     and now the codebehind...

    1    Imports System.Data
    2    
    3    Partial Class DisableEdit
    4        Inherits System.Web.UI.Page
    5    
    6        Protected Sub cityRowDatabound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    7            Dim lbl1 As Label
    8            If e.Row.RowType = DataControlRowType.DataRow Then
    9    
    10               If e.Row.RowState = DataControlRowState.Normal Or _
    11                   e.Row.RowState = DataControlRowState.Alternate Then
    12                   lbl1 = CType(e.Row.FindControl("lbl1"), Label)
    13                   countryData.RowFilter = "CountryID = " & CType(e.Row.DataItem, DataRowView)("CountryId").ToString()
    14                   lbl1.Text = CStr(countryData.Item(0).Row.Item("CountryNaam"))
    15               End If
    16   
    17           End If
    18       End Sub
    19   
    20   
    21   Dim countryData As DataView
    22   ' this DataView will hold all of the countries, loaded at Page_Load
    23   
    24   Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    25       ' Load all of the countries into a DataView from the SqlDataSource
    26       countryData = CType(srcCountry.Select(DataSourceSelectArguments.Empty), DataView)
    27   
    28       If Roles.IsUserInRole(User.Identity.Name, "Admins") Then
    29           GridView1.Columns(0).Visible = "true"
    30       End If
    31   
    32   End Sub
    33   
    34   End Class
    35   
    

     

    Notice that on line 30 of the .aspx file, I set the "visible" attribute of the "CommandField" column to FALSE by default.
    Then, on line 28/29 of the codebehind, I set it back to TRUE if the user is in the "Admins" role...

    To see this in action click on http://liberator.boldlygoingnowhere.org/PlaySite/DisableEdit.aspx Username is "Admin", password is "Administrator!" (Don't forget that exclamation mark/point)

    IMHO, this is a much neater way than the other ways suggested (including my own earlier suggestoins!).

     

    (Just realised you were asking about C#, and my example is in VB - I'll have a look at translating it in the morning...)

     

    (I put the wrong password in by mistake - I've corrected it now)

    Tuesday, October 16, 2007 5:56 PM
  • User-333244544 posted

    Sorry - Seems like the DataView class is completely different in C# from in VB (!) [*-)]

    Also, I've not written much in C#, and my C is very rusty.  Maybe someone else can translate to C# for you?

    Thursday, October 18, 2007 2:25 AM
  • User-1658042184 posted

    hi snehal_bhayani145,

    I think you can do it something like this.

    For disabling all the controls in a page

    foreach(control in page.controls) 

    {

    control.enabled=false;

    I think this will helpful.

    Thursday, October 18, 2007 3:40 AM
  • User-1658042184 posted

    hi snehal_bhayani145,

    I think you can do it something like this.

    For disabling all the controls in a page

    foreach(control in page.controls) 

    {

    control.enabled=false;

    I think this will helpful.

    Regards

    Ramesh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2007 3:40 AM
  • User-333244544 posted

    Hi.

     I've cut down my datagrid, and put the code to disable the edit control in C#, and here's what I came up with...

     

    1    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisableCSharp.aspx.cs" Inherits="DisableCSharp" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml">
    6    <head runat="server">
    7        <title>Disabling GridView Editing Programatically in C#</title>
    8    </head>
    9    <body>
    10       <form id="form1" runat="server">
    11       <div>
    12           <asp:LoginView ID="LoginView1" runat="server">
    13               <loggedintemplate>
    14                   Welcome,
    15                   <asp:LoginName ID="LoginName1" runat="server" />
    16                   <br />
    17                   <br />
    18                   <br />
    19                   <asp:LoginStatus ID="LoginStatus1" runat="server" />
    20                   <br />
    21               </loggedintemplate>
    22               <AnonymousTemplate>
    23                   <asp:Login ID="Login1" runat="server">
    24                   </asp:Login>
    25               </AnonymousTemplate>
    26           </asp:LoginView>
    27           <br />
    28           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CityID"
    29               DataSourceID="srcCity" >
    30               <Columns>
    31                   <asp:CommandField visible="false" ShowEditButton="True"></asp:CommandField>
    32                   <asp:BoundField DataField="CityNaam" HeaderText="City" SortExpression="CityNaam"></asp:BoundField>
    33               </Columns>
    34           </asp:GridView>
    35           <asp:SqlDataSource ID="srcCity" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
    36               SelectCommand="SELECT * FROM [tblCity]" ConflictDetection="CompareAllValues"
    37               DeleteCommand="DELETE FROM [tblCity] WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
    38               InsertCommand="INSERT INTO [tblCity] ([CountryID], [CityNaam]) VALUES (@CountryID, @CityNaam)"
    39               UpdateCommand="UPDATE [tblCity] SET [CountryID] = @CountryID, [CityNaam] = @CityNaam WHERE [CityID] = @original_CityID AND [CountryID] = @original_CountryID AND [CityNaam] = @original_CityNaam"
    40               OldValuesParameterFormatString="original_{0}">
    41               <DeleteParameters>
    42                   <asp:Parameter Name="original_CityID" Type="Int16" />
    43                   <asp:Parameter Name="original_CountryID" Type="Int16" />
    44                   <asp:Parameter Name="original_CityNaam" Type="String" />
    45               </DeleteParameters>
    46               <UpdateParameters>
    47                   <asp:Parameter Name="CountryID" Type="Int16" />
    48                   <asp:Parameter Name="CityNaam" Type="String" />
    49                   <asp:Parameter Name="original_CityID" Type="Int16" />
    50                   <asp:Parameter Name="original_CountryID" Type="Int16" />
    51                   <asp:Parameter Name="original_CityNaam" Type="String" />
    52               </UpdateParameters>
    53               <InsertParameters>
    54                   <asp:Parameter Name="CountryID" Type="Int16" />
    55                   <asp:Parameter Name="CityNaam" Type="String" />
    56               </InsertParameters>
    57           </asp:SqlDataSource>
    58       </div>
    59       </form>
    60   </body>
    61   </html>
    62   
    

     and now the codebehind...

    1    using System;
    2    using System.Data;
    3    using System.Configuration;
    4    using System.Collections;
    5    using System.Linq;
    6    using System.Web;
    7    using System.Web.Security;
    8    using System.Web.UI;
    9    using System.Web.UI.WebControls;
    10   using System.Web.UI.WebControls.WebParts;
    11   using System.Web.UI.HtmlControls;
    12   using System.Xml.Linq;
    13   
    14   public partial class DisableCSharp : System.Web.UI.Page
    15   {
    16   
    17       DataView countryData;
    18       // this DataView will hold all of the countries, loaded at Page_Load
    19   
    20       protected void Page_Load(object sender, EventArgs e)
    21       {
    22       if (Roles.IsUserInRole(User.Identity.Name, "Admins"))
    23           {
    24               GridView1.Columns[0].Visible = true;
    25           }
    26       }
    27   }
    

     

    You can see this at http://liberator.boldlygoingnowhere.org/PlaySite/DisableCSharp.aspx User name Admin password Administrator!

    If you've got lots of controls, you should be able to extrapolate on this...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2007 1:23 PM
  • User-1462328531 posted

    Hi

       Sorry for the delay in reply.

       Thanx a lot for your reply and all the effort and pains you took for it..

      Its working..

       Thanx once again.

      

    Tuesday, October 23, 2007 5:53 AM