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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with code behind

Status
Not open for further replies.

nc297

Programmer
Apr 7, 2010
162
US
I have security on my page and want to use sessions to grant access.

On the index.aspx.vb page I have

Session("FO") =Data1.OfficeCode (my validation to get the FO)
Session("Pos") = Data1.FnCode (my validation to get the Pos)


Region FO >300 DDS >300 FO & DDS 250 - 300
PHI 626 266 876
Area 2 28 24 80
227 1 4 7
C08 0 0 1
C14 5 0 3

I'm logged into this person who is Session("FO") = C14 and the Session("Pos") = CRGXX

So when I get to this page I only want to see links for the C14 row which I belong to. I still see all of the links for C08 and 227



I placed this in the code behind where the gridview is located but it does nothing.

If e.Row.Cells(1).Text = Session("FO").ToString() AndAlso Session("Pos").ToString() = "CRGXX" AndAlso e.Row.Cells(1).Text = Session("FO").ToString() _
AndAlso e.Row.Cells(2).Text = Session("FO").ToString() AndAlso Session("Pos").ToString() = "CRGXX" AndAlso e.Row.Cells(2).Text = Session("FO").ToString() _
AndAlso e.Row.Cells(3).Text = Session("FO").ToString() AndAlso Session("Pos").ToString() = "CRGXX" AndAlso e.Row.Cells(3).Text = Session("FO").ToString() Then
End If

What am I doing wrong?
 
I see at least 2 red flags
1. abusing http session
2. trying to filter the view instead of the data.
Code:
Session("FO") = Data1.OfficeCode (my validation to get the FO)
Session("Pos") = Data1.FnCode (my validation to get the Pos)
seems sketchy to me. I would use session only for truly user scoped data that is used across the entire site, and most of that can be handled by Context.CurrentUser.

Load the data when it's needed (per request). If performance needs to be optimized, this can be done later. starting with quantifying where the bottleneck is and resolving that (ie usually poor queries and/or the number of queries).
...
by the time the html is rendered it's text made for a browser to parse and a human to reader. formatting, layout, etc. are all applied and it difficult to separate the data from the presentation.

filtering/conditional formatting should be done before rendering. apply logic to the data bound to the grid, not the grid itself. the gridview has a number of event you can hook into to customize how it behaves.


Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I'm sorry I'm lost and don't have a clue what I should do. What are some of the events I can use to customize how validation behaves?
 
alright then :) lets start with what you do know.

what do you have that is working?
what part of that code doesn't work?

the is the conceptual approach I use to develop user interfaces

user makes request
get data => query database, load file, call web service, etc.
process data => manipulate the data so it's ready to display
display data => rendering html, no logic, just pluging values into templates

What are some of the events I can use to customize how validation behaves?
this may just be semantics, but validation relates to verifying user input. What you described is authorization and/or filtering (depending on the context, which is still unclear). you want to restrict access to data based on who the user is or what criteria the user supplied.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thank you so much!!!

Ok I have this session on my index.aspx.vb page:

Session("FO") = Data1.OfficeCode

I am looking for the office code of the person who is logging in. In this case it would be C14

Here's the Gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource3" EnableModelValidation="True" Width="856px">

<Columns>

<asp:BoundField DataField="Sort" HeaderText="Sort" ReadOnly="True"
SortExpression="Sort" Visible="False" />

<asp:BoundField DataField="doc" HeaderText="Region" ReadOnly="True" SortExpression="doc" />

<asp:BoundField DataField="totpenfodds" HeaderText="Total Pending"
SortExpression="totpenfodds" ReadOnly="True" DataFormatString="{0:N0}" />

<asp:BoundField DataField="pendinfo"
HeaderText="Pending In FO" ReadOnly="True"
SortExpression="pendinfo" DataFormatString="{0:N0}" >
</asp:BoundField>

<asp:BoundField DataField="pendndds" HeaderText="Pending In DDS"
SortExpression="pendndds" ReadOnly="True" DataFormatString="{0:N0}" />
<asp:TemplateField HeaderText="Pend In FO &gt; 300 Days"
SortExpression="pendfo300">
<ItemTemplate>
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<% # "T16doc.aspx?area=" & session("area") & "&" & "dist=" & session("dist") & "&" & "doc=" & " " & "&" & "type=" & "1" & "&" & "sort1=" & Eval("sort") & "&" & "sort2=" & Eval("doc")%>'
Text='<%# Eval("pendfo300") %>' Enabled='<%# IIF(Eval("pendfo300") = 0 OR (Session("DDSListings")=true) OR (Session("R03")=true), false, true) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pend In DDS &gt; 300 Days"
SortExpression="penddds300">
<ItemTemplate>
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<% # "T16doc.aspx?area=" & session("area") & "&" & "dist=" & session("dist") & "&" & "doc=" & " " & "&" & "type=" & "2" & "&" & "sort1=" & Eval("sort") & "&" & "sort2=" & Eval("doc")%>'
Text='<%# Eval("penddds300") %>' Enabled='<%# IIF(Eval("penddds300") = 0 OR (Session("DDSListings")=true) OR (Session("R03")=true), false, true) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Under the Gridview1_Rowdatabound I have

If e.Row.RowType = DataControlRowType.DataRow Then


Dim hl As New HyperLink() 'add new HyperLink to cell instead of text

If e.Row.Cells(4).Text = Session("FO").ToString() AndAlso e.Row.Cells(5).Text = Session("FO").ToString() Then
hl.Enabled = True
Else
hl.Enabled = False

