dyarosh
asked on
How to show and hide the Edit and Select links in a datagridview in asp.net
I have a DataGridView on my page that has the AutoGenerateSelectButton and AutoGenerateEditButton set to true. When the user clicks on the select link, the selected row is highlighted and information is displayed in another update panel on the page. When the user clicks on the Edit link they can edit the information in the gridview as well as the data displayed in the other UpdatePanel.
I need to prevent the user from clicking on the Edit link on any row except the one selected.
I tried to hide the Edit link using the following code in the RowCreated event:
e.Row.Cells[-].Controls[0] .Visible = false
This worked great. Only the Select link appears.
In the SelectedIndexChanged event I unhide the Edit link and hide the Select link using the following code:
// Show Select Link and Hide Edit Link for previous row selected
GridViewRow prow = gvSchedules.Rows[gvSchedul es.Selecte dIndex];
prow.Cells[0].Controls[0]. Visible = false;
prow.Cells[0].Controls[2]. Visible = true;
GridViewRow row = gvSchedules.SelectedRow;
row.Cells[0].Controls[0].V isible = true;
row.Cells[0].Controls[1].V isible = false;
This also works great. Only the Edit link appears in the Selected row.
The problem occurs when I click on the Edit Link. I only get the Cancel link. I don't get the Update link. Can anyone tell me how to get the Update link to appear with my current setup? If I don't try and hide/show the Edit/Select links then when I click on Edit, the Update and Cancel links appear and work appropriately. Any help is greatly appreciated!
I need to prevent the user from clicking on the Edit link on any row except the one selected.
I tried to hide the Edit link using the following code in the RowCreated event:
e.Row.Cells[-].Controls[0]
This worked great. Only the Select link appears.
In the SelectedIndexChanged event I unhide the Edit link and hide the Select link using the following code:
// Show Select Link and Hide Edit Link for previous row selected
GridViewRow prow = gvSchedules.Rows[gvSchedul
prow.Cells[0].Controls[0].
prow.Cells[0].Controls[2].
GridViewRow row = gvSchedules.SelectedRow;
row.Cells[0].Controls[0].V
row.Cells[0].Controls[1].V
This also works great. Only the Edit link appears in the Selected row.
The problem occurs when I click on the Edit Link. I only get the Cancel link. I don't get the Update link. Can anyone tell me how to get the Update link to appear with my current setup? If I don't try and hide/show the Edit/Select links then when I click on Edit, the Update and Cancel links appear and work appropriately. Any help is greatly appreciated!
Check if the RowState is Edit then unhide the [0] control. May be RowEditing is the event that you should look doing it.
ASKER
I put a break in the RowEditing Event and this is what I found:
gvSchedules.Rows[e.NewEdit Index].Cel ls[0].Cont rols.Count = 3
....Cells[0].Controls[0].T ext = "Edit"
....Cells[0].Controls[0].V isible = "true"
....Cells[0].Controls[1].T ext is not defined
....Cells[0].Controls[1].V isible = "true"
....Cells[0].Controls[2].T ext = "Select"
....Cells[0].Controls[2].V isible = "false"
Here is what gvSchedules.Rows[e.NewEdit Index].Cel ls[0].Cont rols[1] shows:
? gvSchedules.Rows[e.NewEdit Index].Cel ls[0].Cont rols[1]
{System.Web.UI.LiteralCont rol}
[System.Web.UI.LiteralCont rol]: {System.Web.UI.LiteralCont rol}
AppRelativeTemplateSourceD irectory: "~/"
BindingContainer: {System.Web.UI.WebControls .GridViewR ow}
ClientID: "dynamiccontainer_onebox_o nebox_gvSc hedules_ct l02_0"
ClientIDMode: Inherit
Controls: {System.Web.UI.EmptyContro lCollectio n}
DataItemContainer: {System.Web.UI.WebControls .GridViewR ow}
DataKeysContainer: {System.Web.UI.WebControls .GridView}
EnableTheming: true
EnableViewState: false
ID: "ctl02"
NamingContainer: {System.Web.UI.WebControls .GridViewR ow}
Page: {ASP.maintainscheduledescr iptions_as px}
Parent: {System.Web.UI.WebControls .DataContr olFieldCel l}
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: {ASP.maintainscheduledescr iptions_as px}
TemplateSourceDirectory: "/"
UniqueID: "ctl00$ctl00$ctl00$dynamic container$ onebox$one box$gvSche dules$ctl0 2$ctl02"
ViewStateMode: Inherit
Visible: true
I can't find the Update and Cancel links and only the Cancel link shows.
gvSchedules.Rows[e.NewEdit
....Cells[0].Controls[0].T
....Cells[0].Controls[0].V
....Cells[0].Controls[1].T
....Cells[0].Controls[1].V
....Cells[0].Controls[2].T
....Cells[0].Controls[2].V
Here is what gvSchedules.Rows[e.NewEdit
? gvSchedules.Rows[e.NewEdit
{System.Web.UI.LiteralCont
[System.Web.UI.LiteralCont
AppRelativeTemplateSourceD
BindingContainer: {System.Web.UI.WebControls
ClientID: "dynamiccontainer_onebox_o
ClientIDMode: Inherit
Controls: {System.Web.UI.EmptyContro
DataItemContainer: {System.Web.UI.WebControls
DataKeysContainer: {System.Web.UI.WebControls
EnableTheming: true
EnableViewState: false
ID: "ctl02"
NamingContainer: {System.Web.UI.WebControls
Page: {ASP.maintainscheduledescr
Parent: {System.Web.UI.WebControls
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: {ASP.maintainscheduledescr
TemplateSourceDirectory: "/"
UniqueID: "ctl00$ctl00$ctl00$dynamic
ViewStateMode: Inherit
Visible: true
I can't find the Update and Cancel links and only the Cancel link shows.
If you set breakpoint in RowCreated event do you see Update link?
ASKER
In the RowCreated event I still do not see the Update and Cancel Links:
e.Row.Cells[0].Controls[0]
{Text = "Edit"}
[System.Web.UI.WebControls .DataContr olLinkButt on]: {Text = "Edit"}
AppRelativeTemplateSourceD irectory: "~/"
BindingContainer: {System.Web.UI.WebControls .GridViewR ow}
ClientID: "ctl00_0"
ClientIDMode: Inherit
Controls: {System.Web.UI.ControlColl ection}
DataItemContainer: {System.Web.UI.WebControls .GridViewR ow}
DataKeysContainer: null
EnableTheming: true
EnableViewState: true
ID: "ctl00"
NamingContainer: {System.Web.UI.WebControls .GridViewR ow}
Page: null
Parent: {System.Web.UI.WebControls .DataContr olFieldCel l}
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: null
TemplateSourceDirectory: "/"
UniqueID: "ctl00"
ViewStateMode: Inherit
Visible: true
e.Row.Cells[0].Controls[1]
{System.Web.UI.LiteralCont rol}
[System.Web.UI.LiteralCont rol]: {System.Web.UI.LiteralCont rol}
AppRelativeTemplateSourceD irectory: "~/"
BindingContainer: {System.Web.UI.WebControls .GridViewR ow}
ClientID: "ctl01_0"
ClientIDMode: Inherit
Controls: {System.Web.UI.EmptyContro lCollectio n}
DataItemContainer: {System.Web.UI.WebControls .GridViewR ow}
DataKeysContainer: null
EnableTheming: true
EnableViewState: false
ID: "ctl01"
NamingContainer: {System.Web.UI.WebControls .GridViewR ow}
Page: null
Parent: {System.Web.UI.WebControls .DataContr olFieldCel l}
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: null
TemplateSourceDirectory: "/"
UniqueID: "ctl01"
ViewStateMode: Inherit
Visible: true
e.Row.Cells[0].Controls[2]
{Text = "Select"}
[System.Web.UI.WebControls .DataContr olLinkButt on]: {Text = "Select"}
AppRelativeTemplateSourceD irectory: "~/"
BindingContainer: {System.Web.UI.WebControls .GridViewR ow}
ClientID: "ctl02_0"
ClientIDMode: Inherit
Controls: {System.Web.UI.ControlColl ection}
DataItemContainer: {System.Web.UI.WebControls .GridViewR ow}
DataKeysContainer: null
EnableTheming: true
EnableViewState: true
ID: "ctl02"
NamingContainer: {System.Web.UI.WebControls .GridViewR ow}
Page: null
Parent: {System.Web.UI.WebControls .DataContr olFieldCel l}
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: null
TemplateSourceDirectory: "/"
UniqueID: "ctl02"
ViewStateMode: Inherit
Visible: true
e.Row.Cells[0].Controls[0]
{Text = "Edit"}
[System.Web.UI.WebControls
AppRelativeTemplateSourceD
BindingContainer: {System.Web.UI.WebControls
ClientID: "ctl00_0"
ClientIDMode: Inherit
Controls: {System.Web.UI.ControlColl
DataItemContainer: {System.Web.UI.WebControls
DataKeysContainer: null
EnableTheming: true
EnableViewState: true
ID: "ctl00"
NamingContainer: {System.Web.UI.WebControls
Page: null
Parent: {System.Web.UI.WebControls
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: null
TemplateSourceDirectory: "/"
UniqueID: "ctl00"
ViewStateMode: Inherit
Visible: true
e.Row.Cells[0].Controls[1]
{System.Web.UI.LiteralCont
[System.Web.UI.LiteralCont
AppRelativeTemplateSourceD
BindingContainer: {System.Web.UI.WebControls
ClientID: "ctl01_0"
ClientIDMode: Inherit
Controls: {System.Web.UI.EmptyContro
DataItemContainer: {System.Web.UI.WebControls
DataKeysContainer: null
EnableTheming: true
EnableViewState: false
ID: "ctl01"
NamingContainer: {System.Web.UI.WebControls
Page: null
Parent: {System.Web.UI.WebControls
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: null
TemplateSourceDirectory: "/"
UniqueID: "ctl01"
ViewStateMode: Inherit
Visible: true
e.Row.Cells[0].Controls[2]
{Text = "Select"}
[System.Web.UI.WebControls
AppRelativeTemplateSourceD
BindingContainer: {System.Web.UI.WebControls
ClientID: "ctl02_0"
ClientIDMode: Inherit
Controls: {System.Web.UI.ControlColl
DataItemContainer: {System.Web.UI.WebControls
DataKeysContainer: null
EnableTheming: true
EnableViewState: true
ID: "ctl02"
NamingContainer: {System.Web.UI.WebControls
Page: null
Parent: {System.Web.UI.WebControls
RenderingCompatibility: {4.0}
Site: null
SkinID: ""
TemplateControl: null
TemplateSourceDirectory: "/"
UniqueID: "ctl02"
ViewStateMode: Inherit
Visible: true
try the following:
//set all visible in currently selected row.
GridViewRow row = gvSchedules.SelectedRow;
row.Cells[0].Controls[0].V isible = true;
row.Cells[0].Controls[1].V isible = true;
row.Cells[0].Controls[2].V isible = true;
//set all visible in currently selected row.
GridViewRow row = gvSchedules.SelectedRow;
row.Cells[0].Controls[0].V
row.Cells[0].Controls[1].V
row.Cells[0].Controls[2].V
ASKER
Did as you suggested and it still just shows the Cancel link when Edit is clicked.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. This will make is so much easier for my user.
Wouldn't 1 be the update link?
If you set a break point on that line, you can interrogate each of the controls via the immediate window to find out which control needs to be visible.