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

Show Password Button

7 Answers 30 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Grant
Top achievements
Rank 1
Grant asked on 09 Jul 2014, 07:48 AM
Hi 

I have a grid button(linkbutton) , that I want to show a password from a data source after I click it.

So I need to change the GridButtonColumn to a GridBoundColumn after the button is clicked, is this possible?

The I want to change it to a GridBoundColumn becuase I need the DataField and HeaderText properties.

Help would be much appreciated.

Grant.

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 09 Jul 2014, 09:10 AM
Hi Grant,

I'm not clear about your requirement, I guess you have a GridButtonColumn and on its click you want to change that row's  to GridBoundColumn, as far as I know, this is not possible. As a suggestion you can use GridButtonColumn and set GridBoundColumn with required datafield and set display as false, then on its click you can display the text you want. Please take a look at the following code snippet.

ASPX:
<telerik:GridButtonColumn Text="ShowPassword" UniqueName="buttoncolumn" CommandName="ShowPassword" ButtonType="LinkButton">
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="Password" HeaderText="Password" UniqueName="Password" Display="false" />

C#:
void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == "ShowPassword")
  {
   GridDataItem dataItem = (GridDataItem)e.Item;
   dataItem["buttoncolumn"].Text = dataItem["Password"].Text;
  }
}

Please elaborate on your exact requirement if this doesn't help you.
Thanks,
Princy
0
Grant
Top achievements
Rank 1
answered on 09 Jul 2014, 10:05 AM
Hi Princy,

Thank you for the response, I didnt want to have a button next to the password field because it isn't the desired look that I am going for.
All I want is to have a link or button, that when clicked shows the password field, I would like it to be one control(not a button on the side that makes the field next to it visible when clicked).
Could i possible do this with a hover over event ?

Thanks
Grant
0
Princy
Top achievements
Rank 1
answered on 10 Jul 2014, 12:58 PM
Hi Grant,

Can you please elaborate on your requirement, I'm not clear about what you are trying to achieve. Why is it you need the DataField and HeaderText properties?

Thanks,
Princy
0
Grant
Top achievements
Rank 1
answered on 10 Jul 2014, 01:21 PM
Hi Princy,

I have a gridview with several gridboundcolumns, that I bind with data from my db.
I need a button or a link in place of where the password is, so that when clicked it shows the password.

If you look at the image attached , you will see what I mean.

Thanks
Grant
0
Princy
Top achievements
Rank 1
answered on 11 Jul 2014, 08:51 AM
Hi Grant,

You can bind a LinkButton to the BoundColumn and on it's click you can show the column text as shown below:

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        LinkButton lnkpswd = new LinkButton();
        lnkpswd.ID = "lnkPassword";
        lnkpswd.Text = "Show Password";
        dataItem["Password"].Controls.Add(lnkpswd);
    }
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        LinkButton lnkpswd = new LinkButton();
        lnkpswd.ID = "lnkPassword";
        lnkpswd.Text = "Show Password";
        lnkpswd.Click += new EventHandler(lnkpswd_Click);
        dataItem["Password"].Controls.Add(lnkpswd);
    }
}
 
void lnkpswd_Click(object sender, EventArgs e)
{
    LinkButton lnkpswd = (LinkButton)sender;
    GridDataItem dataItem = (GridDataItem)lnkpswd.NamingContainer;
    lnkpswd.Text = dataItem["Password"].Text;
    lnkpswd.Enabled = false;
}

Thanks,
Princy
0
Grant
Top achievements
Rank 1
answered on 14 Jul 2014, 10:09 AM
Hi Princy,

Thanks that helped a lot, but when I bind the data it goes back to " show password"

Im struggling to bind the data for just the linkbutton column and leaving all the other data the same.

Thanks Grant
0
Accepted
Princy
Top achievements
Rank 1
answered on 15 Jul 2014, 08:06 AM
Hi Grant,

Its the default behavior of the Grid not to persist any changes when the Grid rebinds. If you want the changes to persist, please try the following:

1.Add an ItemCommand event handler to the grid.
When a "Show Password" command occurs, store the key values for the selected row in a Session variable.

2.Add a PreRender event handler to the grid.
In the pre-render event of the grid, traverse the rows of the grid and compare their key values to the values saved in the Session variable. Whenever you find a match,show the text for that row.

Below is the complete code behind:

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        LinkButton lnkpswd = new LinkButton();
        lnkpswd.ID = "lnkPassword";
        lnkpswd.Text = "Show Password";
        dataItem["Password"].Controls.Add(lnkpswd);
    }
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        LinkButton lnkpswd = new LinkButton();
        lnkpswd.ID = "lnkPassword";
        lnkpswd.Text = "Show Password";
        lnkpswd.CommandName = "ShowPassword";
        lnkpswd.Click += new EventHandler(lnkpswd_Click);
        dataItem["Password"].Controls.Add(lnkpswd);
    }
}
 
void lnkpswd_Click(object sender, EventArgs e)
{
    LinkButton lnkpswd = (LinkButton)sender;
    GridDataItem dataItem = (GridDataItem)lnkpswd.NamingContainer;
    lnkpswd.Text = dataItem["Password"].Text;
    lnkpswd.Enabled = false;
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    ArrayList selectedItems;
    if (Session["selectedItems"] == null)
    {
        selectedItems = new ArrayList();
    }
    else
    {
        selectedItems = (ArrayList)Session["selectedItems"];
    }
    if (e.CommandName == "ShowPassword" && e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        string ID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["ID"].ToString();
        selectedItems.Add(ID);
        Session["selectedItems"] = selectedItems;
    }      
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (Session["selectedItems"] != null)
    {
        ArrayList selectedItems = (ArrayList)Session["selectedItems"];
        Int16 stackIndex;
        for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
        {
            string curItem = selectedItems[stackIndex].ToString();
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                if (item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)item;
                    if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["ID"].ToString()))
                    {
                        LinkButton lnkpswd = (LinkButton)dataItem.FindControl("lnkPassword");
                        lnkpswd.Enabled = false;
                        lnkpswd.Text = dataItem["Password"].Text;
                        break;
                    }
                }
            }
        }
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Grant
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Grant
Top achievements
Rank 1
Share this question
or