End If

I have the right FO as I did a response.write session("FO") and I have C14 at the top of the page.

What's grey out is the first column Region with the code I wrote in the code behind.

Only three columns have links...Region, PendFO300 and PendDDS300.

What I want is for the Region column not to be touched and since I'm C14 I only want that row to have links and not the 227 and C08 rows. I hope this makes sense.

Region TotPend PendInFO PendInDDS PendFO300 PendDDS300
PHI 65,009 13,411 51,598 629 264
Area1 12,467 2,030 10,437 30 25
227 961 198 763 6 4
C08 127 24 103 0 0
C14 294 68 226 5 3

 
ok, this makes a little more sense.
1st, the use of session doesn't make sense. just reference Data1.OfficeCode when you need the current code. if needed, make Data1 a protected property so you can access it from both the code behind and the markup.
Code:
protected MyObject Data1 {get;set;}
2nd, filter out the restricted rows as part of the sql query.
3rd preform the logic in the row data bound event based on the data, not on the rendered html
Code:
if(e.Row.RowType != DataControlRowType.DataRow) return;

var data = (MyObject)e.Row.DataItem;
hl.Enabled = data.TheValue == Data1.OfficeCode;

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks! I will ask you to explain to me afterwards. I tried all but got this error message:


Public member 'TheValue' on type 'DataRowView' not found.


Here's the info I entered in rowdatabound I may be missing some steps such as step 2.

Protected Property IVFData1() As Object
Get
Return m_IVFData1
End Get
Set(ByVal value As Object)
m_IVFData1 = value
End Set
End Property
Private m_IVFData1 As Object

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Response.Write(Session("FO"))

Dim a = 0
e.Row.Cells(0).Visible = False

If e.Row.RowType <> DataControlRowType.DataRow Then
Return
End If

Dim hl As New HyperLink() 'add new HyperLink to cell instead of text
Dim data = DirectCast(e.Row.DataItem, Object)
hl.Enabled = data.TheValue = IVFData1.OfficeCode



Dim txt As String = e.Row.Cells(1).Text 'get the current text







hl.Visible = False 'just hide the link

Else
hl.Text = txt

If txt = "Area 1" Then
Session("area") = "01"
ElseIf txt = "Area 2" Then
Session("area") = "02"
ElseIf txt = "Area 3" Then
Session("area") = "03"
ElseIf txt = "Area 4" Then
Session("area") = "04"
ElseIf txt = "Area 5" Then
Session("area") = "05"
ElseIf txt = "Area 6" Then
Session("area") = "06"
ElseIf txt = "PHI" Then
Session("area") = "PHI"
Else
Session("dist") = txt
End If
End If
If txt = "Area 1" Or txt = "Area 2" Or txt = "Area 3" Or txt = "Area 4" Or txt = "Area 5" Or txt = "Area 6" Then
hl.NavigateUrl = "AreaDocListing.aspx?reg=" & Request.QueryString("reg") & "&" & "area" & "=" & Request.QueryString("area")
ElseIf Session("area") = "PHI" Then
hl.NavigateUrl = "Region.aspx"
End If
e.Row.Cells(1).Controls.Clear()

e.Row.Cells(1).Controls.Add(hl)





 
TheValue is pseudo code. you have some type of object that is holding the data. on that object there is a way to access the value you are after, that's what this represents.

there is never a reason to call
Code:
e.Row.Cells(#).Text
especially not to preform logical operations.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
okay thanks will look into it in the morning. I had left work after your last reply.
 
I'm sorry I don't understand it at all. I need to cell and then the row that FO is located in so the links won't appear.

I tried this and all it does is hide the pendfo300 column:

If e.Row.RowType = DataControlRowType.DataRow Then

Dim txt As String = e.Row.Cells(1).Text 'get the current text

'Dim hl As New HyperLink() 'add new HyperLink to cell instead of text
Dim hl As HyperLink = DirectCast(e.Row.FindControl("hyperlink1"), HyperLink)

If e.Row.Cells(0).Text = Session("FO").ToString() Then 'AndAlso e.Row.Cells(0).Text = Session("POS").ToString() Then
e.Row.FindControl("pendfo300").Visible = True
'Else
' e.Row.FindControl("pendhyperlink3").Visible = False
End If
 
these lines are the problem
Code:
Dim txt As String = e.Row.Cells(1).Text 'get the current text
//and
e.Row.Cells(0).Text
you don't want to use the rendered html to determine page layout. use the data that is bound to the control instead. this value comes from the row's DataItem property
Code:
var link = e.FindControl("id of control") as HyperLink;
if(link == null) return;

var data = (whatever type of object this is)e.Row.DataItem;
var value = data.TheValueYouWantToUseForTheComparison;
var link.Visible = value == value to compare;
there is another approach you could take as well.
define a boolean property on the data before binding to the grid. then just set the Visible property in the markup
Code:
var data = getdata();
foreach(var d in data)
{
  d.showlink = d.somevalue == compared to;
}
grid.DataSource = data;
DataBind();
Code:
<asp:gridview...>
...
<asp:HyperLink ... Visible='<%#Eval("showlink")%>'
this code is much cleaner and simpler. depending on how you get the data this may be easy to implement.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Ugh...been trying this for two days now and still can't get it.

You said this is the problem but I need this to get my links in the first column (0).

Any suggestions?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top