Reputation: 4633
In my existing project, I am still understanding the codes from the previous developer. I am uncertain how Eval
in HTML forms are used.
I have an onclick
action on my html form which downloads a file depending on the username, item number, and document type.
It contains a hard coded value of DocType=10
.
It should be:
Group 1 = 10
Group 2 = 20
Group 3 = 30
Code snippet:
UserControl.ascx
<a href="#" onclick="openDialog('/SitePages/FileDownload.aspx?DocType=10&ItemNo=<%#Eval("ItemNo")%>&CustomerID=<%#Eval("CustomerID")%>')">
UserControl.ascx.cs
TBL_USER_PROFILEProvider uprovider = new TBL_USER_PROFILEProvider();
int DOC_TYPE;
// Document Types
const int G1_DOC_TYPE = 10;
const int G2_DOC_TYPE = 20;
const int G3_DOC_TYPE = 30;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string userName = SPContext.Current.Web.CurrentUser.Name;
TBL_USER_PROFILE p = uprovider.GetUser(userName);
if (p != null)
{
// get group permissions
List<string> G1List = uprovider.GetAccessByModuleName(p.UserProfileID, "Group1");
List<string> G2List = uprovider.GetAccessByModuleName(p.UserProfileID, "Group2");
List<string> G3List = uprovider.GetAccessByModuleName(p.UserProfileID, "Group3");
// check doc type and deny access if not any
if (G1List.Count != 0)
{
DOC_TYPE = G1_DOC_TYPE;
}
else if (G2List.Count != 0)
{
DOC_TYPE = G2_DOC_TYPE;
}
else if (G3List.Count != 0)
{
DOC_TYPE = G3_DOC_TYPE;
}
else
{
Response.Redirect("/SitePages/AccessDeny.aspx");
}
}
}
}
May I know how can I pull the value of DOC_TYPE
from code behind to my CS form?
p.s. DOC_TYPE
is DocumentType
, int
from TBL_DOCUMENT
table.
UPDATE after @tetsuya-yamamoto 's answer:
UserControl.ascx
<a href="#" runat="server" onclick="openDialog('/SitePages/FileDownload.aspx?DocType=<%# DocType %>&ItemNo=<%#Eval("ItemNo")%>&CustomerID=<%#Eval("CustomerID")%>')">
UserControl.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// other logic here
if (G1List.Count != 0)
{
DOC_TYPE = G1_DOC_TYPE;
}
else if (G2List.Count != 0)
{
DOC_TYPE = G2_DOC_TYPE;
}
else if (G3List.Count != 0)
{
DOC_TYPE = G3_DOC_TYPE;
}
else
{
Response.Redirect("/SitePages/AccessDeny.aspx");
}
Page.DataBind();
}
}
I will test out tomorrow as I am unable to access my server till then.
Do correct me if I corrected wrongly. Thank you!
Upvotes: 0
Views: 234
Reputation: 24957
Since you have an int
variable with default access modifier, you need to create a property which has public
or protected
access modifier which has same data type for visibility in page markup:
int DOC_TYPE;
public int DocType
{
get
{
return DOC_TYPE;
}
}
Then you can provide DocType
property using projection syntax <%= DocType %>
:
<a href="#" onclick="openDialog('/SitePages/FileDownload.aspx?DocType=<%= DocType %>&ItemNo=<%#Eval("ItemNo")%>&CustomerID=<%#Eval("CustomerID")%>')">
If you want to use that property in server control markup (i.e. having runat="server"
attribute), then you should use <%# DocType %>
binding syntax instead of <%= DocType %>
projection syntax and call Page.DataBind()
on Page_Load
method:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// other logic here
if (G1List.Count != 0)
{
DOC_TYPE = G1_DOC_TYPE;
}
else if (G2List.Count != 0)
{
DOC_TYPE = G2_DOC_TYPE;
}
else if (G3List.Count != 0)
{
DOC_TYPE = G3_DOC_TYPE;
}
else
{
Response.Redirect("/SitePages/AccessDeny.aspx");
}
Page.DataBind();
}
}
Upvotes: 1