This is a migrated thread and some comments may be shown as answers.

password problem in grid

9 Answers 314 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jaws1021
Top achievements
Rank 1
jaws1021 asked on 28 Jun 2010, 12:37 AM
how can I have GridBoundColumn called password and have this password show in TextMode = "password" that no one can see what is the password is for security reason..

I have RadGrid that has edit, insert, delete, update events...

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Jun 2010, 05:51 AM
Hello,

You can achieve this by accessing the GridBoundColumn from code behind and set its TextMode property as "Password" like below.

ASPX:
<telerik:GridBoundColumn UniqueName="Password" DataField="Password" HeaderText="Password" Visible="false"

C#:
 
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
        { 
            GridEditFormItem edititem = (GridEditFormItem)e.Item; 
            TextBox txtpwd = (TextBox)edititem["Password"].Controls[0]; 
            txtpwd.TextMode = TextBoxMode.Password; 
            txtpwd.Visible = true
        } 
    } 
 
 

Thanks,
Princy.


0
jaws1021
Top achievements
Rank 1
answered on 28 Jun 2010, 08:07 PM
why you said to password to visible = false? and Control(0) ?

so far the code didnt work..
0
Princy
Top achievements
Rank 2
answered on 29 Jun 2010, 06:18 AM
Hello,

The above code is a small part of code that I used once for accomplishing my requirement. I set Visible to false in order to hide the password column in normal mode for the security reason. I tried accessing the textbox rendered in edit mode by using the code:
            GridEditFormItem edititem = (GridEditFormItem)e.Item;
            TextBox txtpwd = (TextBox)edititem["Password"].Controls[0]; 

 and then set the TextMode property to "Password" to hide the text that is typed.

You can change the logic according to your need. I guess you are trying a different scenario. Sorry if it confused you. Could you provide some more information about the requirement if that is different than this?

Thanks,
Princy.
0
jaws1021
Top achievements
Rank 1
answered on 29 Jun 2010, 04:54 PM
that didnt work, but thanks for the effort.. can close this question.
0
Kannan
Top achievements
Rank 1
answered on 27 Dec 2010, 12:35 PM
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
        { 
            GridEditFormItem edititem = (GridEditFormItem)e.Item; 
            TextBox txtpwd = (TextBox)edititem["Password"].Controls[0]; 
            txtpwd.TextMode = TextBoxMode.Password; 
            txtpwd.Visible = true
        } 
    } 

Hi,

I have used the above coding to mask the password text box. It is working but the problem is in edit mode the data which is bidden to that textbox is removed and the textbox remains empty. If i commented this line "txtpwd.TextMode = TextBoxMode.Password;" then the textbox shows the password, but the password is readable. When i uncomment it the textbox remains empty.

Secondly i need to display a message that if user name and password fields are empty. That is i need set user name & password as mandatory fields.

Please give me a solution.

Thanks & Regards
Kannan
0
Princy
Top achievements
Rank 2
answered on 28 Dec 2010, 11:54 AM
Hello Kannan,

If you want to show password in edit mode, one suggestion is to try the following approach.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
      if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
      {
          GridEditFormItem edititem = (GridEditFormItem)e.Item;
          DataRowView dr = (DataRowView) edititem.DataItem;
          TextBox txtpwd = (TextBox)edititem["Password"].Controls[0];
          txtpwd.TextMode = TextBoxMode.Password;
          txtpwd.Attributes.Add("value", dr["Password"].ToString());
          txtpwd.Visible = true;
      }
  }

Another option is using GridTemplateColumn with TextBox inside EditItemTemplate instead of GridBoundColumn. Then try to add the value attribute to TextBox like below.

ASPX:
<telerik:GridTemplateColumn>
  <EditItemTemplate>
    <asp:TextBox ID="txtPassword" runat="server" TextMode="password"
         value='<%# Eval("Password") %>'  Text='<%# Bind("Password") %>'>
    </asp:TextBox>
  </EditItemTemplate>
</telerik:GridTemplateColumn>

And please refer the following documentation for validating control in edit form.
Validation

Thanks,
Princy.
0
Kannan
Top achievements
Rank 1
answered on 28 Dec 2010, 12:21 PM
Hi Princy,

Thanks for ur code. And its working fine.

For validation i have used RequiredFieldValidator it does the validation in a good manner. I am using RadTabStrip and RadGrid in my app.

But the problem i am facing now is when i click the add new record button  and then if i click any of the other tab in the page, the validators triggers and its not allowing to view the other tab which i clicked. Its poping up the message which i wrote in RequiredFieldValidator For eg: Enter User Name.

Here is the sample code for validation which i used :

 

 

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if ((e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) || (e.Item is GridEditableItem && e.Item.IsInEditMode))
    {
         GridEditableItem item = e.Item as GridEditableItem
        RequiredFieldValidator validator = new RequiredFieldValidator();
        validator.ControlToValidate = ((
TextBox)item["username"].Controls[0]).ID;
        validator.ErrorMessage =
"* Enter User Name";
        item[
"username"].Controls.Add(validator);
        RequiredFieldValidator validator1 = new RequiredFieldValidator();
        validator1.ControlToValidate = ((
TextBox)item["password"].Controls[0]).ID;
        validator1.ErrorMessage =
"* Enter Password";
        item[
"password"].Controls.Add(validator1);
    }
}

And i have one more Requirement that is I should show astrick symbol (which shows the user as required field) after the textbox or what ever control it is after when i click the Add New Record Button.

Please give me a solution.

Thanks & Regards
Kannan

 

0
Princy
Top achievements
Rank 2
answered on 29 Dec 2010, 08:16 AM
Hello Kannan,

In order to avoid this problem, you can set ValidationGroup to the validator, control to validate and to the button. Sample code is given below.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if ((e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) || (e.Item is GridEditableItem && e.Item.IsInEditMode))
        {
            GridEditableItem item = e.Item as GridEditableItem;
            RequiredFieldValidator validator = new RequiredFieldValidator();
            validator.ErrorMessage = "* Enter User Name";
            validator.ControlToValidate = ((TextBox)item["username"].Controls[0]).ID;
            validator.ValidationGroup = "ValidationGroup1";//setting ValidationGroup to the validator
             
            ((TextBox)item["username"].Controls[0]).ValidationGroup = "ValidationGroup1";//setting ValidationGroup to the control
             
            LinkButton insertbutton = (LinkButton)item.FindControl("PerformInsertButton");
            insertbutton.ValidationGroup = "ValidationGroup1";//setting ValidationGroup to the button
             
            item["username"].Controls.Add(validator);
        }
    }

And for your second requirement, you can create a span dynamically like below.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
       {
           if ((e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) || (e.Item is GridEditableItem && e.Item.IsInEditMode))
           {
               GridEditableItem item = e.Item as GridEditableItem;
               TableCell cell = (TableCell)item["username"];
               // creating span
               var span = new HtmlGenericControl("span");
               span.InnerHtml = "*";
               cell.Controls.Add(span);
           }
   }

Thanks,
Princy.
0
Kannan
Top achievements
Rank 1
answered on 29 Dec 2010, 12:37 PM
Hi Princy

Thanks for your code and it is working fine

Thanks & Regards
Kannan
Tags
Grid
Asked by
jaws1021
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
jaws1021
Top achievements
Rank 1
Kannan
Top achievements
Rank 1
Share this question
or