I want to use Session variable's value on the markup. This is the code written on UserContorl.ascx, file on NavigateUrl I want to send the Username which is stored in the session variable. I don't want to set NavigateUrl value on PageLoad function for some reason.
Note the code gives the error: The server tag is not well formed.
<asp:Panel ID="pnlMenuItems" runat="server" HorizontalAlign="Left">
<asp:HyperLink ID="LinkLogout" runat="server" NavigateUrl="~/logout/"+
<%= HttpContext.Current.Session["UserName"].ToString(); %>> CssClass="pnlMenuItems"
ForeColor="#666666">Logout</asp:HyperLink>
</asp:Panel>
You can bind data within server tags. e.g.
<asp:HyperLink ID="LinkLogout" runat="server"
NavigateUrl="<%# LogoutUrl %>"
CssClass="pnlMenuItems"
ForeColor="#666666">Logout</asp:HyperLink>
then in your code behind:
protected string LogoutUrl {
get {
return "~/logout/" + HttpContext.Current.Session["UserName"].ToString();
}
}
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) { DataBind(); }
}
The NavigateUrl gets set during the call to DataBind() using this method. In my example, the value would get set during page load, but you wouldn't have to specifically do it. If you need it to happen at a different time during the page lifecycle, you can try calling DataBind() during a different event.
How about:
<asp:Panel ID="pnlMenuItems" runat="server" HorizontalAlign="Left">
<asp:HyperLink ID="LinkLogout" runat="server"
NavigateUrl="<%# "~/logout/" + HttpContext.Current.Session["UserName"].ToString() %>"
CssClass="pnlMenuItems"
ForeColor="#666666">Logout</asp:HyperLink>
</asp:Panel>
Related
I have a method being used on a button to redirect to a different page. There are several variables that are inserted into the URL to help navigate to what the user wants to see. It works well, except for the fact that the variables do not show up in the address bar.
Upon button click and redirect to the next page the url looks like: /Beta.aspx/?year=&track=&event=&car=27&session
How can I get my variables to show up in the address bar? Below is the code being used for the button click.
protected void btnConfirm_Click(object sender, EventArgs e)
{
string url = string.Format("Beta.aspx/?year={0}&track={1}&event={2}&car=27&session{3}",hidYear.Value, hidTrack.Value, hidEvent.Value, hidSession.Value);
Response.Redirect(url);
}
Button Setup
<telerik:RadImageButton ID="RadImageButton2" runat="server" Skin="Material" Text="27" OnClick="btnConfirm_Click">
</telerik:RadImageButton>
Form Tag
<form id="form1" runat="server" class="SmallFont">
Hidden Fields
<asp:HiddenField runat="server" ID="hidYear" />
<asp:HiddenField runat="server" ID="hidTrack" />
<asp:HiddenField runat="server" ID="hidEvent" />
<asp:HiddenField runat="server" ID="hidSession" />
When RadImageButton2 is clicked, the page is posted back to the server for processing. This process is called ASP.NET postback mechanism and IsPostback is normally used on page_load event to detect if the page is getting generated due to postback requested by a control on the page, or if the page is getting loaded for the first time. This is important for the case when values of the controls are set programmatically and should not be overwritten when page was posted back.
See this snippet:
protected void Page_Load(object sender, EventArgs e)
{
hidYear.Value = "";
hidTrack.Value = "";
hidEvent.Value = "";
hidSession.Value = "";
}
protected void Init_Click(object sender, EventArgs e)
{
hidYear.Value = "2020";
hidTrack.Value = "1";
hidEvent.Value = "2";
hidSession.Value = "0123456789";
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
string url = string.Format("Beta.aspx/?year={0}&track={1}&event={2}&car=27&session{3}",
hidYear.Value, hidTrack.Value, hidEvent.Value, hidSession.Value);
Response.Redirect(url);
}
It all will run well, but values on the redirect will be always "" because Page_Load() is called with every postback. However, with the following little change the values on redirect will be not changed and populated to the state before form submit:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
hidYear.Value = "";
hidTrack.Value = "";
hidEvent.Value = "";
hidSession.Value = "";
}
}
Note, actual values of controls can be found by looking at the source of the page
Bottom line here: hidden fields should have values when looking at the source before clicking on RadImageButton2 and Page_Load() should not call any code that changes those values, or should check for if (!IsPostBack).
P.S.
Simple redirect does not required server code and can be done with client script, example:
<script type="text/javascript">
function PageRedirect() {
window.location.href = "Beta.aspx/?year="
+ document.getElementById('<%=hidYear.ClientID%>').value
+ "&track=" + document.getElementById('<%=hidTrack.ClientID%>').value
+ "&event=" + document.getElementById('<%=hidEvent.ClientID%>').value
+ "&car=27&session" + document.getElementById('<%=hidSession.ClientID%>').value;
}
</script>
<asp:Button ID="Button1" runat="server" Text="Client Redirect"
OnClientClick="PageRedirect();return false;" />
You're saying that it does work. That means that the variables are being passed.
There is a chance that the solution is really, really simple.
Variables don't show up in the address bar until you click on it. Try clicking on the URL in your address bar, it will select the URL and the variables will show. If not, only then it has to do something with the code.
Ahh, there's the mistake Bro. there isn't any value for these hidden controls
<asp:HiddenField runat="server" ID="hidYear" />
<asp:HiddenField runat="server" ID="hidTrack" />
<asp:HiddenField runat="server" ID="hidEvent" />
<asp:HiddenField runat="server" ID="hidSession" />
either you can add value in the ASPX page as
<asp:HiddenField runat="server" ID="hidYear" Value="1234"/>
<asp:HiddenField runat="server" ID="hidTrack" Value="4321" />
<asp:HiddenField runat="server" ID="hidEvent" Value="1234" />
<asp:HiddenField runat="server" ID="hidSession" Value="2405"/>
or as mentioned by user https://stackoverflow.com/users/2316116/user2316116 you can use
hidYear.Value = "2020";
hidTrack.Value = "1";
hidEvent.Value = "2";
hidSession.Value = "0123456789";
the reason the value is not being displayed in the URL is simply that there isn't a value that can be displayed.
make sure to check for empty values if you're taking inputs from the user in future applications. 😀
You should not have '/' between 'beta.aspx' and '?'. Try removing that extra slash and see if that resolves your issue.
I have a gridview and I want to keep the headers as hyperlink to navigate the link in new tab
<asp:TemplateField
<HeaderTemplate>
<asp:HyperLink ID="hlnk" runat="server" Target="_blank" NavigateUrl='<%# GettheNavigateUrl()%>'>Header</asp:HyperLink>
</HeaderTemplate>
...........
protected string GettheNavigateUrl()
{
return "http://www.google.com/";
}
Now I want to pass the id to the method. How to do it '<%# GettheNavigateUrl(??)%>'
This will work with Jquery
NavigateUrl='<%# GettheNavigateUrl($(this).attr('id'))%>'>
Jquery is Third Party Library which helps you optimize js Code
or
Have a look at this link
http://weblogs.asp.net/gurusarkar/pass-querystring-parameter-with-navigaterurl-in-hyperlink-inside-a-gridview
try this:
in .aspx
<asp:HyperLink ID="hlnk" runat="server" Target="_blank" >Header</asp:HyperLink>
You can add RowDataBound method as GridView_RowDataBound
OnRowDataBound="GridView_RowDataBound"
in code
protected string GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
HyperLink hyp = (HyperLink)e.Row.FindControl("hyp");
hyp.NavigateUrl = "www.google.com" + hyp.ClientID;
// hyp.ClientID is your id of control
}
}
I'm having a repeater control and want to bind Images to the sc control for onitemdatabound event.
My markup is:
<sc:Link runat="server" ID="sclnk" Field="#" rel="iframe-960-540">
<sc:image id="scimage" runat="Server" field="#">
</sc:image>
</sc:Link>
And my code is:
Sitecore.Web.UI.WebControls.Link scBannerLink = e.Item.FindControl("sclnk") as Sitecore.Web.UI.WebControls.Link;
if (scBannerLink != null)
{
scBannerLink.DataBind(promoItem.ID.ToString(), promoItem.PromoLink.Field.InnerField.Name);
}
Sitecore.Web.UI.WebControls.Image scPromoImage = e.Item.FindControl("scimage") as Sitecore.Web.UI.WebControls.Image;
if (scPromoImage != null)
{
scPromoImage.DataBind(promoItem.ID.ToString(), promoItem.PromoImage.Field.InnerField.Name);
}
I'm not getting any error but not diaplaying images
I've never used the Databind method of the control to set the properties.
The easier solution is to specify the Fieldname and set the Item in your repeater:
<asp:Repeater runat="server" ID="rptImages">
<ItemTemplate>
<sc:Link runat="server" ID="scLnk" Field="MyLinkFieldName" Item="<%# Container.DataItem %>" Parameters="rel=iframe-960-540">
<sc:image id="scImage" runat="Server" Field="MyImageFieldName" Item="<%# Container.DataItem %>" />
</sc:Link>
</ItemTemplate>
</asp:Repeater>
And you can pass in the additional attributes in the Parameters field on the control as a URL encoded parameters string, e.g. Parameters="rel=iframe-960-540¶m2=value2¶m3=value3"
And your code behind binding the control should be:
protected void Page_Load(object sender, EventArgs e)
{
rptImages.DataSource = Sitecore.Context.Item.GetChildren(); // this needs to be changed to whatever your query is...
rptImages.DataBind();
}
In my aspx page, I have,
<asp:ListView ID="listview1" runat="server" DataSourceID="dtasrc_load">
<ItemTemplate>
<h4>
<asp:Label ID="lbl_titlename" runat="server" Text='<%#Eval("abt_vch_Title") %>'></asp:Label>
</h4>
<asp:LinkButton runat="server" OnClick="Content_Load" class="btn">Edit</asp:LinkButton>
<asp:HiddenField ID="hiddenID" runat="server" Value='<%#Eval("abt_int_ID") %>' />
</ItemTemplate>
</asp:ListView>
I need to access the value in the hidden field control so that I can pass that value to the database on the linkbutton click event. Below is where I've gotten so far.
protected void Content_Load(object sender, EventArgs e)
{
HiddenField hd = new HiddenField();
HiddenField myhiddenfield = new HiddenField();
myhiddenfield = (HiddenField)listview1.FindControl("hiddenID");
int myID = Convert.ToInt32(myhiddenfield.Value);
I get a run-time error as "Object not referenced to instance of an object". The value seems to be null.
Can anyone tell me why I am getting this? What should I do?
give your linkbutton an id
<asp:LinkButton runat="server" OnClick="Content_Load" class="btn"
id="editlinkbutton">Edit</asp:LinkButton>
and change your code to this
protected void Content_Load(object sender, EventArgs e)
{
LinkButton editlinkbutton = sender as LinkButton;
HiddenField myhiddenfield = editlinkbutton.NamingContainer.FindControl("hiddenID") as HiddenField;
int myID = Convert.ToInt32(myhiddenfield.Value);
}
edit: maybe linkbutton doesnt have to have an id, not sure. my linkbuttons usually have id's :)
I recently had a similar issue. Try not to look for System.Web.UI.WebControls.HiddenField, but rather for System.Web.UI.HtmlControls.HtmlInputHidden-class, here.
Additionally, you should be more cautious, rather use
System.Web.UI.HtmlControls.HtmlInputHidden hi =
listview1.FindControl("hiddenID") as ystem.Web.UI.HtmlControls.HtmlInputHidden;
if(hi != null)
...
I have the following markup / code block in the ASPX file.
The binding of the ddl is triggered after Page_Load event which is in the code behind file.
This results me not able to get the selected value of the dropdownlist if I were to use such flow.
However for some purpose I require it to work this way.
Any idea how I could get the dropdownlist selected value when a post back is being triggered (click of the button)?
Page URL: page.aspx?para1=0¶2=value
ASPX PAGE
<%
if (Convert.ToInt32(Request.QueryString["para1"]) == 0)
{
ddl.DataValueField = "value";
ddl.DataTextField = "text";
ddl.DataSource = ds; //ds is valid, exact code not shown
ddl.DataBind();
} else {
//write in this area
Response.Write("Not 0");
}
%>
<form runat="server" id="user_form" class="form-horizontal">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:UpdatePanel ID="updPanel" runat="server">
<ContentTemplate>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>
<%-- this button will call btnSave_Click to get the ddl's value--%>
<asp:Button runat="server" ID="btn" Text="Button" OnClick="btn_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//do my stuff
}
}
protected void btn_Click(object sender, EventArgs e)
{
int intValue = Convert.ToInt32(ddl.SelectedValue);
//do my stuff
}
The ASPX Page code block will run after Page_Load / page's lifecycle, then will determine what to do base on the url parameters.
Thanks in advance!
You could always throw in a hidden object and use jquery to copy the value to the hidden value based on a certain action without a postback and would do it client side like it sounds like you want it to do