Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide/Display columns in Gridview not working

Status
Not open for further replies.

JRWM

Programmer
Mar 9, 2009
1
US
I have a gridview with 10 columns with the last three columns having linkbuttons in them. The first column displaying them in the Item Template, the second within the Edit Template and the third in a footer template. Depending upon which linkbutton is clicked within the Item Template, I am trying to hide/display the other two columns with link buttons. This is to avoid having users clicking on other buttons without using the Cancel button.

When i click on the edit button, the first and third column of linkbuttons are hidden and only the second column with the EditTemplate displays...which works, but when I click on my Insert linkbuttons which are supposed to hide the first and second columns, it also hides the third column that displays the footertemplate. In addition, my footer controls, which I set to true at this time, do not display even though I can see they are set to true while debugging. I've listed some of my code. Any help is greatly appreciated as my deadline is coming up on this project.

Thank You!!!

aspx code (last 5 columns in gridview 6-10):
<asp:Gridview id="gv_Current"....

<asp:TemplateField HeaderText="PL Percentage">
<EditItemTemplate>
<asp:TextBox ID="txtPLPerc" runat="server" Text='<%# Bind("PLPer") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPLPerc" runat="server" Text='<%# Eval("PLPer") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPLPerc_f" runat="server" Visible="false" />
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="PL Value$">
<ItemTemplate>
<asp:Label ID="lblPLValue" runat="server" Text='<%# Eval("PLValue") %>' />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CausesValidation="true" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="btnInsert_T" runat="server" CausesValidation="true" CommandName="Insert_T" Text="Add Tech" />
<asp:LinkButton ID="btnInsert_BU" runat="server" CausesValidation="true" CommandName="Insert_BU" Text="Add BU" />
<asp:LinkButton ID="btnInsert_PL" runat="server" CausesValidation="False" CommandName="Insert_PL" Text="Add PL" />
<asp:LinkButton ID="btnDelete" runat="server" CausesValidation="true" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" CausesValidation="False" CommandName="Update" Text="Update" />
<asp:LinkButton ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="btnInsert_T_f" runat="server" CausesValidation="true" CommandName="Insert" Text="Insert" Visible="false" />
<asp:LinkButton ID="btnInsert_PL_f" runat="server" CausesValidation="true" CommandName="Insert_PL2" Text="Insert" Visible="false" />
<asp:LinkButton ID="btnInsert_BU_f" runat="server" CausesValidation="true" CommandName="Insert_BU2" Text="Insert" Visible="false" />
<asp:LinkButton ID="btnCancelInsert_f" runat="server" CausesValidation="true" CommandName="Cancel" Text="Cancel" Visible="false" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


vb code:
I first tried it in the RowDataBound sub.

Protected Sub gv_CurrentPlan_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If ViewState("strCmdButton") = "Edit" Or ViewState("strCmdButton") = "InsertBU" Or ViewState("strCmdButton") = "InsertPL" Or ViewState("strCmdButton") = "Insert" Then
Response.Write("12d")
e.Row.Cells(8).Visible = False
e.Row.Cells(10).Visible = False
End If

End Sub
I then tried in the RowCommand Sub. here is the code for one of the insert buttons that don't work and the Edit button that does work.

Protected Sub gv_CurrentPlan_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_CurrentPlan.RowCommand

