I have RadGrid that has edit, insert, delete, update events...
9 Answers, 1 is accepted

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.

so far the code didnt work..

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.


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

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.

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

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.

Thanks for your code and it is working fine
Thanks & Regards
Kannan