Dim lblTech_f As Label = CType(gv_CurrentPlan.FooterRow.FindControl("lblTech_f"), Label)
Dim lblTechTarget_f As Label = CType(gv_CurrentPlan.FooterRow.FindControl("lblTechTarget_f"), Label)
Dim lblBU_f As Label = CType(gv_CurrentPlan.FooterRow.FindControl("lblBU_f"), Label)
Dim lblBUPerc_f As Label = CType(gv_CurrentPlan.FooterRow.FindControl("lblBUPerc_f"), Label)
Dim lblBUValue_f As Label = CType(gv_CurrentPlan.FooterRow.FindControl("lblBUValue_f"), Label)
Dim ddlTech_f As DropDownList = CType(gv_CurrentPlan.FooterRow.FindControl("ddlTech_f"), DropDownList)
Dim txtTechTarget_f As TextBox = CType(gv_CurrentPlan.FooterRow.FindControl("txtTechTarget_f"), TextBox)
Dim ddlBU_f As DropDownList = CType(gv_CurrentPlan.FooterRow.FindControl("ddlBU_f"), DropDownList)
Dim txtBUPerc_f As TextBox = CType(gv_CurrentPlan.FooterRow.FindControl("txtBUPerc_f"), TextBox)
Dim btnIns_T_f As LinkButton = CType(gv_CurrentPlan.FooterRow.FindControl("btnInsert_T_f"), LinkButton)
Dim btnCancelIns_f As LinkButton = CType(gv_CurrentPlan.FooterRow.FindControl("btnCancelInsert_f"), LinkButton)
Dim ddlPL_f As DropDownList = CType(gv_CurrentPlan.FooterRow.FindControl("ddlPL_f"), DropDownList)
Dim txtPLPerc_f As TextBox = CType(gv_CurrentPlan.FooterRow.FindControl("txtPLPerc_f"), TextBox)
Dim btninsert As LinkButton = CType(gv_CurrentPlan.FooterRow.FindControl("btnInsert_T_f"), LinkButton)
Dim btnIns_BU_f As LinkButton = CType(gv_CurrentPlan.FooterRow.FindControl("btnInsert_BU_f"), LinkButton)
Dim btnIns_PL_f As LinkButton = CType(gv_CurrentPlan.FooterRow.FindControl("btnInsert_PL_f"), LinkButton)
Dim btnIns_PL As LinkButton = CType(gv_CurrentPlan.FindControl("btnInsert_PL"), LinkButton)

'Variables for Alert Message
Dim strMessage, csName, strScript As String
Dim cstype As Type = Me.GetType

'Variables for selecting grid-row
Dim lb As LinkButton = CType(e.CommandSource, LinkButton)
Dim gvRow As GridViewRow = lb.BindingContainer

If .....

ElseIf e.CommandName = "Insert_PL" Then

Dim listPL As DropDownList = gv_CurrentPlan.FooterRow.FindControl("ddlPL_f")
Dim dsc2 As SqlDataSource = gv_CurrentPlan.FooterRow.FindControl("SqlDS_PL_f")
Dim dv As System.Data.DataRowView = gv_CurrentPlan.FooterRow.DataItem

ViewState.Add("iDIDKey", gv_CurrentPlan.DataKeys(gvRow.RowIndex).Values(0))
ViewState.Add("iBUKey", gv_CurrentPlan.DataKeys(gvRow.RowIndex).Values(2))

Dim connectionString As String = WebConfigurationManager.ConnectionStrings("AnnualPlan").ConnectionString
Dim objConn As New SqlConnection(connectionString)
Dim strSQL As String = "SELECT statement"
Dim objCmd As New SqlCommand(strSQL, objConn)

objConn.Open()
Dim objDR As SqlDataReader = objCmd.ExecuteReader
objDR.Read()

dsc2.SelectParameters("BID").DefaultValue = objDR("BID")
listPL.DataBind()

ViewState.Add("iTID", objDR("TID"))
ViewState.Add("iTarget", objDR("Target"))
ViewState.Add("iBID", objDR("BID"))
ViewState.Add("iBUPer", objDR("BUPer"))
ViewState.Add("iBUValue", objDR("BUValue"))
lblTech_f.Text = objDR("Technology")
lblBU_f.Text = objDR("BU")

objDR.Close()
objConn.Close()

lblTechTarget_f.Text = ViewState("iTarget")
lblBUPerc_f.Text = ViewState("iBUPer")
lblBUValue_f.Text = ViewState("iBUValue")

btnIns_PL_f.Visible = True
lblTechTarget_f.Visible = True
lblTech_f.Visible = True
lblBU_f.Visible = True
lblBUPerc_f.Visible = True
lblBUValue_f.Visible = True
ddlPL_f.Visible = True
txtPLPerc_f.Visible = True
btnCancelIns_f.Visible = True

ViewState.Add("strCmdButton", "InsertPL")

gv_CurrentPlan.Columns(8).Visible = False
gv_CurrentPlan.Columns(9).Visible = False

ElseIf e.CommandName = "Edit" Then

ViewState.Add("strCmdButton", "Edit")
lblTech_f.Visible = False
lblTechTarget_f.Visible = False
lblBU_f.Visible = False
lblBUPerc_f.Visible = False
lblBUValue_f.Visible = False
ddlTech_f.Visible = False
txtTechTarget_f.Visible = False
ddlBU_f.Visible = False
txtBUPerc_f.Visible = False
btnIns_T_f.Visible = False
btnCancelIns_f.Visible = False
ddlPL_f.Visible = False
txtPLPerc_f.Visible = False
btninsert.Visible = False
btnIns_BU_f.Visible = False
btnIns_PL_f.Visible = False

Elseif....

End if
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top